Reputation: 129
So I have a ball class and two instances of that class. Within the class is a directions method that besaically accelerates the ball up left or right, depending on the the arrow keys pressed. Now my problem is, only the first instance ball can be controlled by the arrow keys. How is it possible that two instances of the same class behave differently? What should I change so that I can control both balls? I preferably want to keep the controls of the balls within the class.
here is my code:
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)
def directions(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.ay +=-1000
if event.key == pygame.K_LEFT:
self.ax += -1000
if event.key == pygame.K_RIGHT:
self.ax += 1000
def physics(self):
self.Dy = Cd*self.vy*abs(self.vy)
self.Dx = Cd*self.vx*abs(self.vx)
self.Fy = m*g - self.Dy
self.Fx = -self.Dx
self.ay = self.Fy/m
self.ax = self.Fx/m
self.directions()
self.vy += self.ay*dt
self.vx += self.ax*dt
self.x +=self.vx*dt
self.y +=self.vy*dt
if self.x <= self.r:
self.x =self.r
self.vx *= -0.7
if self.x >= displaySize[0]- self.r:
self.x = displaySize[0] - self.r
self.vx *= -0.7
if self.y <= self.r:
self.y = self.r
self.vy *= -0.7
if self.y >= displaySize[1] - self.r:
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)
ballOne.update()
ballTwo.update()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
Upvotes: 0
Views: 46
Reputation: 8587
Not really familiar with pygame
, but from the documentation I'd say the problem is that your first ball empties the event queue when you call update()
.
The queue is then empty when you call update()
on the second ball, so it does not move as there are no events to tell it to move.
Same issue with the exit condition I'd say, there is never an appropriate event in the queue when you reach that point in your code.
A quick fix would be to do all the event processing in your main loop (where you check for exit condition), and change your balls so that you merely inform them about player input.
def playerinput(self, key):
if key == pygame.K_UP:
self.ay += -1000
...
In your main loop, assuming all your balls are in a list playerobjects
:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
for player in playerobjects:
player.playerinput(event.key)
Upvotes: 2