def generate_clouds(clouds, fbool): if fbool: for i in range(10): cloud = sprites.Cloud( (random.randint(50, 430), random.randint(100, 500)), random.randint(0, 3), clouds) elif not fbool: if len(clouds.sprites()) < 10: cloud = sprites.Cloud((500, random.randint(100, 500)), random.randint(0, 3), clouds)
def main(): running = True title_text = py_text.get_text("Unicorn Cake Game(c)", 64) title_rect = title_text.get_rect() title_rect.center = (SCREEN_WIDTH // 2, (SCREEN_HEIGHT // 2) - title_rect.height) header2_text = py_text.get_text("Coded by Paul Millar", 43) header3_text = py_text.get_text("Designed by Ivy Snow Leeder", 43) header4_text = py_text.get_text("Press 'Return' key to Play.", 32) header2_rect = header2_text.get_rect() header2_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) header3_rect = header3_text.get_rect() header3_rect.center = (SCREEN_WIDTH // 2, (SCREEN_HEIGHT // 2) + header2_rect.height) current_y_pos = (SCREEN_HEIGHT // 2) + header3_rect.height current_y_pos += header3_rect.height + 20 header4_rect = header4_text.get_rect() header4_rect.center = (SCREEN_WIDTH // 2, current_y_pos) clouds = pygame.sprite.Group() all_sprites = pygame.sprite.Group() pygame.mixer.music.load(os.path.join(sfx.SOUND_PATH, "menu.mp3")) pygame.mixer.music.play(loops=-1) while running: for event in pygame.event.get(): if event.type == pygame.KEYUP: if event.key == pygame.K_ESCAPE: running = False if event.key == pygame.K_RETURN: pygame.mixer.music.stop() game_loop() pygame.mixer.music.load(os.path.join(sfx.SOUND_PATH, "menu.mp3")) pygame.mixer.music.play(loops=-1) if event.key == pygame.K_1: unicorn_selection_menu() if event.type == ADD_CLOUD: cloud = sprites.Cloud() clouds.add(cloud) all_sprites.add(cloud) clouds.update() screen_buffer.fill(graphics.SKY_BLUE) for entity in all_sprites: screen_buffer.blit(entity.surf, entity.rect) screen_buffer.blit(title_text, title_rect) screen_buffer.blit(header2_text, header2_rect) screen_buffer.blit(header3_text, header3_rect) screen_buffer.blit(header4_text, header4_rect) pygame.display.update() clock.tick(graphics.FRAME_RATE)
def unicorn_selection_menu(): running = True all_sprites = pygame.sprite.Group() unicorns = pygame.sprite.Group() while running: for event in pygame.event.get(): if event.type == ADD_CLOUD: cloud = sprites.Cloud() all_sprites.add(cloud) if event.type == pygame.KEYUP: if event.key == pygame.K_ESCAPE: running = False screen_buffer.fill(graphics.SKY_BLUE) all_sprites.update() for entity in all_sprites: screen_buffer.blit(entity.surf, entity.rect) pygame.display.update() clock.tick(graphics.FRAME_RATE) all_sprites.empty() unicorns.empty() del all_sprites
def generate_clouds(): for i in range(15): generated_int = random.randint(0, 1) cloud = sprites.Cloud( (random.randint(0, public.SWIDTH), random.randint( 0, public.SHEIGHT)), generated_int)
def update(self, display): self.player.update() self.fires.update() self.dust.update() self.projectiles.update() self.animals.update() self.clouds.update() # Allow continuous shooting if pygame.mouse.get_pressed()[0]: self.shoot_fireball() # Determine if player is shooting if pygame.time.get_ticks() - self.previous_fireball < 250: self.player.shooting = True else: self.player.shooting = False # Horizontal Camera scrolling self.cam_x_offset = self.player.rect.x - settings.display_width / 2 if self.cam_x_offset < 0: self.cam_x_offset = 0 if self.cam_x_offset > (len(self.current_level[0]) - 25) * 32: self.cam_x_offset = (len(self.current_level[0]) - 25) * 32 # Reset game if player is out of the screen if self.player.rect.y > settings.display_height - 60 + 64: self.startup() # Randomly spawn clouds cloud_num = random.randint(0, 700) if cloud_num == 700: c = sprites.Cloud(settings.display_width, random.randint(0, 300)) self.clouds.add(c) # Make fireballs Burn for fireballs in self.projectiles: if fireballs.direction == "right": f = sprites.Fire( fireballs.rect.center[0] - 8, fireballs.rect.center[1] + random.randint(-16, 16), random.randint(1, 3) * 8, 8, fireballs.speed - 5, 0, self.walls) elif fireballs.direction == "left": f = sprites.Fire( fireballs.rect.center[0] + 8, fireballs.rect.center[1] + random.randint(-16, 16), random.randint(1, 3) * 8, 8, fireballs.speed + 5, 0, self.walls) self.fires.add(f) # Remove plants destroyed by fireballs for remove_plants in self.details: if remove_plants.dead: for x in range(5): f = sprites.Fire( remove_plants.rect.center[0] + random.randint(-4, 4), remove_plants.rect.bottom, 8, random.randint(1, 3) * 8, 0, -2 + random.randint(-1, 1), self.walls, 35) self.fires.add(f) remove_plants.kill() # Remove dead fires for fire in self.fires: if fire.dead: fire.kill() # Remove dead dust for du in self.dust: if du.dead: du.kill() # Make hit animals Burn and kill dead animals for hit in self.animals: if hit.hit: f = sprites.Fire(hit.rect.center[0] + random.randint(-4, 4), hit.rect.bottom, 8, random.randint(1, 3) * 8, 0, -2 + random.randint(-1, 1), self.walls, 35) self.fires.add(f) if hit.dead: hit.kill() # Remove dead fireballs for fireballs in self.projectiles: if fireballs.dead: if fireballs.direction == "right": for x in range(5): d = sprites.Dust(fireballs.rect.right, fireballs.rect.center[1], 8, -4, random.randint(-4, 4), self.walls) self.dust.add(d) if fireballs.direction == "left": for x in range(5): d = sprites.Dust(fireballs.rect.left, fireballs.rect.center[1], 8, 4, random.randint(-4, 4), self.walls) self.dust.add(d) fireballs.kill() # Dust effect upon ground impact: if self.player.dust > 0: d = sprites.Dust(self.player.rect.center[0], self.player.rect.bottom, 8, random.randint(-5, 5), -3, self.walls) self.dust.add(d) self.player.dust -= 1 # Randomly spawn dust particles when player is moving dust_num = random.randint(0, 40) if self.player.moving and not self.player.jumping and dust_num == 30: d = sprites.Dust(self.player.rect.center[0], self.player.rect.bottom, 8, random.randint(-5, 5), -3, self.walls) self.dust.add(d) # Slowly stop screen shake if self.shake_amount > 0: self.shake_amount -= 0.5 self.draw(display)
@author: bruno ''' import jsonpickle import sprites import baddies import platforms from sprites import * import pygame #This dict is used to deserialize the objects contained in the #JSON file that represents the level. Each class name is associated #with the constructor of the corresponding class constructor_map = dict( Brick=lambda x, y, e: platforms.Brick_Platform((x * 32, y * 32), e * 2), Bush=lambda x, y, e: sprites.Bush((x * 32, y * 32), e * 2), Cloud=lambda x, y, e: sprites.Cloud((x * 32, y * 32), e * 2), Flag=lambda x, y, e: sprites.Flag((x * 32, y * 32), e * 2), Hill=lambda x, y, e: sprites.Hill((x * 32, y * 32), e * 2), Troopa=lambda x, y, e: baddies.Troopa((x * 32, y * 32), e * 2), Greenshroom=lambda x, y, e: sprites.Greenshroom((x * 32, y * 32), e * 2), Redshroom=lambda x, y, e: sprites.Redshroom((x * 32, y * 32), e * 2), Pipe=lambda x, y, e: platforms.Pipe((x * 32, y * 32), e * 2), Bigpipe=lambda x, y, e: platforms.Bigpipe((x * 32, y * 32), e * 2), Brick_Platform=lambda x, y, e: platforms.Brick_Platform( (x * 32, y * 32), e * 2), Question_Platform=lambda x, y, e: platforms.Question_Platform( (x * 32, y * 32), e * 2), Goomba=lambda x, y, e: baddies.Goomba((x * 32, y * 32), e * 2)) #class A: # def __init__(self, x, y, e):
def game_loop(): """ main game loop """ # create a new unicorn player object unicorn = sprites.Unicorn() # Init sprite groups entities = {"enemies": pygame.sprite.Group(), "clouds": pygame.sprite.Group(), "stars": pygame.sprite.Group(), "cakes": pygame.sprite.Group(), "rainbow_powerups": pygame.sprite.Group(), "all": pygame.sprite.Group()} entities["all"].add(unicorn) # setup the unicorn cake score cake_counter = sprites.CakeScore(0, 32) pygame.mixer.music.load(os.path.join(sfx.SOUND_PATH, "main.ogg")) pygame.mixer.music.play(loops=-1) running = True # bool check to see if unicorn is still alive unicorn_is_dead = False while running: # get the game events for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: rainbow_star = sprites.RainbowStar(unicorn) entities["all"].add(rainbow_star) entities["stars"].add(rainbow_star) if event.key == pygame.K_2: running = False if event.type == ADD_ENEMY: # SPAWN A NEW ENEMY if random.randint(0, 1) == sprites.DOG: enemy = sprites.Dog() else: enemy = sprites.Cat() entities["enemies"].add(enemy) entities["all"].add(enemy) if event.type == ADD_CLOUD: # ADD A NEW CLOUD cloud = sprites.Cloud() entities["clouds"].add(cloud) entities["all"].add(cloud) if event.type == ADD_CAKE: # SPAWN A UNICORN CAKE cake = sprites.UnicornCake() entities["cakes"].add(cake) entities["all"].add(cake) # PROCESS EVENTS CHANGE SPRITE POSITIONS unicorn.update(pygame.key.get_pressed()) entities["enemies"].update() entities["stars"].update(entities["enemies"]) entities["clouds"].update() entities["cakes"].update() entities["rainbow_powerups"].update() # DRAW TO BUFFER AND THEN TO SCREEN screen_buffer.fill(graphics.SKY_BLUE) if unicorn_is_dead: dead_unicorn.update() dead_unicorn.draw(screen_buffer) # if last sprite loaded then kill then break the animation loop and exit if dead_unicorn.walk_count == len(dead_unicorn.surfs) -1: screen_buffer.fill(graphics.SKY_BLUE) screen_buffer.blit(dead_unicorn.surfs[-1], dead_unicorn.rect) display_game_results("YOU LOSE", sfx.game_over_voice, 3) dead_unicorn.kill() del dead_unicorn running = False else: screen_buffer.blit(cake_counter.surf, cake_counter.rect) for entity in entities["all"]: entity.draw(screen_buffer) # update the screen pygame.display.update() # if player still alive then check for collisions if not unicorn_is_dead: # CHECK FOR COLLISIONS for _cake in entities["cakes"]: if pygame.sprite.collide_rect(unicorn, _cake): # play cake collection sound sfx.gulp.play() _cake.kill() cake_counter.inc_score() if cake_counter.score == MAX_CAKES: # we won the challlenge!! display_game_results("Congratulations!! You won the Unicorn Cake Challlenge!!!", sfx.level_complete, 4) running = False elif cake_counter.score == 2: rainbow_powerup = sprites.RainbowPowerup() entities["rainbow_powerups"].add(rainbow_powerup) entities["all"].add(rainbow_powerup) for powerup in entities["rainbow_powerups"]: if pygame.sprite.collide_rect(unicorn, powerup): sfx.powerup.play() powerup.kill() unicorn.speed += 20 for enemy in entities["enemies"]: if pygame.sprite.collide_rect(unicorn, enemy): unicorn.kill() enemy.kill() #display_game_results("You Lose!! :(", sfx.game_over_voice, 4) #running = False unicorn_is_dead = True dead_unicorn = sprites.DeadUnicorn(unicorn) clock.tick(graphics.FRAME_RATE) # CLEAN UP unicorn.kill() entities["all"].empty() del entities["all"] del entities["clouds"] del entities["stars"] del entities["enemies"] del entities["cakes"] del cake_counter del unicorn pygame.mixer.music.stop()
def update_clouds(): if len(public.clouds.sprites()) < 15: generated_int = random.randint(0, 2) cloud = sprites.Cloud(((550, random.randint(0, public.SHEIGHT))), generated_int, public.all_sprites, public.clouds)