M
M
megasaur2020-04-03 11:49:11
Python
megasaur, 2020-04-03 11:49:11

I can't find what's wrong. who will help?

mistake:

Traceback (most recent call last):
  File "C:/DinoGame/DinoGame.py", line 92, in <module>
    run_game()
  File "C:/DinoGame/DinoGame.py", line 54, in run_game
    draw_array(cactus_arr)
  File "C:/DinoGame/DinoGame.py", line 86, in draw_array
    check = cactus.move()
  File "C:/DinoGame/DinoGame.py", line 29, in move
    if self.x >= -self.width:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'


the code:
import pygame
import random
pygame.init()
display_width = 800
display_height = 600
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('DinoRun')
icon = pygame.image.load('Sprite-0001.png')
pygame.display.set_icon(icon)
usr_width = 60
usr_height = 100
usr_x =display_width//3
usr_y =display_height-100-100
clock = pygame.time.Clock()
make_jump = False
jump_counter = 30
cactus_width =20
cactus_height =70
cactus_x=800-50
cactus_y=600-100-70
class Cactus:
    def __init__(self,x,y,width,height,speed):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.speed = speed
    def move(self):
        if self.x >= -self.width:
            pygame.draw.rect(display, (5, 149, 0), (self.x, self.y, self.width, self.height))
            self.x -= self.speed
            return True
        else:
            self.x = display_width + 100 + random.randrange(-80,60)
            return False
    def return_self(self,radius):
        self.x = radius
def run_game():
    global make_jump
    game = True
    cactus_arr =[]
    create_cactus_arr(cactus_arr)
    while game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE]:
            make_jump = True
        if make_jump:
            jump()
        display.fill((4,192,232))
        draw_array(cactus_arr)
        pygame.draw.rect(display,(232,44,4),(usr_x,usr_y,usr_width,usr_height))
        pygame.display.update()
        clock.tick(80)
def jump():
    global usr_y, make_jump, jump_counter
    if jump_counter >= -30:
        usr_y -= jump_counter/2.5
        jump_counter -=1
    else:
        jump_counter = 30
        make_jump = False
def create_cactus_arr(array):
    array.append(Cactus(display_width+20,display_height-170,20,70,4))
    array.append(Cactus(display_width+300,display_height-150,30,50,4))
    array.append(Cactus(display_width+600,display_height-180,25,80,4))
def find_radius(array):
    maximum = max(array[0].x, array[1].x, array[2].x)
    if maximum < display_width:
        radius = display_width
        if radius - maximum < 50:
            radius += 150
        else:
            radius = maximum
        choise = random.randrange(0,5)
        if choise == 0:
            radius += random.randrange(10,15)
        else:
            radius += random.randrange(200,350)
        return radius
def draw_array(array):
    for cactus in array:
        check = cactus.move()
        if not check:
            radius = find_radius(array)
            cactus.return_self(radius)


run_game()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2020-04-03
@megasaur

Well look. Message

self.x >= -self.width:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'

indicates to us that during the error Initially I see x is filled. This means that during operation it is set to None at some point. Such a problematic moment is visible only in return_self , where its value is set to a certain passed radius , which in turn is obtained from the find_radius function. A cursory glance at this function shows that the only return in it, due to indentation, is inside the if block . Those. if the condition is not met, the function just returns None, causing a program error. So the problem is to be found there. Since I don’t know the planned logic of the game, I’ll assume that it’s enough just to correct this line withself.x = None
self.x = radius
return radius (remove extra indent), it is logical that it returns the result regardless of the condition.

A
Alexander, 2020-04-03
@swwso1

Re-read carefully the error itself, check the lines it points to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question