Commit dbde0b47 by BellCodeEditor

save project

parent f711a782
Showing with 64 additions and 7 deletions
import pygame import pygame
import random as r import random as r
from pygame import locals from pygame import locals
def shade(list,object):
for i in list:
if i[0]==object[0] and i[1]==object[1] and i[3]!=object[3]:
return 1
return 0
c=0
pygame.init() # 初始化 pygame.init() # 初始化
score = 0 score = 0
grid_size = 20 # 格子大小 grid_size = 20 # 格子大小
...@@ -9,8 +14,9 @@ grid_num_width = 15 # 横向格子数量 ...@@ -9,8 +14,9 @@ grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量 grid_num_height = 25 # 纵向格子数量
FPS = 30 FPS = 30
sqare=[] sqare=[]
mo=[[(120,20),(120,40)(140,40),(140,60)],[(120,20),(140,20),(160,20),(180,20)],[(120,20),(120,40),(140,20),(140,40)]] mo=[[(120,20),(120,40),(140,60),(140,40)],[(120,20),(140,20),(180,20),(160,20)],[(120,20),(120,40),(140,20),(140,40)],[(120,0),(140,20),(100,20),(120,20)],[(120,20),(100,40),(80,40),(100,20)],[(120,20),(120,40),(140,60),(120,60)]]
grass=(21,219,58) grass=(21,219,58)
peach=(255,218,185)
white=(255,255,255) white=(255,255,255)
red=(255,0,0) red=(255,0,0)
blue=(0,0,255) blue=(0,0,255)
...@@ -20,9 +26,7 @@ crimson=(220,20,60) ...@@ -20,9 +26,7 @@ crimson=(220,20,60)
fuchsia=(255,0,255) fuchsia=(255,0,255)
slate=(112,128,144) slate=(112,128,144)
aqua=(0,255,255) aqua=(0,255,255)
color=[grass,red,blue,green,orchid,crimson,fuchsia,slate,aqua] color=[grass,red,blue,green,orchid,crimson,fuchsia,slate,aqua]
for i in r.choice(mo):
# 创建窗口 # 创建窗口
screen = pygame.display.set_mode((460, 500)) screen = pygame.display.set_mode((460, 500))
pygame.display.set_caption("俄罗斯方块") pygame.display.set_caption("俄罗斯方块")
...@@ -36,14 +40,65 @@ while True: ...@@ -36,14 +40,65 @@ 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.key==locals.K_LEFT:
flag=0
for i in range(len(sqare)):
if sqare[i][3]==c:
sqare[i][0]-=20
if sqare[i][0]<=-20 or shade(sqare,sqare[i]):
flag=1
if flag==1:
for i in range(len(sqare)):
if sqare[i][3]==c:
sqare[i][0]+=20
if event.key==locals.K_RIGHT:
flag=0
for i in range(len(sqare)):
if sqare[i][3]==c:
sqare[i][0]+=20
if sqare[i][0]>=300 or shade(sqare,sqare[i]):
flag=1
if flag==1:
for i in range(len(sqare)):
if sqare[i][3]==c:
sqare[i][0]-=20
if event.key==locals.K_DOWN:
flag=0
for i in range(len(sqare)):
if sqare[i][3]==c:
sqare[i][1]+=20
if sqare[i][1]>=480 or shade(sqare,[sqare[i][0],sqare[i][1]+20,0,sqare[i][3]]):
flag=1
if flag==1:
c+=1
cc=r.choice(color)
for i in r.choice(mo):
print(i)
sqare.append([i[0],i[1],cc,c])
if event.key==locals.K_UP:
for i in range(len(sqare)-1):
if sqare[i][3]==c:
difx=sqare[i][0]-sqare[-1][0]
dify=sqare[i][1]-sqare[-1][1]
sqare[i][0]=sqare[-1][0]-dify
sqare[i][1]=sqare[-1][1]-difx
for i in range(0,500,20):
flag=1
for j in range(0,300,20):
if
if c==0:
c+=1
cc=r.choice(color)
for i in r.choice(mo):
print(i)
sqare.append([i[0],i[1],cc,c])
# 将背景图画上去 # 将背景图画上去
screen.blit(background,(0,0)) screen.blit(background,(0,0))
# 得分 # 得分
score_text=font.render(str(score),True,(0,255,200)) score_text=font.render(str(score),True,(0,255,200))
screen.blit(score_text,(350,70)) screen.blit(score_text,(350,70))
# 刷新画面 # 刷新画面
sqare.append([120,20,grass])
for i in sqare: for i in sqare:
pygame.draw.rect(screen,i[2],(i[0],i[1],20,20),0) pygame.draw.rect(screen,i[2],(i[0],i[1],20,20),0)
pygame.draw.rect(screen,white,(i[0],i[1],20,20),1) pygame.draw.rect(screen,white,(i[0],i[1],20,20),1)
......
mo=[(120,20),(120,40),(140,40),(140,60)]
\ No newline at end of file
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