1

移动球时如何让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) 
4

1 回答 1

2

您必须保持一个状态,该状态指示是否应绘制线。此外,您还需要一个存储玩家位置的列表,以便在这些点之间画一条线。

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

state = 0
points = []
def game(Player):
    global state
    global points
    keys = pg.key.get_pressed()
    for event in pg.event.get():
        if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
            quit_game()
        ### change state when space is pressed
        if event.type == pg.KEYDOWN and event.key == pg.K_SPACE:
            if state == 0:
                state = 1
            elif state == 1:
                state = 2
            else:
                ### clear the points list
                state = 0
                points = []
    Player.movement = [0,0]
    for key in DIRECTDICT:
        if keys[key]:
            for i in (0,1):
                Player.movement[i] += DIRECTDICT[key][i]*Player.speed

### helper method to get each element of a list and each previous element
def offset(iterable):
    prev = None
    for elem in iterable:
        yield prev, elem
        prev = elem
        
def main(Surf,Player):
       global state
       global points
       game(Player) 
       Surf.fill((255,255,255)) 
       Player.update(Surf) 
       ### in state 1, start "collecting" the player position
       if state == 1:
           points.append(Player.rect.center)
       ### in state 1 and 2, draw the line
       if state in (1, 2):
           for p1, p2 in offset(points):
                if p1 and p2:
                   pg.draw.line(Surf, (255,0,0), p1, p2)
       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) 

在此处输入图像描述

(PS:您的代码/缩进非常混乱,您的问题也不是 100% 清楚,但您仍然应该明白这一点)。

于 2013-07-15T15:04:14.343 回答