Commit 75ac3cba by BellCodeEditor

save project

parent fd017c11
Showing with 81 additions and 65 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import os import random
pygame.init() # 初始化 pygame.init() # 初始化
score = 0
grid_size = 20 # 格子大小
grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量
FPS = 30
# 创建窗口 # 创建窗口
screen = pygame.display.set_mode((640, 480)) screen = pygame.display.set_mode((460, 500))
# 载入图片、资源 pygame.display.set_caption("俄罗斯方块")
bg_img = pygame.image.load('background.png') # 背景图 clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
play_img = pygame.image.load('play.png') # 播放按钮 # 载入素材
stop_img = pygame.image.load('stop.png') # 暂停按钮 background = pygame.image.load('bg.png')
last_img = pygame.image.load('last.png') # 上一曲按钮 font = pygame.font.Font('STKAITI.TTF', 60) # 字体
next_img = pygame.image.load('next.png') # 下一曲按钮
logo_img = pygame.image.load('logo.png') # 下一曲按钮 # 俄罗斯方块所有形状
O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
I = [[(0, -1), (0, 0), (0, 1), (0, 2)],
[(-1, 0), (0, 0), (1, 0), (2, 0)]]
Z = [[(0, -1), (0, 0), (1, 0), (1, 1)],
[(-1, 0), (0, 0), (0, -1), (1, -1)]]
S = [[(-1, 0), (0, 0), (0, 1), (1, 1)],
[(1, -1), (1, 0), (0, 0), (0, 1)]]
T = [[(0, -1), (0, 0), (0, 1), (-1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, 1)],
[(0, -1), (0, 0), (0, 1), (1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, -1)]]
J = [[(-1, 0), (0, 0), (1, 0), (1, -1)],
[(0, -1), (0, 0), (0, 1), (-1, -1)],
[(-1, 0), (0, 0), (1, 0), (-1, 1)],
[(0, -1), (0, 0), (0, 1), (1, 1)]]
L = [[(-1, 0), (0, 0), (1, 0), (1, 1)],
[(0, -1), (0, 0), (0, 1), (1, -1)],
[(-1, 0), (0, 0), (1, 0), (-1, -1)],
[(0, -1), (0, 0), (0, 1), (-1, 1)]]
shape_list = [I, J, L, O, S, T, Z] # 7种类型俄罗斯方块
# 一些RGB颜色
cube_colors = [
(204, 153, 153), (102, 102, 153),(153, 0, 102),
(255, 204, 0), (204, 0, 51),(255, 0, 51), (0, 102, 153),
(153, 0, 51), (204, 255, 102), (255, 153, 0)]
# 载入音乐 center = [2, 8] # 第2行第8列
m=[] current_shape=random.choice(shape_list)
path='C:\\Users\\Administrator\\Desktop\\test' index=random.randint(0,len(current_shape)-1)
filelist=os.listdir(path) shape=current_shape[index]
for i in filelist: color=random.choice(cube_colors)
if i[-4:]=='.wav'or i[-4:]=='.ogg':
m.append(i) def check(center):
num=-1 for cube in shape:
volume = 0.2 cube=[cube[0]+center[0],cube[1]+center[1]]
pygame.mixer.music.set_volume(volume) # 初始播放音量 if cube[0]<1 or cube[1]<1 or cube[0]>grid_num_height or cube[1]>grid_num_width:
click = 0 return False
play_button = stop_img
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
exit() exit()
# 按键,控制声音大小
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if event.key == locals.K_UP: if event.key == locals.K_RIGHT: # 向右
volume += 0.1 center[1] = center[1]+1
if volume > 1: if check(center)==False:
volume = 1 center[1] -= 1
pygame.mixer.music.set_volume(volume) elif event.key == locals.K_LEFT: # 向左
if event.key == locals.K_DOWN: center[1] = center[1]-1
volume -= 0.1 if check(center)==False:
if volume < 0: center[1] += 1
volume = 0 elif event.key == locals.K_DOWN: # 向下
pygame.mixer.music.set_volume(volume) center[0]=center[0]+1
if check(center) ==False:
# 按下鼠标 center[0] -= 1
if event.type==locals.MOUSEBUTTONDOWN:
if event.button==1:
x,y=event.pos
if x>270 and x<370 and y>350 and y<450:
click+=1
if click%2==0:
play_button=stop_img
pygame.mixer.music.unpause()
else:
play_button=play_img
pygame.mixer.music.pause()
if x>120 and x<220 and y>350 and y<400:
num-=1
if num<0:
num=len(m)-1
pygame.mixer.music.load(path+"\\"+m[num])
pygame.mixer.music.play()
play_button=stop_img
click=0
if pygame.mixer.music.get_busy() == False:
num+=1
if num>len(m)-1:
num=0
pygame.mixer.music.load(path+"\\"+m[num])
pygame.mixer.music.play()
# 绘制画面 # 将背景图画上去
screen.blit(bg_img, (0, 0)) # 填充背景 screen.blit(background, (0, 0))
screen.blit(play_button, (270, 330)) # 暂停按钮 # 计算出所有小方块的行、列位置
screen.blit(logo_img, (170, 60)) # 中间logo图 current_pos = []
screen.blit(last_img, (120, 350)) # 上一曲 for cube in shape:
screen.blit(next_img, (420, 350)) # 下一曲 pos = (cube[0] + center[0], cube[1] + center[1])
current_pos.append(pos)
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
for cube in current_pos:
pygame.draw.rect(screen, color,
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0)
pygame.draw.rect(screen, (255, 255, 255),
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1)
# 得分
text_surface = font.render(str(score), True, (0, 0, 0))
screen.blit(text_surface, (350,70))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
clock.tick(FPS)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment