python實現簡單俄羅斯方塊遊戲

本文實例為大傢分享瞭python實現簡單俄羅斯方塊遊戲的具體代碼,供大傢參考,具體內容如下

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("俄羅斯方塊")

#下落、位置、數組檢測、得分、屏幕信息
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)

#方塊設置、變化、背景改變
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))

#方塊的移動,防止出界,碰撞
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])


#旋轉,位置都進行變化
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()

效果:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: