-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
executable file
·168 lines (131 loc) · 4.73 KB
/
game.py
File metadata and controls
executable file
·168 lines (131 loc) · 4.73 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
import asyncio
from enum import Enum
import logging
import time
import sys
import pygame
import pygame.freetype
from pygame.locals import *
from twitchio import Channel, User, Client
import creds
from chocobo import Chocobo
from chocobosprites import ChocoboSprites, CHOCOBO_HEIGHT
class GameState(Enum):
IDLE = 1
STARTING = 2
RACING = 3
WINNER = 4
class Game:
def __init__(self, twitch):
self.screen_width = 1920
self.screen_height = CHOCOBO_HEIGHT
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
self.chocobo_sprites = ChocoboSprites()
self.font = pygame.freetype.Font("ff6.ttf", 72)
self.twitch = twitch
self.twitch_user = None
self.reset()
def reset(self):
self.state = GameState.IDLE
self.wait_time = 300
self.current_lap = 0
self.winner = None
self.prediction = None
self.chocobos = [
Chocobo(self, 0),
Chocobo(self, 2),
]
def draw_idle(self):
self.wait_time -= 1
if self.wait_time <= 0:
self.wait_time = 300
self.state = GameState.STARTING
async def draw_starting(self):
self.wait_time -= 1
text_surface, rect = self.font.render(f"Starting race in: {self.wait_time / 10}", (255, 255, 255))
self.screen.blit(text_surface, dest=(700, 15))
if self.twitch_user == None:
self.twitch_user = (await self.twitch.fetch_users(names=[creds.username]))[0]
if self.prediction == None:
self.prediction = await self.twitch_user.create_prediction(creds.token, 'Chocobo Race Results', 'yellow', 'red', 30)
if self.wait_time <= 0:
self.wait_time = 300
self.state = GameState.RACING
def draw_race(self):
for c in self.chocobos:
c.draw()
if c.lap > self.current_lap:
self.current_lap = c.lap
self.lap_height = -50
if self.current_lap > 4:
if not self.winner:
self.winner = c
else:
# It's a tie, run another lap
self.winner = None
self.lap_height += 2
text_surface, rect = self.font.render(f"Lap {self.current_lap}", (255, 255, 255))
self.screen.blit(text_surface, dest=(1400, self.lap_height))
if self.winner:
self.state = GameState.WINNER
async def draw_winner(self):
text_surface, rect = self.font.render(f"A Winner is You", (255, 255, 255))
self.screen.blit(text_surface, dest=(700, 15))
if self.prediction:
winning_index = self.chocobos.index(self.winner)
winning_outcome = self.prediction.outcomes[winning_index].outcome_id
await self.twitch_user.end_prediction(creds.token, self.prediction.prediction_id, "RESOLVED", winning_outcome)
self.prediction = None
if self.wait_time <= 0:
self.reset()
elif self.wait_time == 300:
self.winner.x = 1100
else:
self.winner.move()
self.winner.draw()
self.wait_time -= 1
async def run_game(self):
while True:
self.screen.fill((255,0, 255)) # Magenta BG
await asyncio.sleep(.1)
if self.state == GameState.IDLE:
self.draw_idle()
if self.state == GameState.STARTING:
await self.draw_starting()
elif self.state == GameState.RACING:
for c in self.chocobos:
c.move()
self.draw_race()
elif self.state == GameState.WINNER:
await self.draw_winner()
pygame.display.flip()
def pygame_event_loop():
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
break
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logging.debug("Initializing game")
pygame.init()
pygame.display.set_caption("Chocobo Racing")
loop = asyncio.get_event_loop()
twitch = Client(token=creds.token,
initial_channels=[f'#{creds.username}'],
client_secret=creds.client_secret,
loop=loop)
game = Game(twitch)
pygame_task = loop.run_in_executor(None, pygame_event_loop)
game_task = loop.create_task(game.run_game())
twitch_task = loop.create_task(twitch.run())
logging.debug("initialized")
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
pygame_task.cancel()
twitch_task.cancel()
game_task.cancel()
pygame.quit()