-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
151 lines (126 loc) · 5.12 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pygame
import sys
import constants as cst
import block
import forms
import piece_constructor
import time
import robot as bot
from threading import Thread
def matrix(n,m):
M=[]
for i in range(n):
M.append([])
for j in range(m):
if(i==0):
M[i].append(0)
else:
M[i].append(None)
return M
class Tetris(Thread):
def __init__(self):
super(Tetris,self).__init__()
self.setName("Tetris game Thread")
self.screen = pygame.display.set_mode(cst.SIZE)
self.background = pygame.Surface(self.screen.get_size())
self.background.fill(cst.GRAY)
pygame.display.set_caption("Tetrix")
self.foreground = pygame.Surface(cst.BOARD_SIZE)
self.foreground.fill(cst.BLACK)
self.blocks = matrix(cst.BLOCK_WIDTH+2, cst.BLOCK_HEIGHT+2)
self.displayer=pygame.Surface(cst.DISPLAYER_SIZE)
self.displayer.fill(cst.GRAY)
self.ON=True
self.generator=piece_constructor.PieceConstructor(self.displayer,self.blocks)
self.time_next=time.time()
self.Levels=0
self.currentForm=self.generator.get_piece()([5,1],self.foreground,self.blocks)
self.didlock = True
if cst.BOT_PLAY == True:
self.robot = bot.Robot(self.foreground,self.blocks,self.currentForm,self.generator)
def draw(self):
self.screen.blit(self.background,(0,0))
self.screen.blit(self.foreground,(cst.MARGIN,0))
self.screen.blit(self.displayer,(cst.MARGIN+cst.BOARD_WIDTH,0))
pygame.display.flip()
def lock(self):
if not self.currentForm.lock():
return False
for i in range (1,cst.BLOCK_HEIGHT+1):
if(self.blocks[0][i]==cst.BLOCK_WIDTH):
self.Levels+=1
print("Score:"+str(self.Levels))
self.blocks[0][i]=0
for j in range (1,cst.BLOCK_WIDTH+1):
self.blocks[j][i].delete()
self.blocks[j][i]=None
for j in range(i-1,0,-1):
self.blocks[0][j+1]=self.blocks[0][j]
for k in range(1,cst.BLOCK_WIDTH+1):
if self.blocks[k][j]!=None:
self.blocks[k][j].moveWith(0,1)
self.blocks[k][j+1]=self.blocks[k][j]
else:
self.blocks[k][j]=None
self.blocks[k][j]=None
self.currentForm=self.generator.get_piece()([5,1],self.foreground,self.blocks)
self.time_next=time.time()+cst.TIME_MAX
return True
def run(self):
self.draw()
while self.ON:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
self.ON=False
break;
if cst.BOT_PLAY == True:
if self.didlock:
self.didlock = False
self.robot.update(self.blocks,self.currentForm,self.generator)
self.robot.play()
else:
if(event.type==pygame.KEYDOWN):
#ESC = exit()
if(event.key==27):
self.ON=False
break;
#DOWN arrow down
if(event.key==274):
self.currentForm.moveWith(0,1)
#RIGHT arrow right
if(event.key==275):
self.currentForm.moveWith(1,0)
#LEFT arrow left
if(event.key==276):
self.currentForm.moveWith(-1,0)
#ROTATE arrow up
if(event.key==273):
self.time_next=min(time.time()+cst.TIME_MAX,self.time_next+cst.TIME_ADD)
self.currentForm.rotate()
#UP w (DEBUGING FEATURE NOT BUG)
if(event.key==119):
self.currentForm.moveWith(0,-1)
if(event.key==32):
while(self.currentForm.moveWith(0,1)):
None
if not self.lock():
#GAME OVER
self.ON=False
print("GAME OVER")
#print (event.key)
if(cst.GRAVITY and time.time()>=self.time_next):
self.didlock=True
self.time_next=time.time()+cst.TIME_MAX
if not self.currentForm.moveWith(0,1):#TRUE: a mutat
if not self.lock():
#GAME OVER
self.ON=False
print("GAME OVER")
self.draw()
if cst.BOT_PLAY== True:
pygame.event.post(pygame.event.Event(2))
def main():
game = Tetris()
game.run()
if(__name__=="__main__"):
main()