矩形为什么不移动?

时间:2017-05-23 01:42:32

标签: python pygame

这是我的代码:

import pygame
from pygame.locals import *

#define the player
class Player(pygame.sprite.Sprite):
 def __init__(self):
  super(Player, self).__init__()
  self.surf = pygame.Surface((50, 25))
  self.surf.fill((255,255,255))
  self.rect = self.surf.get_rect()

 def update(self, pressed_keys):
  """
  if pressed_keys[K_UP]:
   self.rect.move_ip(0, -5)
  if pressed_keys[K_DOWN]:
   self.rect.move_ip(0, 5)
  """
  if pressed_keys[K_LEFT]:
   self.rect.move_ip(-5, 0)
  if pressed_keys[K_RIGHT]:
   self.rect.move_ip(5, 0)

  #keep player on the screen
  if self.rect.left < 0:
   self.rect.left = 0
  elif self.rect.right > 400:
   self.rect.right = 400

#initialization
pygame.init()

#create the screen object
screen = pygame.display.set_mode((400, 600))

#instantiate the player
player = Player()

running = True

#main loop
while running:
 for event in pygame.event.get():
  if event.type == KEYDOWN:
   if event.key == K_ESCAPE:
    running = False
  elif event.type == QUIT:
   running = False

 #draw the player to the screen
 screen.blit(player.surf, (200, 550))

 pressed_keys = pygame.key.get_pressed()

 player.update(pressed_keys)

 #update the display
 pygame.display.flip()

一切都显示出来,但是我无法用箭头键移动矩形而没有显示任何错误,帮助..提前感谢!我还添加了一个打印来检查pressed_keys是否有效,并且在不移动矩形的情况下进行打印。

1 个答案:

答案 0 :(得分:1)

很高兴看到您正在使用pygame:D

在Python中,编写“好”代码很重要,这意味着代码应该遵循所有语法规则,正确缩进,最重要的是,要清晰可读。

以下是修复代码的一些准则(遵循PEP 8指南):

  1. 始终将代码缩进4个空格或标签

  2. 注释字符("#")后面应始终跟一个空格,即:"# <= there's a space there"

  3. 永远不应有太多空的连续新行

  4. 在列表/元组/集中的逗号后面留一个空格,即(10, 10, 10)

  5. 这里有一些pygame指南:

    1. 您不应明确使用pygame.key.get_pressed()来检查每个循环的按键

    2. 检查for event in pygame.event.get()

    3. 中的按键
    4. 使用clock.tick设置帧速率,最小帧速率应为30,这将使程序保持恒定速度

    5. 总是用一种颜色填充你的屏幕,即使它是黑色的,screen.fill((0, 0, 0))

    6. 每次循环后重新填充屏幕。 screen.fill((0, 0, 0))

    7. 以下是您一直在等待的内容,如何解决您的问题;实际上很简单。 screen.blit(player.surf, (200, 550))您将位置设置为(200, 550)。相反,您可能想要的是将其更改为screen.blit(player.surf, player.rect),这会将player blit到其位置(“rect”)。

      以下是我的所有建议后代码的完整修改版本:

      import pygame
      from pygame.locals import *
      
      
      # define the player
      class Player(pygame.sprite.Sprite):
          def __init__(self):
              super(Player, self).__init__()
              self.surf = pygame.Surface((50, 25))
              self.surf.fill((255, 255, 255))
              self.rect = self.surf.get_rect()
      
          def update(self, pressed_keys):
              if pressed_keys == K_LEFT:
                  self.rect.move_ip(-5, 0)
              if pressed_keys == K_RIGHT:
                  self.rect.move_ip(5, 0)
              # keep player on the screen
              if self.rect.left < 0:
                  self.rect.left = 0
              elif self.rect.right > 400:
                  self.rect.right = 400
      
      # initialization
      pygame.init()
      
      # create the screen object
      screen = pygame.display.set_mode((400, 600))
      screen.fill((0, 0, 0))
      
      # instantiate the player
      player = Player()
      
      # instantiate the ticker
      clock = pygame.time.Clock()
      
      # mainloop
      running = True
      while running:
          for event in pygame.event.get():
              if event.type == KEYDOWN:
                  if event.key == K_ESCAPE:
                      running = False
                  elif event.key == K_LEFT:
                      player.update(event.key)
                  elif event.key == K_RIGHT:
                      player.update(event.key)
              elif event.type == QUIT:
                  running = False
      
          screen.fill((0, 0, 0))
          # draw the player to the screen
          screen.blit(player.surf, player.rect)
          # update the display
          pygame.display.flip()
          clock.tick(30)