为什么蛇不向下移动(向下键盘)

问题描述 投票:-1回答:1

我正在用python编写蛇游戏,并且蛇能够左右移动,当我尝试将其向下移动(使用箭头时,总是会出现错误。我正在碰撞,因为蛇会与自身碰撞(蛇头触碰到身体),屏幕将显示“ GAME OVER- YOU LOST”。

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)
python pygame collision
1个回答
0
投票

collision_y = playerRect.centrex中的elif dy < 0:行的中心拼写错误

© www.soinside.com 2019 - 2024. All rights reserved.