Python Pygame,移动一个矩形,并移除先前位置的旧矩形

Python Pygame,移动一个矩形,并移除先前位置的旧矩形,python,while-loop,pygame,draw,rect,Python,While Loop,Pygame,Draw,Rect,我写这段代码是因为我想用箭头键移动一个矩形。 它可以工作,但循环循环不会移动矩形,而是每次创建一个新的矩形。结果就像一条轨迹。您可以在图片中看到: 下面是我写的代码: 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.im

我写这段代码是因为我想用箭头键移动一个矩形。 它可以工作,但循环循环不会移动矩形,而是每次创建一个新的矩形。结果就像一条轨迹。您可以在图片中看到:

下面是我写的代码:

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的一些原理,但老实说,我不知道是哪一个。

它正按照你的要求做:每次都画一个新的矩形。计算机正以这种方式令人沮丧。您使用了绘制而不是移动对象

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

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

  • 它正在做你让它做的事情:每次画一个新的矩形。计算机正以这种方式令人沮丧。您使用了绘制而不是移动对象

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

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

  • 每次运行循环时,都会在旧屏幕的图像上绘制。因此,在您的例子中,您正在前面的矩形之上绘制矩形。您需要清除屏幕上循环的上一次迭代中出现的内容。最简单的方法是用单一颜色或图像填充屏幕:

    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()
    

    每次运行循环时,都会在旧屏幕的图像上绘制。因此,在您的例子中,您正在前面的矩形之上绘制矩形。您需要清除屏幕上循环的上一次迭代中出现的内容。最简单的方法是用单一颜色或图像填充屏幕:

    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()