Python Pygame,如何打印不断变化的变量
这是一个抛射模拟器,我希望用户将鼠标放置在离球一定距离的位置,当他们单击鼠标时,根据角度和离球的距离进行射击,当我放置一个文本来检测角度时,它仅在我单击后显示,并保持上次单击时的值,我是否可以让它不断变化,以便用户在单击和启动之前能够清楚地知道角度。这里是一个游戏的概述,请帮助我即时通讯新Python Pygame,如何打印不断变化的变量,python,Python,这是一个抛射模拟器,我希望用户将鼠标放置在离球一定距离的位置,当他们单击鼠标时,根据角度和离球的距离进行射击,当我放置一个文本来检测角度时,它仅在我单击后显示,并保持上次单击时的值,我是否可以让它不断变化,以便用户在单击和启动之前能够清楚地知道角度。这里是一个游戏的概述,请帮助我即时通讯新 class BALL (object): def __init__(self, x, y, radius, color): self.x = x self.y = y self
class BALL (object):
def __init__(self, x, y, radius, color):
self.x = x
self.y = y
self.radius = radius
self.color = color
def draw(self, win):
pygame.draw.circle(screen, (0,0,0), (self.x,self.y), self.radius) #outline of the ball
pygame.draw.circle(screen, self.color, (self.x,self.y), self.radius-1) #the ball
@staticmethod
def BALLPath (startx, starty, SPEED, ANGLE, TIME):
Vx = math.cos(ANGLE)*SPEED
Vy = math.sin(ANGLE)*SPEED
Sx = Vx*TIME
Sy = (Vy*TIME) + ((-9.81*(TIME)**2)/2)
NEWx = round(Sx + startx)
NEWy = round(starty - Sy)
return(NEWx, NEWy)
BasketBall = BALL(300, 494, 5, (255,255,255))
def FINDA(pos): #finds angle using quadrants method.
sX = BasketBall.x
sY = BasketBall.y
try:
ANGLE = math.atan((sY - pos[1]) / (sX - pos[0]))
except:
ANGLE = math.pi / 2
if pos[1] < sY and pos[0] >sX:
ANGLE = abs (ANGLE)
elif pos[1]<sY and pos[0]<sX:
ANGLE = math.pi - ANGLE
elif pos[1]>sY and pos[0] <sX:
ANGLE = math.pi + abs(ANGLE)
elif pos[1]>sY and pos[0] >sX:
ANGLE = (math.pi*2) - ANGLE
return ANGLE
def TEXT(text, font, color, surface, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def PROJECTILE(): #projectile game loop
FONTH = pygame.font.SysFont(None, 25)
X = 0
Y = 0
TIME = 0
SPEED = 0
ANGLE = 0
SHOOT = False
RUN = True
degANGLE = 0
while RUN:
if SHOOT:
if BasketBall.y < 500 - BasketBall.radius:
TIME += 0.075
POSITION = BALL.BALLPath(X,Y,SPEED,ANGLE,TIME)
BasketBall.x = POSITION[0]
BasketBall.y = POSITION[1]
else:
SHOOT = False
BasketBall.y = 494
pos = pygame.mouse.get_pos()
line = [(BasketBall.x, BasketBall.y), pos]
screen.fill((0,0,0))
BasketBall.draw(screen)
msg = "Angle: {0}".format(degANGLE)
TEXTANGLE = font.render(msg, 1, (255, 255, 255))
screen.blit(TEXTANGLE, (10, 10))
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
if event.type == pygame.MOUSEBUTTONDOWN:
SHOOT = True
X = BasketBall.x
Y = BasketBall.y
TIME = 0
SPEED = math.sqrt((line[1][1] - line[0][1])**2 + (line[1][0]-line[0][0])**2)/6
ANGLE = FINDA(pos)
degANGLE = ANGLE*57.296
pygame.display.update()
Clock.tick(60)
类球(对象):
定义初始值(自、x、y、半径、颜色):
self.x=x
self.y=y
自半径=半径
self.color=颜色
def抽签(自我,赢):
pygame.draw.circle(屏幕,(0,0,0),(self.x,self.y),self.radius)#球的轮廓
pygame.draw.circle(屏幕,self.color,(self.x,self.y),self.radius-1)#球
@静力学方法
def球径(startx、starty、速度、角度、时间):
Vx=数学cos(角度)*速度
Vy=数学正弦(角度)*速度
Sx=Vx*时间
Sy=(Vy*时间)+(-9.81*(时间)**2)/2)
NEWx=圆形(Sx+startx)
NEWy=圆形(星形-Sy)
返回(NEWx,NEWy)
篮球=球(300494,5,(255255))
def FINDA(位置):#使用象限方法查找角度。
sX=篮球.x
sY=篮球
尝试:
角度=数学坐标((sY-pos[1])/(sX-pos[0]))
除:
角度=math.pi/2
如果位置[1]sX:
角度=abs(角度)
elif位置[1]sX:
角度=(数学pi*2)-角度
返回角
def文本(文本、字体、颜色、表面、x、y):
textobj=font.render(文本,1,颜色)
textrect=textobj.get_rect()
textrect.topleft=(x,y)
surface.blit(textobj,textrect)
def sparket():#抛射游戏循环
FONTH=pygame.font.SysFont(无,25)
X=0
Y=0
时间=0
速度=0
角度=0
射击=假
运行=真
角度=0
运行时:
如果拍摄:
如果BasketBall.y<500-BasketBall.radius:
时间+=0.075
位置=球。球径(X,Y,速度,角度,时间)
BasketBall.x=位置[0]
BasketBall.y=位置[1]
其他:
射击=假
篮球,y=494
pos=pygame.mouse.get_pos()
行=[(BasketBall.x,BasketBall.y),位置]
屏幕填充((0,0,0))
篮球。抽签(屏幕)
msg=“角度:{0}”。格式(角度)
TEXTANGLE=font.render(消息,1,(255,255,255))
屏幕。blit(文本角度,(10,10))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.MOUSEBUTTONDOWN:
射击=真
X=篮球
Y=篮球
时间=0
速度=数学sqrt((行[1][1]-行[0][1])**2+(行[1][0]-行[0][0])**2)/6
角度=FINDA(位置)
角度=角度*57.296
pygame.display.update()
时钟滴答(60)
将FINDA
和degANGLE
从事件检查中移出,并在每次绘制屏幕时计算它。