почему змея не движется вниз (клавиатура вниз) - PullRequest
0 голосов
/ 10 апреля 2020

Я кодирую игру со змеями с помощью python, и змея может двигаться вверх, влево и вправо, и всегда возникает ошибка, когда я пытаюсь переместить ее вниз (используя стрелки). Я работаю со столкновением, так как змея столкнется сама с собой (голова змеи касается тела), на экране отобразится «ИГРА НАД - ВЫ ПОТЕРЯЛИ».

Traceback (most recent call last):
 File "/Users/myname/Documents/SnakeGame.py", line 107, in <module>
   collision_y = playerRect.centrex
AttributeError: 'pygame.Rect' object has no attribute 'centrex'
#timer for the game and frames per second
startTime = time.perf_counter()
FPS = 300
elapsedTime = 0

#creating numbers and text
GameOverFont = pygame.font.SysFont ('arial', 20)
GameOverMsg = GameOverFont.render("GAME OVER - YOU LOST", True, (RED))
screen.blit (GameOverMsg, [screenCentreX, screenCentreY])

collision_x = 0
collision_y = 0
collision_colour = None

clock = pygame.time.Clock()
speed = 3
screen.fill(WHITE)


#create a Rect to hold the player
# you need to give it the x,y, width, height parameters
playerRect = pygame.Rect(startX,startY,32,32)

# set main loop to True so it will run
main = True
# main loop
while main:
    for event in pygame.event.get(): 
        if event.type ==pygame.QUIT: 
            main = False 
        if event.type ==pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                dx = 0
                dy = -speed
            elif event.key == pygame.K_DOWN:
                dx = 0
                dy = speed
            elif event.key == pygame.K_LEFT:  # note: this section of code
                dx = -speed                  # doesn't have to change from
                dy = 0                        # code not using Rects
            elif event.key == pygame.K_RIGHT:
                dx = speed
                dy = 0
            elif event. key == pygame.K_q:
                pygame.quit()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                dx = 0
                dy = 0
            elif event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                dx = 0
                dy = 0

      #frames per second the program is running on
        clock.tick(FPS)


        if dx > 0:
                collision_x = playerRect.right + 1
                collision_y = playerRect.centery
                collision_colour = screen.get_at((collision_x,collision_y))

        elif dx < 0:
                collision_x = playerRect.left - 1
                collision_y = playerRect.centery
                collision_colour = screen.get_at((collision_x,collision_y))
        elif dy > 0:
                collision_x = playerRect.bottom + 1
                collision_y = playerRect.centrex
                collision_colour = screen.get_at((collision_x,collision_y))
        elif dy < 0:
                collision_x = playerRect.top - 1
                collision_y = playerRect.centerx
                collision_colour = screen.get_at((collision_x,collision_y))

        if collision_colour == BLUE:
                screen.fill(BLACK)
                startX = screenCentreX
                startY = screenCentreY
                dx = 0
                dy = 0
                main = False
                gameover = True
                elapsedTime = int(time.perf_counter() - startTime)

1 Ответ

0 голосов
/ 10 апреля 2020

У вас центр с ошибкой на линии collision_y = playerRect.centrex в elif dy < 0:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...