1
所以我是新来的pygame/python /编码和第一次使用类。我已经做出了一个工作pygame程序的球弹跳和重力加速。但是,现在我使用了课堂,球只似乎以恒定的速度移动。请告诉我我做错了什么,以及如何改变我的代码。提前致谢!为什么我的pygame球在使用类时不会加速?
import pygame, sys
pygame.init()
red = (255,0,0)
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
pygame.mouse.set_visible(0)
clock = pygame.time.Clock()
displaySize = (800,600)
screen = pygame.display.set_mode(displaySize)
g = 30
dt = 0.05
class ball:
def __init__(self, x, y, vx, vy, r,ax,ay, color):
dt = 0.05
Cd = 0.01
m = 5
self.ay = ay
self.ax = ax
self.x = x
self.y = y
self.r = r
self.color = color
self.vx = vx
self.vy = vx
def update(self, x, y):
x, y = physics()
pygame.draw.circle(screen, self.color, (int(round(x)),int(round(y))), self.r)
pygame.display.update()
def physics(self):
self.Dy = Cd*self.vy*abs(self.vy)
self.Dx = Cd*self.vx*abs(self.vx)
self.Fy = self.m*g - self.Dy
self.Fx = -self.Dx
self.ay = self.Fy/m
self.ax = self.Fx/m
self.vy += self.ay*dt
self.vx += self.ax*dt
self.x +=self.vx*dt
self.y +=self.vy*dt
return self.x, self.y
one = ball(100,100,0,0,0,0,30,red)
while 1:
clock.tick(30)
screen.fill(blue)
one.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
加速代码在哪里?你应该减少到[mcve]。这里的一半代码似乎是不相关的。 – Carcigenicate