python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲
本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
import pygame,sys,random,time
all_block = [[[0,0],[0,-1],[0,1],[0,2]],
? ? ? ? [[0,0],[0,1],[1,1],[1,0]],
? ? ? ? [[0,0],[0,-1],[-1,0],[-1,1]],?
? ? ? ? [[0,0],[0,1],[-1,-1],[-1,0]], ?
? ? ? ? [[0,0],[0,1],[1,0],[0,-1]],?
? ? ? ? [[0,0],[1,0],[-1,0],[1,-1]],
? ? ? ? [[0,0],[1,0],[-1,0],[1,1]]]?
background = [[0 for column in range(0,10)] for row in range(0,22)]
background[0] = [1 for column in range(0,10)]
select_block = list(random.choice(all_block))
block_initial_position = [21,5]?
times = 0
score = [0] ?
gameover = []?
press = False
pygame.init()
screen = pygame.display.set_mode((250,500))
title = pygame.display.set_caption("俄羅斯方塊")
#下落、位置、數(shù)組檢測(cè)、得分、屏幕信息
def block_move_down():
? ? y_drop=block_initial_position[0]?
? ? x_move=block_initial_position[1]
? ? y_drop-=1?
? ? for row,column in select_block:
? ? ? ? row+=y_drop
? ? ? ? column+=x_move
? ? ? ? if background[row][column]==1:
? ? ? ? ? ? break?
? ? else:
? ? ? ? block_initial_position.clear()
? ? ? ? block_initial_position.extend([y_drop,x_move])
? ? ? ? return
? ? y_drop,x_move=block_initial_position
? ? for row,column in select_block:
? ? ? ? background[y_drop+row][x_move+column]=1
? ? complete_row=[]?
? ? for row in range(1,21):
? ? ? ? if 0 not in background[row]:
? ? ? ? ? ? complete_row.append(row)
? ? complete_row.sort(reverse=True)
? ? for row in complete_row:
? ? ? ? background.pop(row)
? ? ? ? background.append([0 for column in range(0,10)])
? ? score[0]+=len(complete_row)
? ? pygame.display.set_caption(str(score[0])+'分')
? ? select_block.clear() ?
? ? select_block.extend(list(random.choice(all_block)))?
? ? block_initial_position.clear() ?
? ? block_initial_position.extend([20,5])
? ? y_drop,x_move=block_initial_position
? ? for row,column in select_block:
? ? ? ? row+=y_drop
? ? ? ? column+=x_move
? ? ? ? if background[row][column]:
? ? ? ? ? ? gameover.append(1)
#方塊設(shè)置、變化、背景改變
def new_draw():
? ? y_drop,x_move=block_initial_position
? ? for row,column in select_block:
? ? ? ? row+=y_drop
? ? ? ? column+=x_move?
? ? ? ? pygame.draw.rect(screen,(255,165,0),(column*25,500-row*25,23,23))
? ? for row in range(0,20):
? ? ? ? for column in range(0,10):
? ? ? ? ? ? bottom_block=background[row][column]
? ? ? ? ? ? if bottom_block:
? ? ? ? ? ? ? ? pygame.draw.rect(screen,(0,0,255),(column*25,500-row*25,23,23))
#方塊的移動(dòng),防止出界,碰撞
def move_left_right(n):
? ? y_drop,x_move=block_initial_position?
? ? x_move+=n
? ? for row,column in select_block:
? ? ? ? row+=y_drop
? ? ? ? column+=x_move
? ? ? ? if column<0 or column>9 or background[row][column]:
? ? ? ? ? ? break
? ? else:
? ? ? ? block_initial_position.clear()
? ? ? ? block_initial_position.extend([y_drop,x_move])
#旋轉(zhuǎn),位置都進(jìn)行變化
def rotate():
? ? y_drop,x_move=block_initial_position
? ? rotating_position=[(-column,row)for row,column in select_block]
? ? for row,column in rotating_position:
? ? ? ? row+=y_drop
? ? ? ? column+=x_move
? ? ? ? if column<0 or column>9 or background[row][column]:
? ? ? ? ? ? break
? ? else:
? ? ? ? select_block.clear()
? ? ? ? select_block.extend(rotating_position)
while True:
? ? screen.fill((255,255,255))
? ? for event in pygame.event.get():
? ? ? ? if event.type==pygame.QUIT:
? ? ? ? ? ? sys.exit()
? ? ? ? elif event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
? ? ? ? ? ? move_left_right(-1)
? ? ? ? elif event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
? ? ? ? ? ? move_left_right(1)
? ? ? ? elif event.type==pygame.KEYDOWN and event.key==pygame.K_UP:
? ? ? ? ? ? rotate()
? ? ? ? elif event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN:
? ? ? ? ? ? press=True
? ? ? ? elif event.type==pygame.KEYUP and event.key==pygame.K_DOWN:
? ? ? ? ? ? press=False
? ? if press:
? ? ? ? times+=10
? ? if times>=50:
? ? ? ? block_move_down()
? ? ? ? times=0
? ? else:
? ? ? ? times+=1
? ? if gameover:
? ? ? ? sys.exit()
? ? new_draw()
? ? pygame.time.Clock().tick(200)
? ? pygame.display.flip()效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python實(shí)現(xiàn)簡(jiǎn)單的俄羅斯方塊游戲
- python游戲?qū)崙?zhàn)項(xiàng)目之俄羅斯方塊的魅力
- python實(shí)現(xiàn)俄羅斯方塊小游戲
- python實(shí)現(xiàn)俄羅斯方塊游戲(改進(jìn)版)
- Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊
- python實(shí)現(xiàn)俄羅斯方塊游戲
- python和pygame實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲
- Python使用pygame模塊編寫(xiě)俄羅斯方塊游戲的代碼實(shí)例
- Python完整實(shí)現(xiàn)俄羅斯方塊游戲全解
相關(guān)文章
Python使用moviepy讀取字幕srt文件報(bào)錯(cuò)的解決方法詳解
這篇文章主要為大家詳細(xì)介紹了Python使用moviepy讀取字幕srt文件報(bào)錯(cuò)‘gbk‘?codec?can‘t?decode的兩種解決辦法,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
解決Django 在ForeignKey中出現(xiàn) non-nullable field錯(cuò)誤的問(wèn)題
python中強(qiáng)制關(guān)閉線程與協(xié)程與進(jìn)程方法
TensorFlow實(shí)現(xiàn)批量歸一化操作的示例
解決python爬蟲(chóng)中有中文的url問(wèn)題

