-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
133 lines (102 loc) · 3.61 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""CSC148 Assignment 2
=== CSC148 Winter 2020 ===
Department of Computer Science,
University of Toronto
=== Module Description ===
This file contains the Game class, which is the main class for the
Blocky game.
At the bottom of the file, there are some function that you
can call to try playing the game in several different configurations.
"""
from typing import List
import pygame
from block import generate_board
from blocky import GameData, GameState, MainState
from player import create_players
from renderer import Renderer
from settings import BOARD_SIZE
class Game:
"""A game of Blocky.
"""
# === Private Attributes ===
# _renderer:
# The object that is capable of drawing our Blocky board on the screen.
# _data:
# The data of the game that can be shared with other GameState objects.
# _state:
# The current GameState.
_renderer: Renderer
_data: GameData
_state: GameState
def __init__(self, max_depth: int,
num_human: int,
num_random: int,
smart_players: List[int]) -> None:
"""Initialize this game, as described in the Assignment 2 handout.
Precondition:
2 <= max_depth <= 5
"""
board = generate_board(max_depth, BOARD_SIZE)
players = create_players(num_human, num_random, smart_players)
self._renderer = Renderer(BOARD_SIZE)
self._data = GameData(board, players)
self._state = MainState(self._data)
def run_game(self, num_turns: int) -> None:
"""Start the main game loop and stop after num_turns.
"""
self._data.max_turns = num_turns
clock = pygame.time.Clock()
while True:
clock.tick(30)
# Process events
for e in pygame.event.get():
if e.type == pygame.QUIT:
return
else:
self._state.process_event(e)
# Update the state of the game
self._state = self._state.update()
# Render the new state of the game
self._renderer.clear()
self._state.render(self._renderer)
# Update the screen
pygame.display.flip()
def create_auto_game() -> Game:
"""Run a game with two computer players of different "difficulty".
"""
return Game(3, 0, 0, [5, 10])
def create_two_player_game() -> Game:
"""Run a game with two human players.
"""
return Game(3, 2, 0, [])
def create_solitaire_game() -> Game:
"""Run a game with one human player.
"""
return Game(3, 1, 0, [])
def create_sample_game() -> Game:
"""Run a sample game with one human player, one random player,
and one smart player.
"""
return Game(3, 1, 1, [6])
if __name__ == '__main__':
import python_ta
python_ta.check_all(config={
'allowed-io': ['run_game'],
'allowed-import-modules': [
'doctest', 'python_ta', 'random', 'typing', 'pygame', 'blocky',
'block', 'goal', 'player', 'renderer', 'settings'
],
'generated-members': 'pygame.*'
})
pygame.init()
# If you want to run the same game sequence each time, to assist with
# debugging, uncomment-out the call to random.seed.
# import random
# random.seed(1001)
# game = create_sample_game()
# game = create_auto_game()
# game = create_two_player_game()
game = create_solitaire_game()
# Run the game for 5 turns
game.run_game(5)
pygame.quit()