-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
74 lines (54 loc) · 1.88 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
import pygame
from yaml import load, dump
from pygame.rect import Rect
from game_states import menu
from game_states import timer
from game_states import selfie
from pygame.time import Clock
import sys
class Game:
FPS = 40
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768
BORDER_SIZE = 10
GAME_WIDTH = SCREEN_WIDTH - 2 * BORDER_SIZE
GAME_HEIGHT = SCREEN_HEIGHT - 2 * BORDER_SIZE
GAME_TOP = BORDER_SIZE
GAME_BOTTOM = SCREEN_HEIGHT - BORDER_SIZE
GAME_LEFT = BORDER_SIZE
GAME_RIGHT = SCREEN_HEIGHT - BORDER_SIZE
def __init__(self, border):
'''Init game state, player score, game count, etc...'''
self.border = border
self.screen = border.subsurface(Rect((Game.BORDER_SIZE, Game.BORDER_SIZE), (Game.SCREEN_WIDTH - 2 * Game.BORDER_SIZE, Game.SCREEN_HEIGHT - 2 * Game.BORDER_SIZE)))
self.state = menu.Menu(self)
self.timer = Clock()
self.font = pygame.font.SysFont('Roboto', 30)
self.title_font = pygame.font.SysFont('Roboto', 65)
self.init()
def init(self):
pass
def run(self):
self.running = True
while self.running:
self.state.run()
pygame.display.update()
self.timer.tick(Game.FPS)
def end_game(self, players=['None', 'None'], winners=[False, False]):
winner = False
if winners[0]:
winner = players[0]
elif winners[1]:
winner = players[1]
self.update_yaml(winner)
self.state = selfie.Selfie(self, players, winners)
def set_timer_state(self, callback):
self.state = timer.timer(self, 3, callback)
def update_yaml(self, winner):
if not winner:
return
stream = file('data/teams.yml', 'r')
teams = load(stream)
teams[winner] += 1
stream = file('data/teams.yml', 'w')
dump(teams, stream)