代碼圖:
import pygame, random pygame.init() # 游戲界面參數(shù) width = 300 height = 600 surface = pygame.display.set_mode((width, height)) # 顏色定義 black = (0, 0, 0) white = (255, 255, 255) red = (200, 0, 0) green = (0, 200, 0) blue = (0, 0, 200) # 俄羅斯方塊參數(shù) block_size = 20 grid_width = width // block_size grid_height = height // block_size grid = [[0] * grid_width for _ in range(grid_height)] # 方塊形狀定義 shapes = [ [[1, 1, 1], [0, 1, 0]], [[0, 2, 2], [2, 2, 0]], [[3, 3, 0], [0, 3, 3]], [[4, 0, 0], [4, 4, 4]], [[0, 0, 5], [5, 5, 5]], [[6, 6], [6, 6]], [[7, 7, 7, 7]] ] # 方塊隨機(jī)生成函數(shù) def new_block(): shape = random.choice(shapes) block = {'shape': shape, 'x': grid_width // 2 - len(shape[0]) // 2, 'y': 0, 'color': random.choice([red, green, blue])} return block # 方塊繪制函數(shù) def draw_block(x, y, color): pygame.draw.rect(surface, color, (x * block_size, y * block_size, block_size, block_size)) pygame.draw.rect(surface, black, (x * block_size, y * block_size, block_size, block_size), 1) # 方塊下落函數(shù) def drop(block): block['y'] += 1 if collision(block): block['y'] -= 1 place(block) block = new_block() return block # 方塊移動函數(shù) def move(block, dx): block['x'] += dx if collision(block): block['x'] -= dx return block # 方塊旋轉(zhuǎn)函數(shù) def rotate(block): shape = block['shape'] block['shape'] = [[shape[y][x] for y in range(len(shape))] for x in range(len(shape[0]) - 1, -1, -1)] if collision(block): block['shape'] = shape return block # 方塊碰撞檢測函數(shù) def collision(block): shape = block['shape'] for y, row in enumerate(shape): for x, cell in enumerate(row): if cell and (block['y'] + y >= grid_height or block['x'] + x < 0 or block['x'] + x >= grid_width or grid[block['y'] + y][block['x'] + x]): return True return False # 方塊存放函數(shù) def place(block): shape = block['shape'] for y, row in enumerate(shape): for x, cell in enumerate(row): if cell: grid[block['y'] + y][block['x'] + x] = block['color'] # 行滿檢測函數(shù) def full_rows(): rows = [] for y, row in enumerate(grid): if 0 not in row: rows.append(y) return rows # 行刪除函數(shù) def remove_rows(rows): rows.sort(reverse=True) for row in rows: del grid[row] grid.insert(0, [0] * grid_width) # 游戲循環(huán) block = new_block() clock = pygame.time.Clock() while True: surface.fill(white) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: block = move(block, -1) elif event.key == pygame.K_RIGHT: block = move(block, 1) elif event.key == pygame.K_UP: block = rotate(block) elif event.key == pygame.K_DOWN: block = drop(block) block = drop(block) for y, row in enumerate(grid): for x, cell in enumerate(row): if cell: draw_block(x, y, cell) for y, row in enumerate(block['shape']): for x, cell in enumerate(row): if cell: draw_block(block['x'] + x, block['y'] + y, block['color']) rows = full_rows() if rows: remove_rows(rows) pygame.display.update() clock.tick(10)
結(jié)果圖:文章來源:http://www.zghlxwxcb.cn/news/detail-762299.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-762299.html
到了這里,關(guān)于俄羅斯方塊小游戲開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!