def __init__(self, window_width, window_height): game.Game.__init__(self, window_width, window_height) window_bounds = util.Rectangle(0, self.window_height, 0, self.window_width) self.paddle1 = game_objects.VerticalPaddle( util.Vec2D(50, int(self.window_height / 2)), util.Vec2D(0, 0), PongGame.PADDLE_HEIGHT, PongGame.PADDLE_WIDTH, window_bounds, pygame.Color("white"), "paddle1") self.paddle2 = game_objects.VerticalPaddle( util.Vec2D(self.window_width - 50, int(self.window_height / 2)), util.Vec2D(0, 0), PongGame.PADDLE_HEIGHT, PongGame.PADDLE_WIDTH, window_bounds, pygame.Color("white"), "paddle2") self.balls = [] for i in range(0, PongGame.NUM_BALLS): radius = util.RandGen.randint(PongGame.RADIUS_RANGE[0], PongGame.RADIUS_RANGE[1]) position = util.Vec2D( util.RandGen.uniform(0 + radius, self.window_width - radius), util.RandGen.uniform(0 + radius, self.window_height - radius)) velocity = util.random_velocity(PongGame.SPEED_RANGE) color_scheme = game_objects.PongBall.RANDOM identifier = i self.balls += [ game_objects.PongBall(position, velocity, color_scheme, radius, identifier) ] self.collidable_objects += [[self.paddle1, self.paddle2]] self.score1 = 0 self.score2 = 0
def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_m: self.mute = not self.mute elif event.key == pygame.K_SPACE: self.instructions = False elif event.key == pygame.K_1: self.instructions = False self.init_level(1) elif event.key == pygame.K_2: self.instructions = False self.init_level(2) elif event.key == pygame.K_3: self.instructions = False self.init_level(3) elif event.key == pygame.K_4: self.instructions = False self.init_level(4) elif event.type == pygame.MOUSEBUTTONDOWN: #print "Button pressed:", event.dict['button'], "@", event.dict['pos'] button_pressed = event.dict['button'] target = event.dict['pos'] if button_pressed == 1: # Left click targets self.pulling = True self.mouse_origin = util.Vec2D(target[0], target[1]) mouse_end = util.Vec2D(target[0], target[1]) self.launch_velocity = self.mouse_origin - mouse_end self.launch_velocity *= AngryBirds.LAUNCH_SPEED elif button_pressed == 3: # Right click fires pass elif event.type == pygame.MOUSEBUTTONUP: #print "Button released:", event.dict['button'], "@", event.dict['pos'] button_pressed = event.dict['button'] target = event.dict['pos'] if button_pressed == 1: # Left click targets self.firing = True mouse_end = util.Vec2D(target[0], target[1]) self.launch_velocity = self.mouse_origin - mouse_end self.launch_velocity *= AngryBirds.LAUNCH_SPEED #bird.velocity = self.launch_velocity #print self.launch_velocity elif button_pressed == 3: # Right click fires pass elif event.type == pygame.MOUSEMOTION: target = event.dict['pos'] mouse_end = util.Vec2D(target[0], target[1]) self.launch_velocity = self.mouse_origin - mouse_end self.launch_velocity *= AngryBirds.LAUNCH_SPEED
def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_m: self.mute = not self.mute keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT]: self.paddle.set_velocity(util.Vec2D(BrickBreaker.PADDLE_SPEED, 0)) elif keys[pygame.K_LEFT]: self.paddle.set_velocity( util.Vec2D(BrickBreaker.PADDLE_SPEED, 0) * (-1)) else: self.paddle.set_velocity(util.Vec2D(0, 0))
def __init__(self, window_width, window_height): game.Game.__init__(self, window_width, window_height) window_bounds = util.Rectangle(0, self.window_height, 0, self.window_width) self.paddle = game_objects.HorizontalPaddle( util.Vec2D(int(self.window_width / 2), self.window_height - 50), util.Vec2D(0, 0), BrickBreaker.PADDLE_HEIGHT, BrickBreaker.PADDLE_WIDTH, window_bounds, pygame.Color("white"), "paddle") radius = 20 position = util.Vec2D(self.window_width / 2, self.window_height - 100) velocity = util.Vec2D(1, -5) color_scheme = game_objects.PongBall.RANDOM identifier = "ball" self.balls = [ game_objects.PongBall(position, velocity, color_scheme, radius, identifier) ] self.bricks = [] for row in range(0, BrickBreaker.NUM_ROWS): brick_width = self.window_width / BrickBreaker.NUM_BRICKS brick_height = BrickBreaker.ROW_HEIGHT position = util.Vec2D( brick_width / 2, brick_height / 2 + BrickBreaker.TOP_MARGIN + (brick_height * row)) for i in range(0, BrickBreaker.NUM_BRICKS): self.bricks += [ game_objects.Brick(position, brick_height, brick_width, i) ] position += util.Vec2D(brick_width, 0) self.collidable_objects += [[self.paddle]] + [self.bricks] self.lives = 3
def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_m: self.mute = not self.mute keys = pygame.key.get_pressed() if keys[pygame.K_UP]: self.paddle2.set_velocity(util.Vec2D(0, -1 * PongGame.PADDLE_SPEED)) elif keys[pygame.K_DOWN]: self.paddle2.set_velocity(util.Vec2D(0, PongGame.PADDLE_SPEED)) else: self.paddle2.set_velocity(util.Vec2D(0, 0)) if keys[pygame.K_w]: self.paddle1.set_velocity(util.Vec2D(0, -1 * PongGame.PADDLE_SPEED)) elif keys[pygame.K_s]: self.paddle1.set_velocity(util.Vec2D(0, PongGame.PADDLE_SPEED)) else: self.paddle1.set_velocity(util.Vec2D(0, 0))
def __init__(self, window_width, window_height): print "Seed = " + str(util.get_seed()) pygame.init() pygame.mixer.init() self.wood = pygame.mixer.Sound("audio/wood.wav") self.stone = pygame.mixer.Sound("audio/stone.wav") self.ice = pygame.mixer.Sound("audio/ice.wav") self.bleep = pygame.mixer.Sound("audio/bleep.wav") self.moss = pygame.mixer.Sound("audio/moss.wav") self.mute = False self.font = pygame.font.Font(None, 80) self.score = 0 self.currentLevel = 0 self.shootingBird = [] self.birdType = 1 self.running = False self.instructions = True self.win_screen = False self.firing = False self.pulling = False self.mouse_origin = util.Vec2D(0, 0) self.launch_velocity = util.Vec2D(0, 0) self.window_width = window_width self.window_height = window_height self.window = pygame.display.set_mode( (self.window_width, self.window_height)) self.space = pymunk.Space() self.space.gravity = 0, 3 self.space.damping = 1 def collision(arbiter, space, data): if (issubclass(arbiter.shapes[0].body.__class__, Bird) and issubclass(arbiter.shapes[1].body.__class__, Block)): bird = arbiter.shapes[0] block = arbiter.shapes[1] elif (issubclass(arbiter.shapes[0].body.__class__, Block) and issubclass(arbiter.shapes[1].body.__class__, Bird)): bird = arbiter.shapes[1] block = arbiter.shapes[0] else: return True if issubclass(block.body.__class__, Crate): if not self.mute: self.wood.play(0, 400) space.remove(bird, bird.body) space.remove(block, block.body) bird.body.kill() block.body.kill() return False elif issubclass(block.body.__class__, Ice): if not self.mute: self.ice.play(0, 400) space.remove(bird, bird.body) space.remove(block, block.body) bird.body.kill() block.body.kill() return False elif issubclass(block.body.__class__, Sheep): self.score += 100 if not self.mute: self.wood.play(0, 400) space.remove(bird, bird.body) space.remove(block, block.body) bird.body.kill() block.body.kill() return False elif issubclass(block.body.__class__, Stone): if not self.mute: self.stone.play(0, 400) return True elif issubclass(block.body.__class__, Moss): if not self.mute: self.moss.play(0, 400) return True h = self.space.add_default_collision_handler() h.begin = collision self.birds = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.blocks = pygame.sprite.Group() self.slingshots = pygame.sprite.Group() self.slingshot = Slingshot(util.Vec2D(125, self.window_height - 370), 50, "slingshot") self.slingshots.add(self.slingshot) self.levels = Levels(window_width, window_height)