pythonpygameflappy-bird-clone

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. But I wrote the int()


I uploaded a whole of code,I am sure that the problem is around "def score_display" I was doing the exact same thing what author did. But infortunately I got the error. At the beginning the code runs perfectly and when I fall and the function where I have mistaken should be fulfilled then the game breaks and gives this error. Thanks (so sorry about grammar)

import pygame
import sys, random


def floor_repetition():
    win.blit(floor_surface, (floor_x_pos,588))
    win.blit(floor_surface, (floor_x_pos + 576,588))



def create_pipe():
    random_pipe_pos = random.choice(pipe_height)
    bottom_pipe = pipe_surface.get_rect(midtop = (700, random_pipe_pos))
    top_pipe = pipe_surface.get_rect(midbottom = (700, random_pipe_pos - 170))
    return bottom_pipe, top_pipe


def move_pipes(pipes):  
    for pipe in pipes:
        pipe.centerx -= 5
    return pipes


def draw_pipes(pipes):
    for pipe in pipes:
        if pipe.bottom >= 588:
            win.blit(pipe_surface, pipe)
        else:
            flip_pipe = pygame.transform.flip(pipe_surface, False, True)
            win.blit(flip_pipe,pipe)

def check_collision(pipes):
    for pipe in pipes:
        if bird_rect.colliderect(pipe):
            death_sound.play()
            return False
    
    if bird_rect.top <= -100 or bird_rect.bottom >= 588:
        return False

    return True


def rotate_bird(bird1):
    new_bird = pygame.transform.rotozoom(bird1, -bird_movement * 3,1)
    return new_bird


def bird_animation():
    new_bird = bird_frames[bird_index]
    new_bird_rect = new_bird.get_rect(center = (100, bird_rect.centery))
    return new_bird, new_bird_rect

def score_display(game_state):
    if game_state == 'main_game':
        score_surface = game_font.render(str(int(score)), True,(255,255,255))   
        score_rect = score_surface.get_rect(center = (288,50))
        win.blit(score_surface, score_rect)

    if game_state == 'game over':
        score_surface = game_font.render(f'Score: {(int(score))}', True,(255,255,255))  
        score_rect = score_surface.get_rect(center = (288,50))
        win.blit(score_surface, score_rect)

        high_score_surface = game_font.render(f'High Score: {(int(high_score))}', True,(255,255,255))   
        high_score_rect = high_score_surface.get_rect(center = (288,250))
        win.blit(high_score_surface, high_score_rect)
        
def update_score(score, high_score):
    if score > high_score:
        high_score = score
        return high_score
pygame.mixer.pre_init(frequency = 44100, size = 16, channels = 1, buffer = 512)
pygame.init()

#pygame.set_caption('Flappy bird')

win = pygame.display.set_mode((576,700))
clock = pygame.time.Clock()
game_font = pygame.font.Font('04B_19.TTF',40)


# Game valiables
gravity = 0.25
bird_movement = 0
game_active = True
score = 0
high_score = 0

bg = pygame.image.load('assets/background-day.png').convert()
bg = pygame.transform.scale2x(bg)

floor_surface = pygame.image.load('assets/base.png')
floor_surface = pygame.transform.scale2x(floor_surface)
floor_x_pos = 0




bird_downflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-downflap.png').convert_alpha())
bird_midflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-midflap.png').convert_alpha())
bird_upflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-upflap.png').convert_alpha())
bird_frames = [bird_downflap, bird_midflap, bird_upflap]
bird_index = 2
bird = bird_frames[bird_index]
bird_rect = bird.get_rect(center = (100,350))


BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP, 200)



# bird
#bird = pygame.image.load('assets/bluebird-midflap.png').convert_alpha()
#bird = pygame.transform.scale2x(bird)
#bird_rect = bird.get_rect(center = (100,350))

pipe_surface = pygame.image.load('assets/pipe-green.png')
pipe_surface = pygame.transform.scale2x(pipe_surface)
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)
pipe_height = [200,300,400]


game_over_surface = pygame.transform.scale2x(pygame.image.load('assets/message.png').convert_alpha())
game_over_rect = game_over_surface.get_rect(center = (288,350))

flap_sound = pygame.mixer.Sound('sound/sfx_wing.wav')
death_sound = pygame.mixer.Sound('sound/sfx_hit.wav')
score_sound = pygame.mixer.Sound('sound/sfx_point.wav')
score_sound_countdown = 100

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and game_active:
                bird_movement = 0
                bird_movement -= 6
                flap_sound.play()

            if event.key == pygame.K_SPACE and game_active == False:
                game_active = True
                pipe_list.clear()
                bird_rect.center = (100,350)
                bird_movement = 0
                score = 0


        if event.type == SPAWNPIPE:
            pipe_list.extend(create_pipe())

        if event.type == BIRDFLAP:
            if bird_index < 2:
                bird_index += 1
            else:
                bird_index = 0

            bird, bird_rect = bird_animation()


    win.blit(bg, (0,0))

    if game_active:
        # bird
        bird_movement += gravity
        rotated_bird = rotate_bird(bird)
        bird_rect.centery += bird_movement
        win.blit(bird, bird_rect)
        game_active = check_collision(pipe_list)
        
        # pipes
        pipe_list = move_pipes(pipe_list)
        draw_pipes(pipe_list)

        score += 0.01
        score_display('main game')
        score_sound_countdown -= 1
        if score_sound_countdown <= 0:
            score_sound.play()
            score_sound_countdown = 100
    else:
        win.blit(game_over_surface,game_over_rect)
        high_score = update_score(score, high_score)
        score_display('game over')

    # floor
    floor_x_pos -= 1
    floor_repetition()
    if floor_x_pos <= -576:
        floor_x_pos = 0
    win.blit(floor_surface, (floor_x_pos,588))



    pygame.display.update()
    clock.tick(120)

This is the error itself

Flappy bird.py:173: DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
  bird_rect.centery += bird_movement
Traceback (most recent call last):
  File "Flappy bird.py", line 190, in <module>
    score_display('game over')
  File "Flappy bird.py", line 65, in score_display
    high_score_surface = game_font.render(f'High Score: {(int(high_score))}', True,(255,255,255))   
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Solution

  • The problem is in update_score where you return a None when score <= high_score. Just dedent that return statement.

    def update_score(score, high_score):
        if score > high_score:
            high_score = score
        return high_score