移动球时如何让pygame画一条线?目前我有它,这样当你使用箭头时,一个带有黑色边框的大红球会在屏幕上移动。我想要的是让用户按下一个按钮,然后它基本上希望触发 pygame 画一条线,然后如果用户再次按下该键,它将不再绘制,然后如果他们按下一个按钮,它将清除它。我想合并到我当前的代码中。我已经对 pygame.draw 进行了研究,并在 pygame 中绘图,但没有运气。这是我当前的代码:
import os,sys
import pygame as pg
DIRECTDICT = {pg.K_LEFT : (-1, 0),
pg.K_RIGHT : ( 1, 0),
pg.K_UP : ( 0,-1),
pg.K_DOWN : ( 0, 1)}
class Player:
def __init__(self,rect,speed):
self.rect = pg.Rect(rect)
self.speed = speed
self.movement = [0,0]
self.make_image()
def make_image(self):
self.image = pg.Surface((self.rect.size)).convert_alpha()
self.image.fill((0,0,0,0))
pg.draw.ellipse(self.image,(0,0,0),(1,1,self.rect.size[0]-2,self.rect.size[1]-2))
pg.draw.ellipse(self.image,(255,0,0),(6,6,self.rect.size[0]-12,self.rect.size[1]-12))
def update(self,Surf):
self.rect.move_ip(self.movement)
self.draw(Surf)
def draw(self,Surf):
Surf.blit(self.image,self.rect)
def quit_game():
pg.quit();sys.exit()
def game(Player):
for event in pg.event.get():
Player.movement = [0,0]
keys = pg.key.get_pressed()
if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
quit_game()
for key in DIRECTDICT:
if keys[key]:
for i in (0,1):
Player.movement[i] += DIRECTDICT[key][i]*Player.speed
def main(Surf,Player):
game(Player)
Surf.fill((255,255,255))
Player.update(Surf)
pg.display.update()
if __name__ == "__main__":
os.environ['SDL_VIDEO_CENTERED'] = '1'
Surface = pg.display.set_mode((500,500))
pg.init()
Myclock = pg.time.Clock()
Myplayer = Player((250,250,100,100),3)
while 1:
main(Surface,Myplayer)
Myclock.tick(60)