-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
86 lines (70 loc) · 2.67 KB
/
main.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
import os
import pygame
import player
from constants import fps, local_port
from display import Display
from network import Network
from packet import Packet, PacketType
from tetris import Tetris
def run():
# Initialize the game engine
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_caption("Multris")
network = Network(1 - player.player, "Ramiz") # Other player is 1 - player
network.run()
network.discover()
game = Tetris(network)
display = Display()
# Loop until the user clicks the close button.
pressing_down = [False, False]
done = False
counter = 0
while not done:
counter += 1
if counter > 100000:
counter = 0
for ix in range(2):
if game.figures[ix] is None:
game.new_figure(ix)
if (counter % (fps // game.level // 2) == 0 and ix == player.player) or pressing_down[ix]:
if game.state == "start":
game.go_down(ix)
game.send_field()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if game.state == "start":
game.rotate(player.player)
if event.key == pygame.K_DOWN:
pressing_down[player.player] = True
if event.key == pygame.K_LEFT:
if game.state == "start":
game.go_side(-1, player.player)
if event.key == pygame.K_RIGHT:
if game.state == "start":
game.go_side(1, player.player)
if event.key == pygame.K_SPACE:
if game.state == "start":
game.go_space(player.player)
if event.key == pygame.K_ESCAPE:
game.__init__(network)
game.new_figure(player.player)
if event.key == pygame.K_r:
game.state = "ready"
game.send_state()
if game.remote_state == "ready":
game.state = "start"
game.send_state()
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
pressing_down[player.player] = False
display.display(game.score, game.state, game.figures, game.field)
clock.tick(fps)
for i in range(os.cpu_count() + 4):
network.send_udp_packet(Packet(PacketType.QUIT), (network.local_ip, local_port))
pygame.quit()
if __name__ == '__main__':
run()