gpt4 book ai didi

python - 根据pygame中的球运动画一条线

转载 作者:行者123 更新时间:2023-12-01 05:40:32 30 4
gpt4 key购买 nike

如何让 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)

最佳答案

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

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)

enter image description here

(P.S.:你的代码/缩进非常困惑,你的问题也不是100%清楚,但你应该明白)。

关于python - 根据pygame中的球运动画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17656860/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com