-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_scene.py
112 lines (93 loc) · 3.81 KB
/
game_scene.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
import pygame
import AI_player
import leaderboard
from Figure_and_Tetris import Tetris,colors
def game_start(name,is_AI):
# Initialize the game engine
pygame.init()
# Define colors for figure
BLACK=(0,0,0)
WHITE=(255,255,255)
GRAY=(128,128,128)
size=(400,500)
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Tetris Games With AI")
# Loop till the player clicks the close button or press ESC
done=False
time_clock=pygame.time.Clock()
fps=25
#screen size 20*10 blocks
game=Tetris(20,10)
counter=0
pressing_down=False
while not done:
if game.figure is None:
game.new_figure()
counter+=1
if counter>9999999:
counter=0
if counter%(fps//game.level//2)==0 or pressing_down:
if game.state=="start":
game.go_down()
# for event in pygame.event.get():
if is_AI ==True:
AI_action = AI_player.run_ai(game.field, game.figure, game.width, game.height)
elif is_AI==False:
AI_action = []
for event in list(pygame.event.get())+AI_action:
if event.type==pygame.QUIT:
done=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
game.rotate()
if event.key==pygame.K_DOWN:
pressing_down=True
if event.key==pygame.K_LEFT:
game.go_side(-1)
if event.key==pygame.K_RIGHT:
game.go_side(1)
if event.key==pygame.K_SPACE:
game.go_space()
if event.key==pygame.K_r:
game.__init__(20,10)
if event.key==pygame.K_ESCAPE:
done=True
if event.type==pygame.KEYUP:
if event.key==pygame.K_DOWN:
pressing_down=False
screen.fill(WHITE)
for i in range(game.height):
for j in range(game.width):
pygame.draw.rect(screen,GRAY,[game.x+game.zoom*j,game.y+game.zoom*i,
game.zoom,game.zoom],1)
if game.field[i][j]>0:
pygame.draw.rect(screen,colors[game.field[i][j]],
[game.x+game.zoom*j+1,game.y+game.zoom*i+1,
game.zoom-2,game.zoom-1])
if game.figure is not None:
for i in range(4):
for j in range(4):
p=i*4+j
if p in game.figure.image():
pygame.draw.rect(screen,colors[game.figure.color],
[game.x+game.zoom*(j+game.figure.x)+1,
game.y+game.zoom*(i+game.figure.y)+1,
game.zoom-2,game.zoom-2])
font = pygame.font.SysFont('Calibri',25,True,False)
font1=pygame.font.SysFont('Calibri',30,True,False)
text_score=font.render("Score: "+str(game.score),True,BLACK)
text_name = font.render("Name: " + str(name), True, BLACK)
text_game_over=font1.render("Game Over",True,(245,125,0))
text_game_over1=font1.render("Press R to restart",True,(255,215,0))
text_game_over2 = font1.render("Press ESC to quit", True, (255, 215, 0))
screen.blit(text_score, [0,0])
screen.blit(text_name, [0,20])
if game.state=="gameover":
screen.blit(text_game_over,[20,200])
screen.blit(text_game_over1,[25,265])
screen.blit(text_game_over2, [25, 330])
pygame.display.flip()
time_clock.tick(fps)
print(name,game.score)
leaderboard.r_w_leaderboard(screen,font,font_text=None,rw="w",name=name,score=game.score)
return name,game.score