my_Tetris.py
2.09 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
from operator import truediv
import random
import time
import pygame
from pygame import locals
pygame.init() # 初始化
def main():
willInit = True
score = 0
grid_size = 20 # 格子大小
grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量
FPS = 60
frame = 0
willFall = True
willCheck = True
contents = [0, random.randint(1, 15)]
# 创建窗口
screen = pygame.display.set_mode((460, 500))
pygame.display.set_caption("俄罗斯方块")
clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入素材
background = pygame.image.load('bg.png')
font = pygame.font.Font('STKAITI.TTF', 60) # 字体
while True:
willFall = True
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_a and contents[1] > 1:
contents[1] -= 1
willFall = False
elif event.key == locals.K_d and contents[1] < 15:
contents[1] += 1
willFall = False
if frame % FPS == 0 and frame != 0:
if willInit:
startTime = time.time()
willInit = False
if time.time() - 0.5 > startTime:
frame = 0
print(str(time.time()) + ' ' + str(startTime))
continue
else:
willInit = True
if willFall:
contents[0] += 1 / FPS
frame += 1
# 将背景图画上去
screen.blit(background, (0, 0))
pygame.draw.rect(screen, (52, 235, 140), ((contents[1] - 1) * 20, (contents[0] - 1) * 20, 20, 20), 0)
pygame.draw.rect(screen, (52, 201, 235), ((contents[1] - 1) * 20, (contents[0] - 1) * 20, 20, 20), 1)
# 得分
score_str = font.render(str(score), True, (50, 168, 82))
screen.blit(score_str, (350, 70))
# 刷新画面
pygame.display.update()
clock.tick(FPS)
main()