【问题标题】:Pygame, move a rectangle and remove the old rectangle in precedent positionPygame,移动一个矩形并删除先前位置的旧矩形
【发布时间】:2017-03-21 18:24:05
【问题描述】:

我正在编写此代码,因为我想使用箭头键移动一个矩形。 它可以工作,但循环不会移动矩形,而是每次都会创建一个新矩形。结果就像一个轨迹。您可以在图片中看到它:the result of the code after some key pressure

这里是我写的代码:

import sys
import pygame

pygame.init()
pygame.display.set_caption('SAGA')
clock=pygame.time.Clock()
FPS=30
#the initial position
POS_X=300
POS_Y=300
ship = pygame.image.load("fighter_0.png")

while True:
    #if cycle for detect the key pressure
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_UP:
                POS_Y=POS_Y-10
        if event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_DOWN:
                POS_Y=POS_Y+10
        if event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_RIGHT:
                POS_X=POS_X+10
        if event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_LEFT:
                POS_X=POS_X-10
    #here i draw the rectangle 
    pygame.draw.rect(screen,(255,255,255),(POS_X,POS_Y,30,30))
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()

我觉得我对pygame的一些原理不太了解,但老实说我不知道​​是哪一个。

【问题讨论】:

    标签: python while-loop pygame draw rect


    【解决方案1】:

    它完全按照您的指示进行操作:每次都绘制一个新矩形。计算机就是这样令人沮丧的。您使用 draw 而不是移动对象。

    要解决此问题,您有两种基本选择:

    1. 在每次迭代中,首先用与背景匹配的矩形(黑色)来消除前一个矩形。 然后绘制新的矩形。
    2. 使用可移动对象,例如精灵,并使用 move 方法更新您的游戏,而不是重绘。如果您在 StackOverflow 或互联网上搜索“pygame move object”,您可能会发现很多代码可以用于此目的。

    【讨论】:

      【解决方案2】:

      每次运行循环时,都会覆盖旧屏幕的图像。因此,在您的情况下,您正在先前的矩形之上绘制矩形。您需要清除上一次循环迭代的屏幕。最简单的方法是用单一颜色或图像填充屏幕:

      import sys
      import pygame
      
      # Colours
      black = [0, 0, 0]
      
      pygame.init()
      screen = pygame.display.set_mode((640,480))
      pygame.display.set_caption('SAGA')
      clock=pygame.time.Clock()
      FPS=30
      #the initial position
      POS_X=300
      POS_Y=300
      ship = pygame.image.load("fighter_0.png")
      
      while True:
          #if cycle for detect the key pressure
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  pygame.quit()
                  sys.exit()
              if event.type == pygame.KEYDOWN:
                  if event.key ==pygame.K_UP:
                      POS_Y=POS_Y-10
              if event.type == pygame.KEYDOWN:
                  if event.key ==pygame.K_DOWN:
                      POS_Y=POS_Y+10
              if event.type == pygame.KEYDOWN:
                  if event.key ==pygame.K_RIGHT:
                      POS_X=POS_X+10
              if event.type == pygame.KEYDOWN:
                  if event.key ==pygame.K_LEFT:
                      POS_X=POS_X-10
          #here i draw the rectangle 
          screen.fill(black) # Fill the entire screen with black
          pygame.draw.rect(screen,(255,255,255),(POS_X,POS_Y,30,30))
          clock.tick(FPS)
          pygame.display.update()
      pygame.quit()
      

      【讨论】:

        猜你喜欢
        • 2014-12-02
        • 1970-01-01
        • 2020-12-01
        • 2021-02-06
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        相关资源
        最近更新 更多