def main(): '''This function defines the 'mainline logic' for our game.''' # Display screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Snake") background = pygame.image.load("grass.png") background = background.convert() screen.blit(background, (0, 0)) #Entities Cuckoo = pygame.mixer.Sound("Cuckoo.wav") Cuckoo.set_volume(1.0) Powerup = pygame.mixer.Sound("Powerup.wav") Powerup.set_volume(1.0) Laugh = pygame.mixer.Sound("pacman_eatfruit.wav") Laugh.set_volume(1.0) pygame.mixer.music.load("Chill.mp3") pygame.mixer.music.set_volume(0.4) pygame.mixer.music.play(-1) EndGroup = pygame.sprite.Group() for y in range(4): EndGroup.add(mySprites.EndZone(screen, y)) BodyGroup = pygame.sprite.Group() while True: xBall = random.randrange(20, screen.get_width() - 20, 20) yBall = random.randrange(60, screen.get_height() - 20, 20) if xBall != screen.get_width() / 2 and yBall != screen.get_height() / 2: ball = mySprites.Ball(screen) ball.setPosition(xBall, yBall) break player = mySprites.Head(screen) Score = mySprites.ScoreKeeper() allSprites = pygame.sprite.OrderedUpdates(EndGroup, ball, player, Score, BodyGroup) # ACTION # Assign clock = pygame.time.Clock() keepGoing = True up = True left = True down = True right = True play = 1 # Loop while keepGoing: # Time clock.tick(30) # Events for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and up == True: player.go_up() down = False right = True left = True up = True if event.key == pygame.K_DOWN and down == True: player.go_down() up = False left = True down = True right = True if event.key == pygame.K_RIGHT and right == True: player.go_right() up = True left = False down = True right = True if event.key == pygame.K_LEFT and left == True: player.go_left() up = True left = True down = True right = False if event.key == pygame.K_ESCAPE: keepGoing = False if player.rect.colliderect(ball.rect): Segment = mySprites.Body(screen) BodyGroup.add(Segment) ball.kill() Laugh.play() Score.player() ball = mySprites.Ball(screen) switch = True check = 0 while switch == True: #My attempt to make sure that the ball doesn't land on the body using distance formula. #The way the balls are placed is still alittle buggy and I don't know how to fix it. xBall = random.randrange(20, screen.get_width() - 20, 20) yBall = random.randrange(60, screen.get_height() - 20, 20) for bodies in range(len(BodyGroup.sprites()) - 1, -1, -1): if math.sqrt((BodyGroup.sprites()[bodies].get_centerx() - xBall)**2 + (BodyGroup.sprites()[bodies].get_centery() - yBall)**2) >= 20: check += 1 if check == len(BodyGroup.sprites()): ball.setPosition(xBall, yBall) switch = False else: break allSprites.add(ball, BodyGroup) #Collision with the head and the Border collision = pygame.sprite.spritecollide(player, EndGroup, False) if collision: Cuckoo.play() #Will reset the game for sprites in range(len(BodyGroup.sprites()) - 1, -1, -1): BodyGroup.sprites()[sprites].kill() ball.kill() ball = mySprites.Ball(screen) player.kill() player = mySprites.Head(screen) while True: xBall = random.randrange(20, screen.get_width() - 20, 20) yBall = random.randrange(60, screen.get_height() - 20, 20) if xBall != screen.get_width( ) / 2 and yBall != screen.get_height() / 2: ball = mySprites.Ball(screen) ball.setPosition(xBall, yBall) break allSprites.add(ball, player) Score.reset() play = 1 up = True left = True down = True right = True #Collision with the head and the Bodygroup collide = pygame.sprite.spritecollide(player, BodyGroup, False) if collide: Cuckoo.play() #Will reset the game for sprites in range(len(BodyGroup.sprites()) - 1, -1, -1): BodyGroup.sprites()[sprites].kill() ball.kill() ball = mySprites.Ball(screen) player.kill() player = mySprites.Head(screen) while True: xBall = random.randrange(20, screen.get_width() - 20, 20) yBall = random.randrange(60, screen.get_height() - 20, 20) if xBall != screen.get_width( ) / 2 and yBall != screen.get_height() / 2: ball = mySprites.Ball(screen) ball.setPosition(xBall, yBall) break allSprites.add(ball, player) Score.reset() play = 1 up = True left = True down = True right = True #This for-loop controls the position of the body segments for index in range(len(BodyGroup.sprites()) - 1, 0, -1): x1 = BodyGroup.sprites()[index - 1].get_centerx() y1 = BodyGroup.sprites()[index - 1].get_centery() BodyGroup.sprites()[index].set_position(x1, y1) #This if-statement controls the first body segment and is used to #update all the other segments if len(BodyGroup.sprites()) > 0: x = player.get_x() y = player.get_y() BodyGroup.sprites()[0].set_position(x, y) #The speed is adjusted by using delay. Each time the player reaches #a certain score, the game gets a bit faster if Score.get_score() <= 10: pygame.time.delay(150) elif Score.get_score() >= 11 and Score.get_score() <= 30: if Score.get_score() == 11 and play == 1: Powerup.play() play += 1 pygame.time.delay(120) elif Score.get_score() >= 31 and Score.get_score() <= 50: if Score.get_score() == 31 and play == 2: Powerup.play() play += 1 pygame.time.delay(90) elif Score.get_score() >= 51 and Score.get_score() <= 70: if Score.get_score() == 51 and play == 3: Powerup.play() play += 1 pygame.time.delay(60) elif Score.get_score() >= 71 and Score.get_score() <= 90: if Score.get_score() == 71 and play == 4: Powerup.play() play += 1 pygame.time.delay(30) elif Score.get_score() >= 91: if Score.get_score() == 91 and play == 5: Powerup.play() play += 1 pygame.time.delay(0) # Refresh screen allSprites.clear(screen, background) # The next line calls the update() method for any sprites in the allSprites group. allSprites.update() allSprites.draw(screen) pygame.display.flip() # Close the game window pygame.quit()
def main(): '''This function defines the 'mainline logic' for the Flappy Bird pygame.''' # Display pygame.display.set_caption("Flappy Bird") # Entities background = pygame.image.load("background.png") background = background.convert() screen.blit(background, (0, 0)) # Sprites for: Pipe, ScoreKeeper, Coin, Bird, Ground, and PointZone pipe = mySprites.Pipe(screen) scoreKeeper = mySprites.ScoreKeeper() coin = mySprites.Coin(screen) bird = mySprites.Bird(screen) ground = mySprites.Ground(screen) pointZone = mySprites.PointZone(screen) allSprites = pygame.sprite.OrderedUpdates(pointZone, pipe, ground, coin, bird, scoreKeeper) # Load "GameOver" Image to Display After Game Loop Terminates gameover = pygame.image.load("Game Over.png") # Background Music and Sound Effects pygame.mixer.music.load("background music.wav") pygame.mixer.music.set_volume(0.2) pygame.mixer.music.play(-1) bing = pygame.mixer.Sound("bing.ogg") bing.set_volume(0.6) died = pygame.mixer.Sound("died.ogg") died.set_volume(0.6) # ACTION # Assign clock = pygame.time.Clock() keepGoing = True # Loop while keepGoing: # Time clock.tick(30) # Events for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False elif event.type == pygame.KEYDOWN: # When the space key is pressed, the go_up() method from the bird class is called if event.key == pygame.K_SPACE: bird.go_up() elif event.type != pygame.KEYDOWN: # If no key is pressed, the go_down() method from the bird class is called bird.go_down() # Check if the coin was hit, if so, 2 points will be added to the score as bonus if bird.rect.colliderect(coin): scoreKeeper.player_scored(2) coin.died() # Check if the bird had collided with the pipe if bird.rect.colliderect(pipe.rect): #Check if the bird has collided with a certain point on the pipe for y in range(0, pipe.rect.bottom): if bird.rect.collidepoint(pipe.rect.left, y): #If the bird did not collide with any points, continue the game if y in range(pipe.rect.centery - 30, pipe.rect.centery + 50): keepGoing = True # If the bird had collided with any of the points, terminate game else: keepGoing = False # Check if the bird had collided with pointzone if bird.rect.colliderect(pointZone.rect): #Add one point and plays sound effect scoreKeeper.player_scored(1) bing.play() # End game loop when the player hits the ground if bird.lost(): pygame.mixer.music.fadeout(2000) keepGoing = False # Check to see if the player has beat the game, if so end the game loop if scoreKeeper.winner(): pygame.mixer.music.fadeout(2000) keepGoing = False # Check to see if the player is at the half way point of the game, if so, change the background if scoreKeeper.half_way(): background = pygame.image.load("night background.png") background = background.convert() screen.blit(background, (0, 0)) # Refresh screen allSprites.clear(screen, background) allSprites.update() allSprites.draw(screen) pygame.display.flip() #Play ending sound effect died.play() # Blit gameover message screen.blit(gameover, (50, 150)) pygame.display.flip() # Delay to close the game window pygame.time.delay(3000) # Close the game window pygame.quit()
def main(): '''This function defines the 'mainline logic' for our Super Break-Out game.''' # DISPLAY pygame.display.set_caption("Ace Pilot v1.0") # ENTITIES background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((0, 0, 0)) screen.blit(background, (0, 0)) # load game background music pygame.mixer.music.load( "music/bgm.mp3") pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play(-1) is_shield_on = False # (not finishing with shield) player = mySprites.Player(screen, is_shield_on) # player initial score and health player_score = mySprites.ScoreKeeper(screen) player_health = mySprites.PlayerHealth(screen) # enemy missiles initial speed missile_speed = 5 # load two images as game background sky = mySprites.Background(screen) sky2 = mySprites.Background2(screen) # assigan player's x, y speed player_xspeed = 5 player_yspeed = -5 laser_sound = pygame.mixer.Sound("sound effects/laser.ogg") laser_sound.set_volume(0.5) enemy_explosion_sound = pygame.mixer.Sound("sound effects/explode.ogg") enemy_explosion_sound.set_volume(0.5) # put the sprites into group otherGroup = pygame.sprite.Group(player_score, player_health) aircraftGroup = pygame.sprite.Group(player) enemyGroup = pygame.sprite.Group(mySprites.Enemy(screen)) playermissileGroup = pygame.sprite.Group() enemymissileGroup = pygame.sprite.Group() backgroundGroup = pygame.sprite.Group(sky, sky2) enemyHitPlayerGroup = pygame.sprite.Group() missileHitPlayerGroup = pygame.sprite.Group() potionGroup = pygame.sprite.Group() # load gameover image gameover = pygame.image.load("images/gameover1.gif") gameover = gameover.convert() # Action # Assign keepGoing = True clock = pygame.time.Clock() counter_for_spawning_enemy = 0 # a time counter used to spawn enemy # a time counter used to increase enemy missile speed counter_for_enemy_missile_speed = 0 fire_cooldown = 20 # missile cooldown time fire = False # determine if the player is firing missiles # Loop while keepGoing: clock.tick(30) counter_for_spawning_enemy += 1 counter_for_enemy_missile_speed += 1 # Events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mixer.music.fadeout(2000) keepGoing = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.change_direction((-player_xspeed, 0)) elif event.key == pygame.K_RIGHT: player.change_direction((player_xspeed, 0)) elif event.key == pygame.K_UP: player.change_direction((0, -player_yspeed)) elif event.key == pygame.K_DOWN: player.change_direction((0, player_yspeed)) elif event.key == pygame.K_SPACE: fire = True elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or pygame.K_RIGHT or pygame.K_UP or pygame.K_DOWN: player.change_direction((0, 0)) if event.key == pygame.K_SPACE: fire = False # player is firing missiles if fire: if fire_cooldown == 0: playermissileGroup.add(mySprites.PlayerMissile( screen, player.current_position())) laser_sound.play() fire_cooldown = 20 # set a fire cooldown time so that the missile will not be casted too fast if fire_cooldown != 0: fire_cooldown -= 1 # spawn enemy if counter_for_spawning_enemy == 50: enemyGroup.add(mySprites.Enemy(screen)) counter_for_spawning_enemy = 0 # enemy missile speed increases as time goes by if counter_for_enemy_missile_speed == 1000: if missile_speed != 15: missile_speed += 2 counter_for_enemy_missile_speed = 0 if missile_speed == 15: missile_speed += 0 # check if player's missiles hit enemies for missile in playermissileGroup: collide_enemy = pygame.sprite.spritecollide( missile, enemyGroup, True) for enemy in collide_enemy: enemy_explosion_sound.play() player_score.player_scored() enemyHitPlayerGroup.add(mySprites.EnemyHitPlayer( screen, enemy.current_position())) missile.kill() # the player will have a chance to earn a potion as reward every time # the player killed an enemy potion_reward = random.randint(1, 11) if potion_reward == 1: potionGroup.add(mySprites.Potion( screen, enemy.current_position())) # check if potion hit the player potion_hit_player = pygame.sprite.spritecollide( player, potionGroup, True) for potion in potion_hit_player: player_health.health_increase() # check if enemies hit the player enemy_hit_player = pygame.sprite.spritecollide( player, enemyGroup, True) for enemy2 in enemy_hit_player: enemy_explosion_sound.play() player_health.health_decrease() enemyHitPlayerGroup.add(mySprites.EnemyHitPlayer( screen, enemy2.current_position())) # enemies fire missiles for enemy3 in enemyGroup: enemy_missile_fire = random.randint(1, 30) if enemy_missile_fire == 1: enemymissileGroup.add(mySprites.EnemyMissile( screen, enemy3.current_position(), missile_speed)) # check if enemies' missiles hit the player emissle_hit_player = pygame.sprite.spritecollide( player, enemymissileGroup, True) for emissile in emissle_hit_player: enemy_explosion_sound.play() missileHitPlayerGroup.add(mySprites.MissleHitPlayer( screen, emissile.current_position())) player_health.health_decrease() # check if the player's health reaches 0 if player_health.game_over(): pygame.mixer.music.fadeout(2000) keepGoing = False backgroundGroup.clear(screen, background) playermissileGroup.clear(screen, background) aircraftGroup.clear(screen, background) otherGroup.clear(screen, background) enemyGroup.clear(screen, background) enemyHitPlayerGroup.clear(screen, background) enemymissileGroup.clear(screen, background) missileHitPlayerGroup.clear(screen, background) potionGroup.clear(screen, background) backgroundGroup.update() playermissileGroup.update() aircraftGroup.update() otherGroup.update() enemyGroup.update() enemyHitPlayerGroup.update() enemymissileGroup.update() missileHitPlayerGroup.update() potionGroup.update() backgroundGroup.draw(screen) playermissileGroup.draw(screen) aircraftGroup.draw(screen) otherGroup.draw(screen) enemyGroup.draw(screen) enemyHitPlayerGroup.draw(screen) enemymissileGroup.draw(screen) missileHitPlayerGroup.draw(screen) potionGroup.draw(screen) pygame.display.flip() screen.blit(gameover, (0, 0)) pygame.display.flip() pygame.time.delay(2500) pygame.quit()
def game(): '''actual game loop''' # E - Entities # sound effects gunshot = pygame.mixer.Sound("./Music/Gun Shot.ogg") gunshot.set_volume(0.1) spawn_yeti = pygame.mixer.Sound("./Music/Kobe.ogg") spawn_yeti.set_volume(0.35) #crosshair crosshair = mySprites.Crosshair() #background background = mySprites.Background(screen) floor = mySprites.Floor(screen, background) #moving characters player = mySprites.Player(screen, floor) gun = mySprites.Weapon(screen, player) #player stats health_bar = mySprites.HealthBar(player) health_num = mySprites.HealthNum(player, health_bar) lives = mySprites.Lives(player) score_keeper = mySprites.ScoreKeeper() #enemy sprites yetiGroup = [] for i in range(2): yetiGroup.append( mySprites.Yeti(background, floor, player, random.randint(1000, 2000))) #groups statGroup = pygame.sprite.Group(health_bar, health_num, lives, score_keeper) bulletGroup = pygame.sprite.Group() enemyBullets = pygame.sprite.Group() enemies = pygame.sprite.Group(yetiGroup) allSprites = pygame.sprite.OrderedUpdates(background, enemies, player, floor, bulletGroup, enemyBullets, gun, statGroup, crosshair) # A - Action # A - Assign key variables pygame.mouse.set_visible(False) clock = pygame.time.Clock() gameRun = True # L - Loop while gameRun: # T - Time clock.tick(30) # E - Event for event in pygame.event.get(): if event.type == pygame.QUIT: return True if event.type == pygame.KEYDOWN: #if player not dead if not player.get_dead(): #movement things if event.key == pygame.K_a: background.go_right() if event.key == pygame.K_d: background.go_left() if event.key == pygame.K_w: player.jump() #changing weapon if event.key == pygame.K_1: gun.set_primary() if event.key == pygame.K_2: gun.set_secondary() #stoping movement things if event.type == pygame.KEYUP: if event.key == pygame.K_a: background.right_idle() if event.key == pygame.K_d: background.left_idle() #player shooting if pygame.mouse.get_pressed() == (1, 0, 0) and gun.get_can_fire(): gunshot.play() gun.fire() bulletGroup.add( mySprites.PlayerBullet(screen, background, gun, pygame.mouse.get_pos())) allSprites = pygame.sprite.OrderedUpdates( background, enemies, player, floor, bulletGroup, enemyBullets, gun, statGroup, crosshair) #yeti randomly shoots for enemy in enemies: if enemy.get_attackmode(): if random.randint(1, 40) == 1: enemyBullets.add( mySprites.EnemyProjectile(background, enemy, player)) allSprites = pygame.sprite.OrderedUpdates( background, enemies, player, floor, bulletGroup, enemyBullets, gun, statGroup, crosshair) #if player contacts yetienemy player_hit_yeti = pygame.sprite.spritecollide(player, enemies, False) if player_hit_yeti: for enemy in player_hit_yeti: player.take_dmg(enemy.attack()) #player getting hit by enemy bullet bullet_hit_player = pygame.sprite.spritecollide( player, enemyBullets, False) for bullet in bullet_hit_player: player.take_dmg(3) bullet.kill() enemyBullets.remove(bullet) #bullet hitting enemy for bullet in bulletGroup: enemy_hit_list = pygame.sprite.spritecollide( bullet, enemies, False) for enemy in enemy_hit_list: enemy.take_dmg(bullet.get_dmg()) #kill yeti and spawn two more if enemy.get_health() <= 0: spawn_yeti.play() xlocation = enemy.rect.centerx enemy.kill() yetiGroup.remove(enemy) score_keeper.add_score(420) for i in range(2): yetiGroup.append( mySprites.Yeti( background, floor, player, random.randint(xlocation - 500, xlocation + 500))) enemies = pygame.sprite.Group(yetiGroup) allSprites = pygame.sprite.OrderedUpdates( background, enemies, player, floor, bulletGroup, enemyBullets, gun, statGroup, crosshair) bullet.kill() bulletGroup.remove(bullet) #if player has fallen thru bottom of screen and died if player.rect.top > screen.get_height(): gameRun = False game_over(screen, root, allSprites) # R - Refresh Screen allSprites.clear(screen, root) allSprites.update() allSprites.draw(screen) pygame.display.flip() #close game pygame.mouse.set_visible(True) return False
def main(): '''This function defines the 'mainline logic' for our invaders game.''' # DISPLAY pygame.display.set_caption("marioinvaders") # ENTITIES background = pygame.image.load("background.png") background = background.convert() # Sprites player = mySprites.Spaceship(screen,40,40) score_keeper = mySprites.ScoreKeeper() alien = mySprites.Aliens(screen,"white.png", 120,0) boss = mySprites.Boss(screen) boss_wall = mySprites.Walls(screen,"wall2.png",360) endzone = mySprites.EndZone(screen) aliens= [] walls =[] #create our minions for i in range(1): aliens.append(mySprites.Aliens(screen,"shyguy.png",75,0+30*i)) aliens.append(mySprites.Aliens(screen,"spiny.png",100,0+30*i)) aliens.append(mySprites.Aliens(screen,"goomba.png",125,0+30*i)) alien_group= pygame.sprite.Group(aliens) #create 5 defense walls for i in range(5): walls.append(mySprites.Walls(screen,"wall1.png",98+128*i)) wall_group = pygame.sprite.Group(walls) #boss bullet group bg = pygame.sprite.Group() allSprites = pygame.sprite.OrderedUpdates(player,score_keeper,aliens,walls,alien) #music and sounds pygame.mixer.music.load("mario_music.mp3") pygame.mixer.music.set_volume(0.6) pygame.mixer.music.play(-1) fireball_sound = pygame.mixer.Sound("fireball.wav") kick_sound= pygame.mixer.Sound("kick.wav") lose_life_sound=pygame.mixer.Sound("loselife.wav") bill_sound=pygame.mixer.Sound("billsound.wav") fireball_sound.set_volume(1) kick_sound.set_volume(1) lose_life_sound.set_volume(1) bill_sound.set_volume(0.6) # ASSIGN clock = pygame.time.Clock() keepGoing = True bullet_exists= False alien_bullet=False boss_alive=False boss_round=False # Hide the mouse pointer pygame.mouse.set_visible(False) # LOOP while keepGoing: # TIME clock.tick(60) # EVENT HANDLING: Player uses keys for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mixer.music.fadeout(3000) keepGoing = False #check for keys to make player move and shoot if event.type == pygame.KEYDOWN: #checks if its is minion round or boss round #if minion round 1 bullet on screen at time #if boss a bullet is spawned whenever a button is pressed #left if event.key == pygame.K_a: player.change_direction((0,1)) if alien_bullet==False: bill_sound.play() alien_bullet=True alien_shot= mySprites.AlienShot(screen,"bulletbill.png",random.randint(0,639),alien.rect.bottom) allSprites.add(alien_shot) if boss_alive==True: bill_sound.play() boss_shot= mySprites.AlienShot(screen,"bulletbill.png",random.randint(boss.rect.left,boss.rect.right),boss.rect.bottom) bg.add(boss_shot) allSprites.add(bg) #right if event.key == pygame.K_d: player.change_direction((0,-1)) if alien_bullet==False: bill_sound.play() alien_bullet=True alien_shot= mySprites.AlienShot(screen,"bulletbill.png",random.randint(0,639),alien.rect.bottom) allSprites.add(alien_shot) if boss_alive==True: bill_sound.play() boss_shot= mySprites.AlienShot(screen,"bulletbill.png",random.randint(boss.rect.left,boss.rect.right),boss.rect.bottom) bg.add(boss_shot) allSprites.add(bg) #shoot if event.key == pygame.K_SPACE: if alien_bullet==False: bill_sound.play() alien_bullet=True alien_shot= mySprites.AlienShot(screen,"bulletbill.png",random.randint(0,639),alien.rect.bottom) allSprites.add(alien_shot) if boss_alive==True: bill_sound.play() boss_shot= mySprites.AlienShot(screen,"bulletbill.png",random.randint(boss.rect.left,boss.rect.right),boss.rect.bottom) bg.add(boss_shot) allSprites.add(bg) #checks if there is a fireball on the screen when space is pressed #if not one is created if event.key == pygame.K_SPACE and bullet_exists == False: bullet_exists = True fireball_sound.play() shot= mySprites.Shot(screen,"fireball.png",player.rect.centerx) allSprites.add(shot) #check if our bullet reaches the end of the screen if bullet_exists == True: if shot.check_bullet()==True: shot.kill() bullet_exists= False # check if our bullet collides with an minion if pygame.sprite.spritecollide(shot, alien_group, False) and boss_alive == False: bullet_exists = False kick_sound.play() #if the bullet does our player can shoot again and the bullet will die and #give player score for index in pygame.sprite.spritecollide(shot, alien_group, True): if boss_alive == False: shot.kill() score_keeper.player_scored() #check if our bullet collides with our wall group if so our bullet will die #and our player can shoot again if pygame.sprite.spritecollide(shot, wall_group, False) and boss_alive==False: shot.kill() bullet_exists = False #check if our bullet collides with the boss wall if shot.rect.colliderect(boss_wall.rect): shot.kill() bullet_exists=False if shot.rect.colliderect(boss.rect): kick_sound.play() shot.kill() bullet_exists=False score_keeper.boss_lose_life() #check if our alien bullet is alive and reaches the end of the screen if alien_bullet==True: if alien_shot.check_bullet()==True: alien_shot.kill() alien_bullet=False #check if our alien bullet collides with our player if so we lose 1 life if alien_shot.rect.colliderect(player.rect): lose_life_sound.play() alien_bullet = False alien_shot.kill() score_keeper.lose_life() #check if our alien bulelt collides with our wall group if so the bullet dies if pygame.sprite.spritecollide(alien_shot, wall_group, False): alien_shot.kill() alien_bullet = False #Check if our boss bullet collides with our boss wall or bottom of screen and kills bullet if so if pygame.sprite.spritecollide(boss_wall, bg, True): continue if pygame.sprite.spritecollide(endzone, bg, True): continue #check if our boss bullet collides with our player if so kills bullet and #player loses life for index in pygame.sprite.spritecollide(player, bg, True): lose_life_sound.play() score_keeper.lose_life() #if all aliens die our boss will spawn and a new wall will appear if score_keeper.get_aliens()<=0: alien_bullet=True boss_alive=True allSprites.remove(aliens,walls) allSprites.add(boss,boss_wall) #if boss loses all lives player wins if score_keeper.get_boss_lives()<=0: keepGoing = False #if play loses all lives game over if score_keeper.get_lives() <= 0: keepGoing = False # REFRESH SCREEN screen.blit(background, (0, 0)) allSprites.clear(screen, background) allSprites.update() allSprites.draw(screen) pygame.display.flip() # Unhide the mouse pointer pygame.mouse.set_visible(True) # Close the game window pygame.time.delay(3000) pygame.quit()
def main(): '''This function defines the 'mainline logic' for our pyPong game.''' # DISPLAY pygame.display.set_caption("Super Break-Out") # ENTITIES screen = pygame.display.set_mode((640, 480)) background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((0, 0, 0)) screen.blit(background, (0, 0)) # Creating a list for the breaks brick = [] # First layer of the brick for i in range(18): brick.append(mySprites.Brick(screen, i * 37, 70, 255, 0, 255)) # Second layer of the brick for i in range(18): brick.append(mySprites.Brick(screen, i * 37, 80, 255, 0, 0)) # Thrid layer of the brick for i in range(18): brick.append(mySprites.Brick(screen, i * 37, 90, 255, 255, 0)) # Fourth layer of the brick for i in range(18): brick.append(mySprites.Brick(screen, i * 37, 100, 255, 165, 0)) # Fifth layer of the brick for i in range(18): brick.append(mySprites.Brick(screen, i * 37, 110, 0, 255, 0)) # Last layer of the brick for i in range(18): brick.append(mySprites.Brick(screen, i * 37, 120, 0, 0, 205)) # Creating all the sprites score_keeper = mySprites.ScoreKeeper() player_endzone = mySprites.EndZone(screen) ball = mySprites.Ball(screen) player = mySprites.Player(screen, 1) player2 = mySprites.Player(screen, 2) bricksprites = pygame.sprite.Group(brick) allSprites = pygame.sprite.Group(ball, player, player2, brick, player_endzone, score_keeper) # Importing the sounds gameover = pygame.image.load("gameover.png") gameover.convert() pygame.mixer.music.load("music.mp3") bounce = pygame.mixer.Sound("bounce.wav") bounce.set_volume(0.1) pygame.mixer.music.set_volume(0.3) # Making the bgm loop pygame.mixer.music.play(-1) # ASSIGN clock = pygame.time.Clock() keepGoing = True # Hide the mouse pointer pygame.mouse.set_visible(False) # LOOP while keepGoing: # TIME clock.tick(30) # EVENT HANDLING: Player 1 using left and right arrow keys for event in pygame.event.get(): if event.type == pygame.QUIT: # Fade out time for 3 seconds pygame.mixer.music.fadeout(3000) keepGoing = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: player.change_direction((-1, 0)) if event.key == pygame.K_LEFT: player.change_direction((1, 0)) # If ball reacts with player 1 it will change direction and play a sound if ball.rect.colliderect(player): ball.change_direction() bounce.play() # If ball reacts with player 2 it will change direction and play a sound if ball.rect.colliderect(player2): ball.change_direction() bounce.play() # Creating a for loop to see every single collision event for brick in pygame.sprite.spritecollide(ball, bricksprites, True): for walls in bricksprites: walls.move() # Gain points for every brick destroyed score_keeper.player1_points() ball.change_direction() bounce.play() # If ball reacts with the end zone, players will lose a life and the ball will reset if ball.rect.colliderect(player_endzone): ball.reset() score_keeper.player1_life() # If all the bricks are destroyed player wins, then the game ends if score_keeper.winner(): pygame.mixer.music.fadeout(3000) keepGoing = False # If player loses all their life, the game ends if score_keeper.loser(): pygame.mixer.music.fadeout(3000) keepGoing = False # REFRESH SCREEN allSprites.clear(screen, background) allSprites.update() allSprites.draw(screen) pygame.display.flip() # Unhide the mouse pointer and have a delay on the quit pygame.time.delay(3000) pygame.mouse.set_visible(True) # Close the game window pygame.quit()