Skip to content

Commit

Permalink
Game Over and Bullets Done!
Browse files Browse the repository at this point in the history
  • Loading branch information
archieruin committed May 31, 2020
1 parent bbc3b5f commit 925082f
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 57 deletions.
27 changes: 23 additions & 4 deletions game/entities/bullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

class Bullet(Entity, Sprite):

def __init__(self, x, y, radius, color, direction, speed=10):
def __init__(self, x, y, radius, direction, speed=10, damage=1):
super().__init__(x, y, width=radius, height=radius)
Sprite.__init__(self)
self.__radius = radius
self.__color = color
self.__direction = self.normalize_vector2(direction)
print(self.__direction)
self.__speed = speed
self.__damage = damage
self.__vel_x = speed * -self.__direction[0]
self.__vel_y = speed * -self.__direction[1]
self.image = self.load(str(settings.bullets_res / 'bullet.png'))
self.rect = self.image.get_rect(topleft=self.get_pos())

def draw(self, screen):
pygame.draw.circle(screen, self.__color, (self._x, self._y), self.__radius)
screen.blit(self.image, (self._x, self._y))
# pygame.draw.circle(screen, self.__color, (self._x, self._y), self.__radius)

def update(self, dt):
if self._x < settings.SCREEN_WIDTH and self._x > 0 and self._y < settings.SCREEN_HEIGHT and self._y > 0:
Expand All @@ -30,3 +30,22 @@ def update(self, dt):
self.rect = self.image.get_rect(topleft=self.get_pos())
else:
self.kill()

def get_damage(self):
return self.__damage

def set_damage(self, damage):
self.__damage = damage

def get_rect(self):
return self.rect

def collidepoint(self, point):
if self.rect.collidepoint(point):
return True
return False

def colliderect(self, rect):
if self.rect.colliderect(rect):
return True
return False
18 changes: 15 additions & 3 deletions game/entities/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Player(Entity, Sprite):

def __init__(self, x, y, width, height, speed=5, health=50):
def __init__(self, x, y, width, height, speed=5, health=10):
super().__init__(x, y, width, height)
Sprite.__init__(self)

Expand Down Expand Up @@ -105,8 +105,8 @@ def take_damage(self, enemy_pos, damage):
dir_y = pos[1] - enemy_pos[1]
dir_x, dir_y = self.normalize_vector2((dir_x, dir_y))
if self._vx < self.__max_vel and self._vy < self.__max_vel:
self._vx += dir_x * 5
self._vy += dir_y * 5
self._vx += dir_x * 2
self._vy += dir_y * 2

def get_rect(self):
return self.rect
Expand Down Expand Up @@ -164,3 +164,15 @@ def handle_events(self, events, crosshair):
self.idle = True
self.move_right = False
self.move_left = False

def get_health(self):
return self.__health

def set_health(self, health):
self.__health = health

def sub_health(self, health):
self.__health -= health

def add_health(self, health):
self.__health += health
35 changes: 26 additions & 9 deletions game/entities/slime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def __init__(self, x, y, width, height, speed=3, health=5):

self.__slime_res = settings.slime_res
self.__speed = speed
self.__max_vel = speed * 5
self.__health = health
self.__throw_countdown = 0
self.__hit_countdown = 0
self.__last_damage_ticks = 0

self.__move_anim = [
self.scale(self.load(str(self.__slime_res / 'slime_run_anim_f0.png')), width, height),
Expand Down Expand Up @@ -47,6 +49,9 @@ def update(self, dt):
self.image = self.__anim[int(self.__frame)]
self.rect = self.image.get_rect(topleft=self.get_pos())

if self.__health <= 0:
self.kill()

def move_to_player(self, player_pos):
self_pos = self.get_pos()
dir_x = self_pos[0] - player_pos[0]
Expand All @@ -57,19 +62,31 @@ def move_to_player(self, player_pos):
self._vx = -dir_x * self.__speed
self._vy = -dir_y * self.__speed

def throw(self, player_pos):
def throw(self, from_pos, force):
self_pos = self.get_pos()
dir_x = self_pos[0] - player_pos[0]
dir_y = self_pos[1] - player_pos[1]
dir_x = self_pos[0] - from_pos[0]
dir_y = self_pos[1] - from_pos[1]
dir_len = sqrt(pow(dir_x, 2) + pow(dir_y, 2))
if dir_x or dir_y:
dir_x = dir_x / dir_len
dir_y = dir_y / dir_len
self._vx += dir_x * 3
self._vy += dir_y * 3

def take_damage(self):
pass # TODO
self._vx += dir_x * force
self._vy += dir_y * force

def take_damage(self, from_pos, damage):
seconds_passed = (pygame.time.get_ticks() - self.__last_damage_ticks) / 1000
if seconds_passed > 0.1:
self.__health -= damage
self.__last_damage_ticks = pygame.time.get_ticks()
self.__hit_countdown = 5
pos = self.get_pos()
dir_x = pos[0] - from_pos[0]
dir_y = pos[1] - from_pos[1]
dir_x, dir_y = self.normalize_vector2((dir_x, dir_y))
# if self._vx < self.__max_vel and self._vy < self.__max_vel:
# self._vx = dir_x * self.__speed
# self._vy = dir_y * self.__speed
self.throw(from_pos, 40)

def get_health(self):
return self.__health
Expand Down
3 changes: 2 additions & 1 deletion game/game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pygame

from game import settings
from game.states.game_manager import GameStatesManager
from game.states.game_states_manager import GameStatesManager
from game.states.game_states import GameStates


Expand All @@ -21,6 +21,7 @@ def __init__(self):

# Inti game states manager
self.__game_states_manager = GameStatesManager(GameStates.MAIN_MENU)
self.__game_states_manager.set_game_over_scene(5)
self.__pause = False

# Run game loop
Expand Down
109 changes: 105 additions & 4 deletions game/scenes/game_over.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,117 @@
import pygame

from game import settings
from game.entities.crosshair import Crosshair
from game.scenes.scene import Scene
from game.states.game_states import GameStates


class GameOverScene(Scene):

def __init__(self, gsm):
def __init__(self, gsm, waves):
super().__init__(gsm)

# Crosshair
self.__crosshair = Crosshair(settings.SCREEN_WIDTH / 2,
settings.SCREEN_HEIGHT / 2,
35, 35,
settings.ui_res,
'crosshair.png')

# Fonts
font_path = str(settings.fonts_res / 'dpcomic.ttf')
self.__small_font = pygame.font.Font(font_path, 45)
self.__large_font = pygame.font.Font(font_path, 100)

# Title
self.__title = self.__large_font.render("Game Over", True, (180, 200, 210))

# Start text
self.__button_text = self.__small_font.render("Restart", True, (255, 255, 255))

# Waves text
waves_text = f"Yor record: {waves} waves."
self.__waves_text = self.__small_font.render(waves_text, True, (235, 235, 235))

# Play Button
button_press_path = str(settings.ui_res / "menu_button_press.png")
button_rel_path = str(settings.ui_res / "menu_button.png")
self.__button_props = (192, 64)
self.__button_press_img = pygame.transform.scale(pygame.image.load(button_press_path), self.__button_props)
self.__button_rel_img = pygame.transform.scale(pygame.image.load(button_rel_path), self.__button_props)
self.__button_image = self.__button_rel_img
self.__button_pressed = False

# Positions
self.__title_pos = (settings.SCREEN_WIDTH / 2 - self.__title.get_width() / 2,
settings.SCREEN_HEIGHT / 2 - self.__title.get_height() / 2 - 80)

self.__button_pos = (settings.SCREEN_WIDTH / 2 - self.__button_props[0] / 2,
settings.SCREEN_HEIGHT / 2 - self.__button_props[1] / 2)

self.__button_text_pos = (self.__button_pos[0] + self.__button_text.get_width() / 2 - 25,
self.__button_pos[1] + self.__button_text.get_height() / 2 - 15)

self.__waves_text_pos = (settings.SCREEN_WIDTH / 2 - self.__waves_text.get_width() / 2,
settings.SCREEN_HEIGHT - 50)

# Rects
self.__button_rect = self.__button_image.get_rect(topleft=self.__button_pos)
self.__button_text_rect = self.__button_text.get_rect(center=self.__button_text_pos)

# Effects
self.__ignore_mouse_clicks = False
self.__transition = False
self.__close_effect = pygame.Surface((settings.SCREEN_WIDTH, settings.SCREEN_HEIGHT))
self.__close_effect_alpha = 0
self.__close_effect.set_alpha(self.__close_effect_alpha)
self.__close_effect.fill((10, 10, 10))

def draw(self, screen):
pass
screen.blit(self.__title, self.__title_pos)
screen.blit(self.__button_image, self.__button_pos)
screen.blit(self.__button_text, self.__button_text_pos)
screen.blit(self.__waves_text, self.__waves_text_pos)
self.__crosshair.draw(screen)
if self.__transition:
screen.blit(self.__close_effect, (0, 0))

def update(self, dt):
pass
self.__crosshair.update(dt)
if self.__transition:
self.__close_effect_alpha += 10
self.__close_effect.set_alpha(self.__close_effect_alpha)
if self.__close_effect_alpha > 255 + 300:
self._gsm.set_state(GameStates.PLAY)

def handle_events(self, events):
pass
keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE] == 1:
if not self.__button_pressed:
self.__ignore_mouse_clicks = True
self.__button_image = self.__button_press_img
self.__button_pressed = True
self.__button_text_pos = (self.__button_pos[0] + self.__button_text.get_width() / 2 - 25,
self.__button_pos[1] + self.__button_text.get_height() / 2 - 8)
self.__transition = True

if not self.__ignore_mouse_clicks:
press = pygame.mouse.get_pressed()
mouse_pos = pygame.mouse.get_pos()
collude_button = self.__button_rect.collidepoint(mouse_pos) or \
self.__button_text_rect.collidepoint(mouse_pos)
if press[0]:
if collude_button and not self.__button_pressed:
self.__button_image = self.__button_press_img
self.__button_pressed = True
self.__button_text_pos = (self.__button_pos[0] + self.__button_text.get_width() / 2 - 25,
self.__button_pos[1] + self.__button_text.get_height() / 2 - 8)
else:
if self.__button_pressed:
self.__button_image = self.__button_rel_img
self.__button_pressed = False
self.__button_text_pos = (self.__button_pos[0] + self.__button_text.get_width() / 2 - 25,
self.__button_pos[1] + self.__button_text.get_height() / 2 - 15)
if collude_button:
self.__transition = True
Loading

0 comments on commit 925082f

Please sign in to comment.