Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/main.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/settings.cpython-313.pyc
Binary file not shown.
60 changes: 57 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(self):
self.playing = False
self.pause = False
self.current_level = None
self.lost = False
self.quit_level = False
self.time = 0
self.command_pressed = False
self.current_path = os.path.dirname(__file__)
Expand Down Expand Up @@ -136,6 +138,8 @@ def level_screen(self):
def new(self):
self.background_music.stop()
self.background_music.play(-1)
self.lost = False
self.quit_level = False

self.current_column_number = 0
self.current_shift = 0
Expand Down Expand Up @@ -227,6 +231,7 @@ def update(self):
self.player.rect.y = -200
else:
self.playing = False
self.lost = True
self.die_sound.play()

if self.player.rect.x < 0:
Expand All @@ -237,6 +242,7 @@ def update(self):
if pygame.mouse.get_pressed()[0] == 1:
if WIDTH - 200 < pos[0] < WIDTH - 200 + 180 and 20 < pos[1] < 20 + 60:
self.playing = False
self.quit_level = True
self.button_sound.play()

def events(self):
Expand Down Expand Up @@ -298,6 +304,28 @@ def wait(self, wait_functions):
if function() is True:
return i

def lose_screen(self):
if self.running is False:
return

self.screen.fill(BLACK)
self.create_text("GAME OVER", 60, RED, WIDTH / 2, 150)
self.create_text("Score: " + str(self.score), 40, COIN_COLOR, WIDTH / 2, 250)

retry_button = MenuButton(self, WIDTH / 2 - 150, HEIGHT / 2 + 50, self.startbutton_image)
menu_button = MenuButton(self, WIDTH / 2 + 150, HEIGHT / 2 + 50, self.menubutton_image)

self.create_text("RETRY", 30, WHITE, WIDTH / 2 - 150, HEIGHT / 2 + 120)
self.create_text("MENU", 30, WHITE, WIDTH / 2 + 150, HEIGHT / 2 + 120)

pygame.display.flip()
choice = self.wait([retry_button.check_click, menu_button.check_click])

if choice == 0:
return "retry"
elif choice == 1:
return "menu"

def end_screen(self):
if self.running is False:
return
Expand All @@ -321,9 +349,35 @@ def end_screen(self):
if __name__ == "__main__":
game = Game()
game.start_screen()

while game.running:
game.new()
game.run()
game.end_screen()
if game.current_level is None:
game.level_screen()
if not game.running:
break

while True:
game.new()
game.run()

if game.quit_level:
game.current_level = None
break

elif game.lost:
choice = game.lose_screen()

if choice == "retry":
continue

elif choice == "menu":
game.current_level = None
break

else:
game.end_screen()
game.current_level = None
break

game.save_highscore()
pygame.quit()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
90 changes: 90 additions & 0 deletions test_game_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import unittest
import sys
import os

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from unittest.mock import MagicMock, patch, call


class TestGameFlow(unittest.TestCase):

@patch('pygame.init')
@patch('pygame.font.init')
@patch('pygame.mixer.init')
@patch('pygame.display.set_icon')
@patch('pygame.display.set_caption')
@patch('pygame.display.set_mode')
@patch('pygame.time.Clock')
@patch('pygame.image.load')
@patch('pygame.mixer.Sound')
def setUp(self, mock_sound, mock_image_load, mock_clock,
mock_set_mode, mock_set_caption, mock_set_icon,
mock_mixer_init, mock_font_init, mock_pygame_init):
mock_image_load.return_value = MagicMock()
mock_sound.return_value = MagicMock()

with patch('main.Game.load_levels'):
with patch('main.Game.load_highscores'):
from main import Game
self.Game = Game

game = object.__new__(Game)
game.levels = [{"name": "Level 1", "maxcoins": 10, "array": []}]
game.highscores = [0]
game.running = True
self.game_instance = game

def test_new_method_resets_all_states(self):
game = self.game_instance

game.background_music = MagicMock()

game.lost = True
game.quit_level = True
game.current_column_number = 100
game.current_shift = 500
game.score = 50
game.last_background_x_pos = 1000

with patch('main.Player') as mock_player:
mock_player.return_value = MagicMock()
with patch('pygame.sprite.Group') as mock_group:
mock_group.return_value = MagicMock()

self.Game.new(game)

self.assertFalse(game.lost)
self.assertFalse(game.quit_level)
self.assertEqual(game.current_column_number, 0)
self.assertEqual(game.current_shift, 0)
self.assertEqual(game.score, 0)
self.assertEqual(game.last_background_x_pos, 100)

def test_player_initialization_resets_state(self):
with patch('settings.GRAVITY', 1.2):
from segway_jump_sprites.player import Player

mock_game = MagicMock()
mock_game.character_left_walking_1_image = MagicMock()
mock_game.character_left_walking_2_image = MagicMock()
mock_game.character_left_walking_3_image = MagicMock()
mock_game.character_jump_1_image = MagicMock()
mock_game.character_jump_2_image = MagicMock()

with patch('pygame.mask.from_surface', return_value=MagicMock()):
with patch('pygame.transform.flip', return_value=MagicMock()):
player = Player(mock_game)

self.assertEqual(player.jumpboost, 0)
self.assertEqual(player.jumpbooston, 0)
self.assertEqual(player.speeddrop, 0)
self.assertEqual(player.speeddropon, 0)
self.assertEqual(player.rect.x, 120)
self.assertEqual(player.rect.y, -120)
self.assertEqual(player.speed.x, 0)
self.assertEqual(player.speed.y, 0)


if __name__ == '__main__':
unittest.main(verbosity=2)