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