Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用d-pad移动矩形_Python_Pygame - Fatal编程技术网

Python 使用d-pad移动矩形

Python 使用d-pad移动矩形,python,pygame,Python,Pygame,我试图通过使用键盘上的d-pad移动一个矩形来理解pygame的基本原理,但矩形并没有移动,而是停留在初始位置 import pygame import sys x1=0 y1=0 x2=100 y2=100 pygame.init() screen=pygame.display.set_mode((288,512)) clock=pygame.time.Clock() x = pygame.draw.rect(screen, (0, 255, 0), (0, 0, x2, y2)) wh

我试图通过使用键盘上的d-pad移动一个矩形来理解pygame的基本原理,但矩形并没有移动,而是停留在初始位置

import pygame
import sys

x1=0
y1=0
x2=100
y2=100

pygame.init()
screen=pygame.display.set_mode((288,512))
clock=pygame.time.Clock()
x = pygame.draw.rect(screen, (0, 255, 0), (0, 0, x2, y2))

while True:
    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_DOWN:
               y1-=100



pygame.display.update()
clock.tick(120)

您必须在每一帧中重新绘制整个场景。这意味着您必须清除显示,绘制矩形并在每一帧中更新显示。
此外,还需要在位置(x1,y1)处绘制矩形。使用时,必须指定矩形的左上角位置以及宽度和高度:

导入pygame
导入系统
x1,y1,w1,h1=0,01000
pygame.init()
screen=pygame.display.set_模式((288512))
clock=pygame.time.clock()
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_向下:
y1+=10
屏幕填充(0)
x=pygame.draw.rect(屏幕,(0,255,0),(x1,y1,w1,h1))
pygame.display.update()
时钟滴答(120)

您从未使用过y1,您希望它如何移动?@Ruli我仍然感到困惑,我将x1和y1添加到了x变量中,但我仍然无法使其移动查看我的答案,您需要在按下箭头时移动矩形并更新屏幕以查看更改