class App: screen_size = (160, 120) margin = 10 @property def objects(self): yield self.paddle def __init__(self): pyxel.init(*App.screen_size) self.background = Background(App.screen_size, App.margin) self.ball = Ball(App.screen_size, App.margin) self.paddle = Paddle(App.screen_size, App.margin) pyxel.run(self.update, self.draw) def update(self): if pyxel.btnp(pyxel.KEY_Q): pyxel.quit() self.ball.update() for obj in self.objects: obj.update() if obj.ball_collide(self.ball) and obj == 'rectangle': obj.alive = False def draw(self): self.background.draw() self.ball.draw() for obj in self.objects: obj.draw()
class Game(Animation): def __init__(self, number, x_range, y_range, width, height, fps=30, background="black", gravity=9.81): super().__init__(width, height, fps, background) allowed_colours = list(self.colours.keys()) allowed_colours.remove('black') self.balls = [ Ball(1, self.colours[choice(allowed_colours)], (randint(40, self.width - 100), 60), id=i, radius=3) for i in range(number) ] self.centre_ball = Ball(2, self.colours["red"], (self.width / 2, self.height / 2), id=69, radius=10) def event_logic(self, event): if event.type == pygame.QUIT: exit() elif event.type == pygame.MOUSEBUTTONDOWN: for ball in self.balls: ball.apply_force([4, 2]) def game_logic(self): selected = None moving = False gravity = [0, 3] for ball in self.balls: #ball.apply_gravity(gravity) #ball.apply_fluid_resistance(1, 0.0001) ball.apply_force([0.1, 0]) force = ball.apply_gravitation_attraction(1, self.centre_ball.mass, self.centre_ball.pos) #self.centre_ball.apply_gravitation_attraction(0.0001, ball.mass, ball.pos) print(force) #if ball.pos[1] == self.height - ball.radius: # ball.apply_friction(0.1, gravity) ball.update() #self.centre_ball.update() #trimmed = trim([0+ball.radius, 0+ball.radius], # [self.width-ball.radius, self.height-ball.radius], # ball.pos) #ball.pos = trimmed[0] #if trimmed[1][0]: # ball.velocity[0] *= -1 #if trimmed[1][1]: # ball.velocity[1] *= -1 print( f"ID: {ball.id}\n\tVelocity: {ball.velocity}\n\tAcceleration: {ball.acceleration}\n\t(x,y): {ball.pos}\n\tmass: {ball.mass}\n\tTouching {ball.pos[1] == self.height - ball.radius}" ) ball.acceleration = np.multiply(ball.acceleration, 0) def display_logic(self): for ball in self.balls: ball.draw(self.screen) self.centre_ball.draw(self.screen)
for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(cl_off_black) screen.blit(update_fps(), (16, 5)) if ball.is_colliding_with(left_paddle) or left_paddle.is_colliding_with( ball): print("Colliding left") ball.bounce_x() keys = pygame.key.get_pressed() if keys[pygame.K_w]: left_paddle.move(dy=-PADDLE_SPEED) if keys[pygame.K_s]: left_paddle.move(dy=PADDLE_SPEED) if keys[pygame.K_UP]: left_paddle.move(dy=-PADDLE_SPEED) if keys[pygame.K_DOWN]: left_paddle.move(dy=PADDLE_SPEED) ball.update() left_paddle.draw(on=screen) right_paddle.draw(on=screen) ball.draw(on=screen) # --- Limit to 60 frames per second clock.tick(FPS) pygame.display.flip()