-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposterGame.py
More file actions
103 lines (80 loc) · 3.44 KB
/
posterGame.py
File metadata and controls
103 lines (80 loc) · 3.44 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
import pygame
import requests
import time
import io
from urllib.request import urlopen
import logging
class MainLoop:
def __init__(self):
self.window_width = int(1080 / 3)
self.window_height = int(1960 / 3)
self.movie_id = None
self.scrn = pygame.display.set_mode((self.window_width, self.window_height))
def check_for_image(self):
try:
response = requests.get(f"http://localhost:8000/get-current-movie/{self.movie_id}")
data = response.json()
msg = data.get('msg')
if msg:
movie_id = msg.get('movie_id')
if movie_id and movie_id != self.movie_id:
logging.info("Image has changed, updating image")
self.update_image(msg)
elif not movie_id:
logging.debug("No movie selected, displaying default")
else:
logging.error("Error trying to reach server")
return
except Exception as e:
logging.error(f"Error in check_for_image: {str(e)}")
return
def update_image(self, data):
try:
self.movie_id = data['movie_id']
self.poster_path = data['poster_path']
image_str = urlopen(self.poster_path).read()
image_file = io.BytesIO(image_str)
image = pygame.image.load(image_file)
poster = pygame.transform.scale(image, (self.window_width - 10, int((self.window_height - 10) * (27 / 32))))
self.scrn.blit(poster, (5, int(self.window_height * (5 / 32))))
pygame.display.flip()
return
except Exception as e:
logging.error(f"There was an error while trying to update the image: {str(e)}")
return
def begin(self):
pygame.init()
pygame.display.set_caption('image')
twilight = pygame.image.load('static/theatername.jpg').convert()
poster = pygame.image.load('static/default.jpeg').convert()
twilight = pygame.transform.scale(twilight, (self.window_width, int(self.window_height * (5 / 32))))
poster = pygame.transform.scale(poster, (self.window_width - 10, int((self.window_height - 10) * (27 / 32))))
self.scrn.blit(twilight, (0, 0))
self.scrn.blit(poster, (5, int(self.window_height * (5 / 32))))
x = 5
y = int(self.window_height * (5 / 32))
border_colors = [(.1 * 255, .1 * 255, .1 * 255), (.3 * 255, .3 * 255, .3 * 255),
(.6 * 255, .6 * 255, .6 * 255), (1 * 255, 1 * 255, 1 * 255)]
for j in range(4):
i = 3 - j
pygame.draw.rect(self.scrn, border_colors[j], (x - (1 * i), y - (1 * i),
(self.window_width - 10) + (2 * i),
int((self.window_height - 10) * (27 / 32)) + (2 * i)),
1, border_radius=4)
pygame.display.flip()
self.main_loop()
return
def main_loop(self):
status = True
old_epoch = time.time()
while status:
if time.time() - old_epoch >= 0.5:
old_epoch = time.time()
self.check_for_image()
for event in pygame.event.get():
if event.type == pygame.QUIT:
status = False
return
if __name__ == "__main__":
MainLoop().begin()
pygame.quit()