Skip to content

Commit

Permalink
This change adds a time bonus for killing an enemy
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Baker committed Jul 13, 2017
1 parent 42296f6 commit 1a3c8f4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

*Demo*

![](http://i.imgur.com/onSjPBU.gif)
![](http://i.imgur.com/6IYe49H.gif)

## Running the Game
You will need the pygame and python3 files installed, as well as the images from my repository (e.g. .\\images\\*) wherever you copy the scripts. Cloning the repository is the easiest method, or download the whole thing. I don't have a lot of "extra" stuff in the repo
Expand Down Expand Up @@ -71,7 +71,8 @@ Container class for the sprites that fly in for the current level display. It c
### level_timer.py
Container class for a frame background image and several digit images (different iamges from the level digits, but same code driving it) which represent the time spent on the current level MM:SS:hh


### time_bonus.py
Text appearing above slain foes showing a time bonus reduction. It slowly flashes and rises before vanishing. The bonus is reflected in the level_timer



Binary file modified images/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"""This module implements the player object (sprite) for Py-Climber"""

import src.game_functions as gf
from pygame.sprite import Group
from src.animation import Animation
from src.animated_sprite import AnimatedSprite
from src.time_bonus import TimeBonus
import pygame

class Player(AnimatedSprite):
"""Player object"""

def __init__(self, settings, screen, images, initial_bounding_rect):
def __init__(self, settings, screen, images, initial_bounding_rect, tile_map):
"""Initialize the player sprite"""
# Calls AnimatedSprite, which in turn will call pygame.Sprite __init_()
super().__init__(settings, screen, images)

self.tile_map = tile_map

# Override the initial position
self.initial_bounding_rect = initial_bounding_rect
self.rect.bottom = initial_bounding_rect.bottom
Expand Down Expand Up @@ -200,4 +201,6 @@ def remove_enemies_above_blocks(self, collision_list):
for enemy in self.enemies:
if kill_rect.colliderect(enemy.rect):
enemy.dying = True
enemy.dy = self.settings.enemy_death_dy
enemy.dy = self.settings.enemy_death_dy
bonus = TimeBonus(enemy.rect, "-0.5 seconds", 500, self.tile_map.level_timer, self.settings.bonus_font)
self.tile_map.bonuses.append(bonus)
3 changes: 3 additions & 0 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def __init__(self):
self.font = pygame.freetype.SysFont(None, 16)
self.font_color = (255, 255, 255)

# Bonus font
self.bonus_font = pygame.freetype.SysFont(None, 10)

# Global sprite settings
self.gravity = 1.4
self.terminal_velocity = 12
Expand Down
16 changes: 14 additions & 2 deletions src/tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from src.blob_exit import BlobExit
from src.level_info import LevelInfo
from src.level_timer import LevelTimer
from src.time_bonus import TimeBonus
import src.game_functions as gf
import random
from pygame.sprite import Group
Expand Down Expand Up @@ -33,7 +34,8 @@ def __init__(self, settings, screen, map_indicies, images, block_image, exit_ima
self.new_enemy_counter = 0
self.level_info = LevelInfo(self.settings, self.screen)
self.level_timer = LevelTimer(self.settings, self.screen)

self.bonuses = []

def reset(self):
"""Resets the game to the starting state"""
self.player.reset()
Expand Down Expand Up @@ -103,7 +105,7 @@ def generate_basic_map(self, number_of_floors, number_of_subfloor_rows=0):
self.blob_exit = BlobExit(self.settings, self.screen, self.exit_images, self)

# Create the player
self.player = Player(self.settings, self.screen, self.player_images, self.player_bounds_rect)
self.player = Player(self.settings, self.screen, self.player_images, self.player_bounds_rect, self)

# Position the timer
self.level_timer.position_frame(self.screen_rect.centery, self.player_bounds_rect.right + self.settings.tile_width * 2)
Expand Down Expand Up @@ -202,6 +204,12 @@ def update(self):
# Update the level timer
self.level_timer.update()

# bonuses
for bonus in self.bonuses:
bonus.update()
if not bonus.alive():
self.bonuses.remove(bonus)

def draw_tiles(self, draw_grid_overlay=False):
"""Draws just the tile portion of the map"""
# Make the bottom of the map align with the bottom of the screen
Expand Down Expand Up @@ -250,3 +258,7 @@ def draw(self, draw_grid_overlay=False):

# Draw the level timer
self.level_timer.draw()

# Draw bonuses
for bonus in self.bonuses:
bonus.draw(self.screen)
45 changes: 45 additions & 0 deletions src/time_bonus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""This module implements the time bonus for killing a blob"""
from src.level_timer import LevelTimer
import random
import pygame

class TimeBonus():
"""Time reduction for killing a blob"""

def __init__(self, enemy_rect, text, milliseconds, level_timer, font):
"""save the initial state"""
self.ms_reduction = milliseconds
self.enemy_rect = enemy_rect
self.dy = -4
self.frame = 0
self.frame_delay = 2
self.frames_max = 80
self.total_frames = 0
self.font = font
self.text = text
self.text_rect = self.font.get_rect(self.text)
self.text_rect.left = self.enemy_rect.left
self.text_rect.top = self.enemy_rect.top
self.color = (255, 0, 0)

level_timer.elapsed_time_ms = max(0, level_timer.elapsed_time_ms - milliseconds)

def alive(self):
"""Check if max frames has expired"""
return self.total_frames < self.frames_max

def update(self):
"""Move the text"""
self.frame += 1
self.total_frames += 1
if self.frame > self.frame_delay:
self.frame = 0
self.text_rect.move_ip(0, self.dy)
self.color = (random.choice([255, 0]), 0, random.choice([255, 0]))

def draw(self, screen):
"""Draw the current text"""
if self.total_frames < self.frames_max and self.text_rect.top >= 0:
self.font.render_to(screen, (self.text_rect.left, self.text_rect.top), self.text, self.color)


0 comments on commit 1a3c8f4

Please sign in to comment.