class IntroScene(Scene): def __init__(self, app): self.app = app self.screen = app.screen super().__init__('IntroScene') self.count_balls = 30 self.ball_list = [] self.ball = Ball(random.randrange(40, 500), random.randrange(50, 400), random.randint(5, 60), random.randrange(-2, 3), random.randrange(-2, 3), (random.randint(0, 255), random.randint( 0, 255), random.randint(0, 255))) def start(self): self.ball.add_balls(self.ball_list) print('se inicia: ', self.name) def process_events(self, event): if event.type == pygame.KEYDOWN: self.app.change_scene('play') print('se presiono una tecla') def update(self): for ball in self.ball_list: ball.update_balls() def draw(self): self.screen.fill((0, 0, 0)) self.ball.draw_ball(self.ball_list, self.screen) def exit(self): print('termina: ', self.name)
def __init__(self, game, name=None, num_boids=8, w=10): Scene.__init__(self, game, name) self.color = 'black' for n in range(num_boids): self.append(Boid(self.game, w)) for n in range(3): self.append(Ball(self.game, 30, 10, 'green')) for n in range(1): self.append(Ball(self.game, 30, 20, 'red'))
def __init__(self, app): self.app = app self.screen = app.screen super().__init__('IntroScene') self.count_balls = 30 self.ball_list = [] self.ball = Ball(random.randrange(40, 500), random.randrange(50, 400), random.randint(5, 60), random.randrange(-2, 3), random.randrange(-2, 3), (random.randint(0, 255), random.randint( 0, 255), random.randint(0, 255)))
def test_act(self): ai2 = AI2() ai2.train() # tests b = [(100, 100), (100, 100), (100, 100), (100, 100), (100, 100)] s = [(1, 1), (1, 1), (1, 1), (1, -1), (-1, -1)] r = [(200, 50), (200, 100), (200, 50), (200, 50), (200, 50)] a = [DOWN, NONE, DOWN, DOWN, NONE] # test 1 -> DOWN ball = Ball() rb = Robot() for i in range(len(b)): ball.set_pos(b[i][0], b[i][1]) ball.speed = [s[i][0], s[i][1]] rb.set_pos(r[i][0], r[i][1]) act = ai2.action(ball, rb) assert act == a[i], str(i) + ' not passed'
def __init__(self): self.bg = pygame.image.load("images/bg.jpg") self.bg = pygame.transform.scale(self.bg, (SCREEN_WIDTH, SCREEN_HEIGHT)) self.bgrect = self.bg.get_rect() self.ball = Ball() self.human = Human() self.robot = Robot() self.goal = Goal(0) self.win = Win(0) self.lose = Lose(0) self.menu = Menu() self.mouse = (0, 0) self.sounds = Sounds() self.sounds.music() self.status = Game.MENU self.reset()
def process(self): if time.time() - self.last_time >= 1: self.balls.append(Ball(BASE_RADIUS)) self.last_time = time.time() for ball in self.balls: if ball.collision( (self.hero.x, self.hero.y, self.hero.width, self.hero.height)): self.score += 1 self.balls.remove(ball) del ball #удаляем из памяти continue if ball.collision((0, HEIGHT + ball.radius * 2, WIDTH, 0)): self.balls.remove(ball) del ball continue ball.move()
def pongGameLoop(): pygame.time.wait(1000) gameExit = False scorep1 = 0 scorep2 = 0 paddle1 = Paddle(white, 10, 100) paddle1.rect.x = 20 paddle1.rect.y = 200 paddle2 = Paddle(white, 10, 100) paddle2.rect.x = 770 paddle2.rect.y = 200 ball = Ball(white, 10, 10) ball.rect.x = 345 ball.rect.y = 195 all_stuff = pygame.sprite.Group() all_stuff.add(paddle1) all_stuff.add(paddle2) all_stuff.add(ball) clock = pygame.time.Clock() while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: gameExit = True keys = pygame.key.get_pressed() if keys[pygame.K_w]: paddle1.up(5) if keys[pygame.K_s]: paddle1.down(5) if keys[pygame.K_UP]: paddle2.up(5) if keys[pygame.K_DOWN]: paddle2.down(5) all_stuff.update() if ball.rect.x >= 790: scorep1 += 1 if scorep1 >= 10 and scorep1 > scorep2: font = pygame.font.Font(None, 75) text = font.render("Pl wins!", 1, white) screen.blit(text, (175, 200)) pygame.display.flip() pygame.time.wait(10000) scorep1 = 0 scorep2 = 0 ball.velocity[0] = -ball.velocity[0] if ball.rect.x <= 0: scorep2 += 1 if scorep2 > 10 and scorep1 < scorep2: font = pygame.font.Font(None, 75) text = font.render("P2 wins!", 1, white) screen.blit(text, (400, 200)) pygame.display.flip() pygame.time.wait(10000) scorep2 = 0 scorep1 = 0 ball.velocity[0] = -ball.velocity[0] if ball.rect.y > 590: ball.velocity[1] = -ball.velocity[1] if ball.rect.y < 0: ball.velocity[1] = -ball.velocity[1] if pygame.sprite.collide_mask( ball, paddle1) or pygame.sprite.collide_mask(ball, paddle2): ball.bounce() screen.fill(black) pygame.draw.line(screen, white, [400, 0], [400, 600], 5) all_stuff.draw(screen) font = pygame.font.Font(None, 75) text = font.render(str(scorep1), 1, white) screen.blit(text, (275, 10)) font = pygame.font.Font(None, 75) text = font.render(str(scorep2), 1, white) screen.blit(text, (445, 10)) pygame.display.flip() clock.tick(60) pygame.quit()
class Game: FPS = 60 BLACK = (0, 0, 0) WHITE = (255, 255, 255) PLAY = 0 PAUSE = 1 GOAL = 2 WIN = 3 LOSE = 4 MENU = 5 EXIT = 6 GOAL_TIME = 2 * FPS # 2 sec WIN_TIME = 3 * FPS LOSE_TIME = 3 * FPS MAX_SCORE = 3 def __init__(self): self.bg = pygame.image.load("images/bg.jpg") self.bg = pygame.transform.scale(self.bg, (SCREEN_WIDTH, SCREEN_HEIGHT)) self.bgrect = self.bg.get_rect() self.ball = Ball() self.human = Human() self.robot = Robot() self.goal = Goal(0) self.win = Win(0) self.lose = Lose(0) self.menu = Menu() self.mouse = (0, 0) self.sounds = Sounds() self.sounds.music() self.status = Game.MENU self.reset() def reset(self): self.ball.set_pos(0.3 * SCREEN_WIDTH, 0.3 * SCREEN_HEIGHT) self.human.set_pos(0.05 * SCREEN_WIDTH, SCREEN_HEIGHT / 2 - self.human.height() / 2) self.robot.set_pos(0.8 * SCREEN_WIDTH, SCREEN_HEIGHT / 2 - self.robot.height() / 2) self.human.score = 0 self.robot.score = 0 self.last_detect = 0 self._key_up = False self._key_down = False self.status = Game.MENU def goal_start(self): self.status = Game.GOAL self.goal = Goal(Game.GOAL_TIME) def win_start(self): self.status = Game.WIN self.win = Win(Game.WIN_TIME) def lose_start(self): self.status = Game.LOSE self.lose = Lose(Game.LOSE_TIME) def update(self): if self.status == Game.MENU: if self.menu.test(self.mouse[0], self.mouse[1]) == Menu.MENU_PLAY: # menu -> play self.reset() self.robot.level = 2 self.status = Game.PLAY self.sounds.click() elif self.menu.test( self.mouse[0], self.mouse[1]) == Menu.MENU_EXIT: # menu -> exit self.status = Game.EXIT self.sounds.click() # goal -> continue elif self.status == Game.GOAL and self.goal.update() == 0: if self.robot.score >= Game.MAX_SCORE: # goal -> end game self.lose_start() self.sounds.lose() elif self.human.score >= Game.MAX_SCORE: # goal -> end game self.win_start() self.sounds.win() else: self.status = Game.PLAY # goal -> continue play # process win/lose elif self.status == Game.WIN: if self.win.update() == 0: self.status = Game.MENU # win -> menu elif self.status == Game.LOSE: if self.lose.update() == 0: self.status = Game.MENU # lose -> menu elif self.status == Game.PLAY: # calc next self.ball.move() if self._key_up and self.human.top() > SCREEN_HEIGHT / 10: self.human.go_up() elif self._key_down and self.human.bottom( ) < SCREEN_HEIGHT * 9 / 10: self.human.go_down() #self.robot.move() # goal detect if self.ball.left() < 0: self.robot.score += 1 self.ball.reset(SCREEN_WIDTH, SCREEN_HEIGHT) self.goal_start() self.status = Game.GOAL self.sounds.goal() # goal detect if self.ball.right() > SCREEN_WIDTH: self.human.score += 1 self.ball.reset(SCREEN_WIDTH, SCREEN_HEIGHT) self.goal_start() self.status = Game.GOAL self.sounds.goal() # wall collision detect if self.ball.top() < 0 or self.ball.bottom() > SCREEN_HEIGHT: self.ball.toggleY() self.sounds.ball() # player collision detect if pygame.time.get_ticks() - self.last_detect > 500: if self.ball.collision( self.human.rect()) or self.ball.collision( self.robot.rect()): self.last_detect = pygame.time.get_ticks() self.sounds.ball() # robot update if self.robot.level == 0 and self.robot.top( ) < SCREEN_HEIGHT / 10 or self.robot.bottom( ) > SCREEN_HEIGHT * 9 / 10: self.robot.toggle_y() elif self.robot.level >= 1: self.robot.ai(self.ball) self.mouse = (0, 0) def draw_score(self, screen): text = str(self.human.score) + ' : ' + str(self.robot.score) font = pygame.font.Font('freesansbold.ttf', 30) text_surface = font.render(text, True, Game.BLACK) text_rect = text_surface.get_rect() text_rect.center = ((SCREEN_WIDTH / 2), (0.1 * SCREEN_HEIGHT)) screen.blit(text_surface, text_rect) def draw(self, screen): screen.fill(Game.BLACK) screen.blit(self.bg, self.bgrect) self.ball.draw(screen) self.human.draw(screen) self.robot.draw(screen) self.draw_score(screen) self.goal.draw(screen) if self.status == Game.MENU: self.menu.draw(screen) elif self.status == Game.WIN: self.win.draw(screen) elif self.status == Game.LOSE: self.lose.draw(screen) def key_up(self, status): self._key_up = status def key_down(self, status): self._key_down = status