【问题标题】:why isn't the snake moving down (down keyboard)为什么蛇不向下移动(向下键盘)
【发布时间】:2020-04-10 04:19:36
【问题描述】:

我正在用 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)

【问题讨论】:

  • 您的错误消息告诉您有错字。我认为应该是“centerx”而不是“centrex”。
  • ^ 是的,这个评论是正确的。您可能不小心将英式拼写换成了美式拼写。

标签: python pygame collision


【解决方案1】:

elif dy &lt; 0: 中的 collision_y = playerRect.centrex 行有一个拼写错误的中心

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2015-09-23
    • 2016-10-11
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多