def __init__(self, stage, pos_x, pos_y, circle_mass, radius, circle_elasticity, color): Ball.__init__(self, stage, pos_x, pos_y, circle_mass, radius, circle_elasticity, color) self.circle.collision_type = 1 self.point_value = 0 self.gun_bullet = pygame.image.load(self.stage.graphics_path + "gunbullet.png").convert_alpha() self.cannon_bullet = pygame.image.load(self.stage.graphics_path + "cannonbullet.png").convert_alpha()
def __init__(self, x = 0.0, y = 0.0): Ball.__init__(self, x, y) if Bar.image is None: # This is the first time loading the sprite, # so we need to load the image data Bar.image = pg.image.load(os.path.join("data","bar.png")).convert() Bar.image.set_colorkey(0) self.image = Bar.image (self.rect.w, self.rect.h) = (self.image.get_rect().w, self.image.get_rect().h) self.infiniteMass = True self.moveable = False
def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) Ball.__init__(self, x, y, 25, pygame.color.Color("Red")) self.image = pygame.transform.smoothscale(pygame.image.load("images/donkey_kong.png").convert_alpha(), (self.radius*2, self.radius*2)) self.vel = Vector(0, 0) self.mass = 1 self.update_rect() self.colliding = False self.lives = 5 self.angle = -90 self.power = 50
def __init__(self, x, y, thrower): pygame.sprite.Sprite.__init__(self) Ball.__init__(self, x, y, 10, pygame.color.Color("Yellow")) self.init_image = pygame.transform.smoothscale( pygame.image.load("images/banana.png").convert_alpha(), (self.radius * 2, self.radius * 2)) self.image = self.init_image self.vel = Vector(0, 0) self.mass = 10 self.update_rect() self.hit = False self.colliding = False self.angle = 0 self.thrower = thrower
def __init__(self, x = 0.0, y = 0.0): Ball.__init__(self) if Blobble.image is None: # This is the first time loading the sprite, # so we need to load the image data Blobble.image = pg.image.load(os.path.join("data","blobble.png")).convert() Blobble.image.set_colorkey(0) self.image = Blobble.image self.rect = self.image.get_rect() #self.radius = self.rect.w / 2.0 self._setPosition(Vector(x, y)) self.infiniteMass = False self.moveable = True self.xAccel = 0.0 self.doJump = False
def __init__(self,x,y): Ball.__init__(self, x, y) self.randomize_color() self.set_speed(30)
def __init__(self,x,y): Ball.__init__(self,x,y) self._color = "BROWN" self.set_dimension(10, 10) self.time_count = 0 self.set_speed(6)
def __init__(self, x,y): Ball.__init__(self, x, y) self._speed = 5 self._angle = random.random()*math.pi*2 self._image = PhotoImage(file="ufo.gif")
def __init__(self, x, y): Ball.__init__(self, x, y) self._color = 'pink'
def __init__(self, x, y, parent): Ball.__init__(self, x, y) self.next = parent self.set_angle(parent.get_angle()) self.color = 'Green'
def __init__(self, x, y): Ball.__init__(self, x, y)
def __init__(self, stage, swing, pos_x, pos_y, circle_mass, radius, circle_elasticity, color): Ball.__init__(self, stage, pos_x, pos_y, circle_mass, radius, circle_elasticity, color) self.swing = swing self.detached = False
def __init__(self, x, y, width, height, angle, speed): Ball.__init__(self, x, y, width, height, angle, speed) self.vision = 50 self.randomize_angle()
def __init__(self, x, y): Ball.__init__(self,x,y) self.randomize_angle()
def __init__(self, x, y): self._r = random.randint(5, 15) Ball.__init__(self, x, y) self.count = 0 self.color = random.choice(Special.colors)
class Breakout: """Overall class to manage game assets and behaviors.""" def __init__(self): """Initialize the game, and create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) self.settings.screen_width = self.screen.get_rect().width self.settings.screen_height = self.screen.get_rect().height pygame.display.set_caption("Breakout") self.stats = GameStats(self) self.sb = Scoreboard(self) self.paddle = Paddle(self) self.ball = Ball(self) self.all_sprites_list = pygame.sprite.Group() self.blocks = pygame.sprite.Group() self.play_button = Button(self, "Play") self.games_ran = 0 def run_game(self): """Start the main loop for the game.""" while True: clock.tick(60) self._check_events() if self.stats.game_active: self.paddle.update() self.paddle_collision() self.ball.update() self.check_ball_block_collision() self.check_ball_bottom() self._update_screen() def _check_events(self): """Respond to keypresses and mouse events.""" for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() self._check_play_button(mouse_pos) def _check_keydown_events(self, event): """Respond to keypresses.""" # Exit game if 'ESCAPE' key is pressed if event.key == pygame.K_ESCAPE: sys.exit() def _check_play_button(self, mouse_pos): """Start a new game when the player clicks Play.""" button_clicked = self.play_button.rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: # Reset the game settings. self.games_ran += 1 self.settings.initialize_dynamic_settings() # Reset the game statistics. self.stats.reset_stats() if self.games_ran == 1: # Fixes bug where the first ran game had an extra life self.stats.lives_left = 2 self.stats.game_active = True self.sb.prep_score() self.sb.prep_level() self.sb.prep_balls() # Create a new row and center the ball self.create_row() self.ball.center_ball() # Hide the mouse cursor. pygame.mouse.set_visible(False) def paddle_collision(self): """Formulas to make the ball bounce off the paddle in a predictable fashion.""" if self.ball.rect.colliderect(self.paddle): diff = (self.paddle.rect.x + self.settings.paddle_width / 2) - ( self.ball.rect.x + self.settings.ball_radius / 2) self.ball.rect.y = self.screen.get_rect( ).height - self.settings.paddle_height - self.settings.ball_radius - 1 self.ball.bounce(diff) def create_row(self): """Create the row of blocks.""" for i in range(14): block = Block(LIGHT_BLUE, 105, 30) block.rect.x = 10 + i * 110 block.rect.y = 60 self.all_sprites_list.add(block) self.blocks.add(block) for i in range(14): block = Block(RED, 105, 30) block.rect.x = 10 + i * 110 block.rect.y = 100 self.all_sprites_list.add(block) self.blocks.add(block) for i in range(14): block = Block(ORANGE, 105, 30) block.rect.x = 10 + i * 110 block.rect.y = 140 self.all_sprites_list.add(block) self.blocks.add(block) for i in range(14): block = Block(LIME_GREEN, 105, 30) block.rect.x = 10 + i * 110 block.rect.y = 180 self.all_sprites_list.add(block) self.blocks.add(block) for i in range(14): block = Block(DARK_BLUE, 105, 30) block.rect.x = 10 + i * 110 block.rect.y = 220 self.all_sprites_list.add(block) self.blocks.add(block) def check_ball_block_collision(self): """Respond to ball-block collisions.""" for block in self.blocks: if self.ball.rect.colliderect(block): self.ball.bounce(0) self.blocks.remove(block) self.stats.score += self.settings.block_points self.sb.prep_score() self.sb.check_high_score() # Change ball speed in correlation with block color if block.rect.y == 220: self.settings.ball_speed = 12 elif block.rect.y == 180: self.settings.ball_speed = 14 elif block.rect.y == 140: self.settings.ball_speed = 16 elif block.rect.y == 100: self.settings.ball_speed = 18 elif block.rect.y == 60: self.settings.ball_speed = 20 if not self.blocks: # Remake game and make it a new level self.stats.level += 1 self.sb.prep_level() self.sb.prep_balls() self.ball.center_ball() sleep(1) self.ball.__init__(self) self.settings.ball_speed = 10 self.create_row() self.ball.bounce(0) def check_ball_bottom(self): """Check if any ball have reached the bottom of the screen.""" screen_rect = self.screen.get_rect() if self.ball.rect.bottom >= screen_rect.bottom: # Treat this the same as if the ship got hit. self.ball_lost() def ball_lost(self): """Respond to the ball hitting the bottom of the screen.""" if self.stats.lives_left > 0: self.stats.lives_left -= 1 self.sb.prep_balls() self.ball.center_ball() sleep(1) self.ball.__init__(self) self.settings.ball_speed = 10 else: # End game if no lives are left self.stats.game_active = False self.blocks.empty() self.settings.ball_speed = 10 pygame.mouse.set_visible(True) def _update_screen(self): """Update images on the screen and flip to the updated screen.""" self.screen.fill(self.settings.bg_color) self.paddle.blitme() self.ball.blitme() self.blocks.draw(self.screen) self.sb.show_score() if not self.stats.game_active: self.play_button.draw_button() pygame.display.flip()
def main_loop(): start = False game_exit = False game_over = False player1 = Player(1, white, screen_width, screen_height) player2 = Player(2, white, screen_width, screen_height) ball = Ball(Direction.No, white, screen_width, screen_height) divider = Divider(white, screen_width) while not game_exit: for event in pygame.event.get(): if event.type == QUIT: game_exit = True # Escape game when escape key is pressed if event.type == KEYDOWN: if event.key == K_ESCAPE: game_exit = True # Move players with arrow keys and WASD if start: if event.key == K_w: player1.move_up() if event.key == K_s: player1.move_down() if event.key == K_UP: player2.move_up() if event.key == K_DOWN: player2.move_down() # Press Space to start game if event.key == K_SPACE: if not start: start = True ball.__init__(Direction.Left, white, screen_width, screen_height) if game_over: main_loop() # Stop moving player if arrows or WASD is up if event.type == KEYUP: if event.key == K_w or event.key == K_s: player1.stop() if event.key == K_UP or event.key == K_DOWN: player2.stop() # Fill black background game_surface.fill(black) # Render game objects and UI if start: ball.draw(game_surface) divider.draw(game_surface, screen_height) player1.draw(game_surface, screen_height) player2.draw(game_surface, screen_height) if not start and not game_over: game_surface.blit(start_text, [240, 200]) display_score(player1) display_score(player2) # Check collisions if not game_over: if ball.check_collision_player( player1) or ball.check_collision_player(player2): ball.change_direction_horizontal() if ball.check_collision_wall(screen_height): ball.change_direction_vertical() if ball.check_score(player1, player2, screen_width) == player1: ball.__init__(Direction.Left, white, screen_width, screen_height) player1.add_score() elif ball.check_score(player1, player2, screen_width) == player2: ball.__init__(Direction.Right, white, screen_width, screen_height) player2.add_score() # Handle game winning if winner(player1, player2, ball) != None: player_win(winner(player1, player2, ball), ball) start = False game_over = True # Render game at 60 fps clock.tick(60) pygame.display.update() quit_game()
def __init__(self, x, y, r, color=(255, 0, 0)): """Initialize the game object.""" Ball.__init__(self, (x, y), r) self.movement = [choice([-1.0, -0.5, 0.5, 1.0]) for _ in [1, 2]] self.__color = color
deadblocks = pygame.sprite.spritecollide(ball, blocks, True) deadmonsters = pygame.sprite.spritecollide(ball, monsters, True) destroychest = pygame.sprite.spritecollide(ball, chests, True) takecoin = pygame.sprite.spritecollide(player, bonuses, True) if len(deadblocks) > 0: ball.bounce(0) score += 10 if len(deadmonsters) > 0: score += 500 if len(monsters) == 0: game_over = True if len(takecoin) > 0: KK += 1 Ball.width = 30 Ball.height = 30 Ball.speed = 7.5 ball.__init__(KK) if len(destroychest) > 0: ball.bounce(0) x, y = ball.x, ball.y - 30 bon = Coin(x, y, 15) allsprites.add(bon) bonuses.add(bon) score += 20 allsprites.draw(screen) pygame.display.flip() pygame.quit()
sc.update() #time.sleep(0.001) if ball.distance(paddle_one.pos()) < 40 or ball.distance( paddle_two.pos()) < 40: ball.move(1) #prevents ball getting 'stuck' to paddle ball.move(0) else: ball.move(0) #if ball y cor different to comp y cor, move to coords if ball.ycor() > paddle_two.ycor() and ball.xcor() > 200: paddle_two.up() elif ball.ycor() < paddle_two.ycor() and ball.xcor() > 200: paddle_two.down() #if ball goes out left add one to comp score and replace ball and vice versa if ball.xcor() < -400: scoreboard.increase_score_two() ball.color("black") ball.__init__() elif ball.xcor() > 400: scoreboard.increase_score_one() ball.color("black") ball.__init__() #if a player exceeds score limit end game if scoreboard.score_one > 9 or scoreboard.score_two > 9: scoreboard.game_over() game_on = False sc.exitonclick()
def __init__(self,x,y): Ball.__init__(self,x,y) self._color = 'green'