Pygame 矩形不动? [英] Pygame the rectangle is not moving?
问题描述
你好,我是 pygame 的新手.当我尝试向右或向左移动 rect
时.矩形不会从一个位置移动到另一个位置,而是向右或向左扩展/延伸.
为什么?
导入pygame、sys、timepygame.init()红色 = (255, 0, 0)gameDisplay = pygame.display.set_mode((800, 600))pygame.display.set_caption('MyGame')移动_x = 200移动_y = 200为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()系统退出()如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_LEFT:移动_x -= 10如果 event.key == pygame.K_RIGHT:移动_x += 10# pygame.Rect.move(10, 10)# gameDisplay.fill(white, rect=[move_x, move_y, 10, 100])pygame.draw.rect(gameDisplay, red, [move_x, move_y, 10, 10])pygame.display.update()pygame.display.flip()
请看上图.按左右键后的样子.
绘制前使用surface.fill.
我使用的 [0, 0, 0]
在 RGB 代码中是黑色的.你应该声明类似
BLACK = (0, 0, 0)
作为避免重复的常量.所以请改变它并像上面一样使用它.
导入pygame、sys、timepygame.init()红色 = (255, 0, 0)gameDisplay = pygame.display.set_mode((800, 600))pygame.display.set_caption('MyGame')移动_x = 200移动_y = 200为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()系统退出()如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_LEFT:移动_x -= 10如果 event.key == pygame.K_RIGHT:移动_x += 10# pygame.Rect.move(10, 10)gameDisplay.fill([0,0,0]) # 添加的行.pygame.draw.rect(gameDisplay, red, [move_x, move_y, 10, 10])pygame.display.update()pygame.display.flip()
注意不要在 fill
方法之前画任何东西,它会被擦除,因为你用别的东西填满了屏幕.
我刚刚意识到你已经定义了red
.如果您将其全部大写,则可能会更好.红色
.因为它是 PEP-8 建议的全局常量.>
Hello I am new to pygame.
When I am trying to move my rect
right or left. The rectangle does not move from one position to other rather it expands/extends towards right or left.
Why?
import pygame, sys, time
pygame.init()
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('MyGame')
move_x = 200
move_y = 200
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_x -= 10
if event.key == pygame.K_RIGHT:
move_x += 10
# pygame.Rect.move(10, 10)
# gameDisplay.fill(white, rect=[move_x, move_y, 10, 100])
pygame.draw.rect(gameDisplay, red, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.display.flip()
Please see the image above. How it looks like after pressing right or left keys.
Use surface.fill before drawing.
The [0, 0, 0]
I used is black in RGB codes. You should declare something like
BLACK = (0, 0, 0)
As a constant to avoid repetition. So please change that and use it like above.
import pygame, sys, time
pygame.init()
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('MyGame')
move_x = 200
move_y = 200
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_x -= 10
if event.key == pygame.K_RIGHT:
move_x += 10
# pygame.Rect.move(10, 10)
gameDisplay.fill([0,0,0]) # The line added.
pygame.draw.rect(gameDisplay, red, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.display.flip()
Be careful not to draw anything before the fill
method, it will be erased because you filled the screen with something else.
Edit: I just realized you already defined red
. It is probably better if you declared it all caps. RED
. Because it is a global constant as PEP-8 suggests.
这篇关于Pygame 矩形不动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!