【发布时间】:2017-08-05 11:12:11
【问题描述】:
我用两个实例创建了一个class Ball:ballOne 和ballTwo。当我调用ballTwo.update() 然后ballOne.update() 时,最后调用的球有时会在某些帧上消失,就像它有时会闪烁。有人可以帮忙吗?
import pygame, sys
pygame.init()
red = (255,0,0)
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
green = (0,255,0)
pygame.mouse.set_visible(0)
clock = pygame.time.Clock()
displaySize = (800,600)
screen = pygame.display.set_mode(displaySize)
g = 50
dt = 0.05
Cd = 0.01
m = 5
class ball:
def __init__(self, x, y, vx, vy, r,ax,ay, color):
self.Fx = 0
self.Fy = 0
self.Dx = 0
self.Dy = 0
self.ay = ay
self.ax = ax
self.x = x
self.y = y
self.r = r
self.color = color
self.vx = vx
self.vy = vy
def update(self):
self.x, self.y = self.physics()
pygame.draw.circle(screen, self.color, (int(round(self.x)),int(round(self.y))), self.r)
pygame.display.update()
def physics(self):
self.x +=self.vx*dt
self.y +=self.vy*dt
self.vy += self.ay*dt
self.vx += self.ax*dt
self.ay = self.Fy/m
self.ax = self.Fx/m
self.Fy = m*g - self.Dy
self.Fx = -self.Dx
self.Dy = Cd*self.vy*abs(self.vy)
self.Dx = Cd*self.vx*abs(self.vx)
if self.x <= self.r:
self.vx *= -0.7
if self.x >= displaySize[0]- self.r:
self.vx *= -0.7
if self.y <= self.r:
self.vy *= -0.7
if self.y >= displaySize[1] - self.r:
self.vy *= -0.7
return self.x, self.y
ballOne = ball(100,100,50,-100,30,0,0,red)
ballTwo = ball(500,500,-75,0,45,0,0,green)
while 1:
clock.tick(60)
screen.fill(blue)
ballTwo.update()
ballOne.update()
【问题讨论】:
-
你的代码很奇怪:缺少方法
def和update()缺少self参数,class应该是小写的。 -
希望现在更好
标签: python python-2.7 class pygame instance