您是否想要制作自己的俄羅斯方塊游戲?pyGame帶你飛!
安裝
使用以下命令即可安裝
pip install pyGame
API介紹
以下是Pygame庫中常用的一些API的介紹:
- pygame.init(): 初始化Pygame庫。
- pygame.display.set_mode(): 創(chuàng)建一個顯示窗口。
- pygame.display.set_caption(): 設置窗口的標題。
- pygame.Surface: Surface是Pygame庫中的一個類,表示一個圖像表面,可以用來繪制圖像和文本,以及進行圖像處理。
- pygame.draw: 這個模塊包含了許多繪制圖形的函數(shù),例如繪制直線、矩形、圓等。
- pygame.event.get(): 獲取當前的事件隊列。
- pygame.event.type: 用于判斷事件的類型,例如QUIT、KEYDOWN等。
- pygame.KEYDOWN: 表示鍵盤某個鍵被按下。
- pygame.K_UP: 表示鍵盤上的上箭頭鍵。
- pygame.K_DOWN: 表示鍵盤上的下箭頭鍵。
- pygame.K_LEFT: 表示鍵盤上的左箭頭鍵。
- pygame.K_RIGHT: 表示鍵盤上的右箭頭鍵。
- pygame.time.Clock(): 創(chuàng)建一個時鐘對象,用于控制游戲幀率。
- pygame.time.delay(): 在指定的毫秒數(shù)內(nèi)暫停游戲。
- pygame.font.SysFont(): 創(chuàng)建一個字體對象,用于繪制文本。
- pygame.mixer.Sound(): 創(chuàng)建一個聲音對象,用于播放聲音。
通過使用這些API,可以輕松地創(chuàng)建2D游戲,實現(xiàn)圖形繪制、文本顯示、聲音播放、用戶輸入等功能。需要注意的是,Pygame庫并不支持3D圖形渲染和網(wǎng)絡通信等高級功能,如果需要這些功能,可以使用其他游戲引擎或網(wǎng)絡庫。
示例
以下是一個簡單的Pygame庫的demo,用于顯示一個窗口和一個文本框:
import pygame
# 初始化Pygame庫
pygame.init()
# 創(chuàng)建窗口
win_width, win_height = 640, 480
win = pygame.display.set_mode((win_width, win_height))
# 設置窗口標題
pygame.display.set_caption("Pygame Demo")
# 創(chuàng)建字體對象
font = pygame.font.SysFont('Arial', 30)
# 創(chuàng)建文本對象
text = font.render('Hello, Pygame!', True, (255, 255, 255))
# 游戲循環(huán)
while True:
# 處理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 填充窗口背景色
win.fill((0, 0, 0))
# 將文本對象繪制到窗口上
win.blit(text, (win_width/2 - text.get_width()/2, win_height/2 - text.get_height()/2))
# 刷新窗口
pygame.display.update()
這個demo中,我們首先使用pygame.init()函數(shù)初始化Pygame庫,然后使用pygame.display.set_mode()函數(shù)創(chuàng)建一個窗口,并使用
pygame.display.set_caption()函數(shù)設置窗口的標題。
接下來,我們使用pygame.font.SysFont()函數(shù)創(chuàng)建一個字體對象,用于繪制文本。然后使用font.render()函數(shù)創(chuàng)建一個文本對象,指定要顯示的文本、是否開啟抗鋸齒和文本顏色。
在游戲循環(huán)中,我們處理事件,然后使用win.fill()函數(shù)填充窗口的背景色。接著,我們使用win.blit()函數(shù)將文本對象繪制到窗口上,并指定文本的位置。最后,使用pygame.display.update()函數(shù)刷新窗口。
運行這個demo,就可以看到一個顯示了文本的窗口。這個demo只是一個簡單的例子,但是可以幫助你了解如何使用Pygame庫的一些基本功能。
俄羅斯方塊
下面是一個使用Pygame實現(xiàn)的俄羅斯方塊游戲的示例代碼:
import pygame
import random
# 初始化 Pygame 庫
pygame.init()
# 設置游戲窗口的大小和標題
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("俄羅斯方塊")
# 設置常量
block_size = 20
play_width = 10 * block_size
play_height = 20 * block_size
top_left_x = (screen_width - play_width) // 2
top_left_y = screen_height - play_height - 30
# 定義顏色常量
white = (255, 255, 255)
black = (0, 0, 0)
gray = (128, 128, 128)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
orange = (255, 165, 0)
purple = (128, 0, 128)
colors = [gray, red, green, blue, yellow, orange, purple]
# 定義方塊形狀
s_shape = [['.....', '.....', '..00.', '.00..', '.....'],
['.....', '..0..', '..00.', '...0.', '.....']]
z_shape = [['.....', '.....', '.00..', '..00.', '.....'],
['.....', '..0..', '.00..', '.0...', '.....']]
i_shape = [['..0..', '..0..', '..0..', '..0..', '.....'],
['.....', '.....', '0000.', '.....', '.....']]
o_shape = [['.....', '.....', '.00..', '.00..', '.....']]
j_shape = [['.....', '.0...', '.000.', '.....', '.....'],
['.....', '..00.', '..0..', '..0..', '.....'],
['.....', '.....', '.000.', '...0.', '.....'],
['.....', '..0..', '..0..', '.00..', '.....']]
l_shape = [['.....', '...0.', '.000.', '.....', '.....'],
['.....', '..0..', '..0..', '..00.', '.....'],
['.....', '.....', '.000.', '.0...', '.....'],
['.....', '.00..', '..0..', '..0..', '.....']]
t_shape = [['.....', '..0..', '.000.', '.....', '.....'],
['.....', '..0..', '..00.', '..0..', '.....'],
['.....', '.....', '.000.', '..0..', '.....'],
['.....', '..0..', '.00..', '..0..', '.....']]
shapes = [s_shape, z_shape, i_shape, o_shape, j_shape, l_shape, t_shape]
# 定義方塊類
class Piece:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = colors[shapes.index(shape)]
self.rotation = 0
# 定義函數(shù):創(chuàng)建游戲界面
def create_grid(locked_positions={}):
grid = [[black for _ in range(10)] for _ in range(20)]
for i in range(len(grid)):
for j in range(len(grid[i])):
if (j, i) in locked_positions:
c = locked_positions[(j, i)]
grid[i][j] = c
return grid
# 定義函數(shù):檢查方塊是否在有效位置
def valid_space(piece, grid):
accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == black] for i in range(20)]
accepted_positions = [j for sub in accepted_positions for j in sub]
formatted = convert_shape_format(piece)
for pos in formatted:
if pos not in accepted_positions:
if pos[1] > -1:
return False
return True
# 定義函數(shù):將方塊形狀轉(zhuǎn)換成坐標形式
def convert_shape_format(piece):
positions = []
shape_format = piece.shape[piece.rotation % len(piece.shape)]
for i, line in enumerate(shape_format):
row = list(line)
for j, column in enumerate(row):
if column == '0':
positions.append((piece.x + j, piece.y + i))
for i, pos in enumerate(positions):
positions[i] = (pos[0] - 2, pos[1] - 4)
return positions
# 定義函數(shù):檢查是否有整行已填滿
def check_clear_rows(grid, locked):
inc = 0
for i in range(len(grid)-1, -1, -1):
row = grid[i]
if black not in row:
inc += 1
ind = i
for j in range(len(row)):
try:
del locked[(j, i)]
except:
continue
if inc > 0:
for key in sorted(list(locked), key=lambda x: x[1])[::-1]:
x, y = key
if y < ind:
new_key = (x, y + inc)
locked[new_key] = locked.pop(key)
return inc * 10
# 定義函數(shù):檢查是否游戲結束
def check_lost(positions):
for pos in positions:
x, y = pos
if y < 1:
return True
return False
# 定義函數(shù):繪制游戲界面
def draw_window(surface, grid, score=0):
surface.fill(black)
pygame.font.init()
font = pygame.font.SysFont('comicsans', 60)
font_small = pygame.font.SysFont('comicsans', 30)
label = font.render('俄羅斯方塊', 1, white)
surface.blit(label, (top_left_x + play_width/2 - (label.get_width()/2), 30))
score_label = font_small.render('得分:' + str(score), 1, white)
surface.blit(score_label, (top_left_x + play_width + 50, top_left_y + 20))
for i in range(len(grid)):
for j in range(len(grid[i])):
pygame.draw.rect(surface, grid[i][j], (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size), 0)
pygame.draw.rect(surface, white, (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size), 1)
pygame.draw.rect(surface, red, (top_left_x, top_left_y, play_width, play_height), 5)
# 定義函數(shù):繪制下一個方塊
def draw_next_shape(piece, surface):
font = pygame.font.SysFont('comicsans', 30)
label = font.render('下一個方塊', 1, white)
x = top_left_x + play_width + 50
y = top_left_y + 100
shape_format = piece.shape[piece.rotation % len(piece.shape)]
for i, line in enumerate(shape_format):
row = list(line)
for j, column in enumerate(row):
if column == '0':
pygame.draw.rect(surface, piece.color, (x + j*block_size, y + i*block_size, block_size, block_size), 0)
surface.blit(label, (x, y - 30))
# 定義函數(shù):繪制游戲結束界面
def draw_game_over(surface):
font = pygame.font.SysFont('comicsans', 60)
label = font.render('游戲結束', 1, white)
surface.blit(label, (top_left_x + play_width/2 - (label.get_width()/2), top_left_y + play_height/2 - (label.get_height()/2)))
def main():
# 初始化變量
locked_positions = {}
grid = create_grid(locked_positions)
change_piece = False
run = True
current_piece = Piece(5, 0, random.choice(shapes))
next_piece = Piece(5, 0, random.choice(shapes))
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.27
level_time = 0
score = 0
# 游戲循環(huán)
while run:
# 更新變量
grid = create_grid(locked_positions)
fall_time += clock.get_rawtime()
level_time += clock.get_rawtime()
clock.tick()
# 每過一定時間提高下落速度
if level_time/1000 > 5:
level_time = 0
if fall_speed > 0.12:
fall_speed -= 0.005
# 方塊下落
if fall_time/1000 > fall_speed:
fall_time = 0
current_piece.y += 1
if not valid_space(current_piece, grid) and current_piece.y > 0:
current_piece.y -= 1
change_piece = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.quit()
quit()
# 方塊左右移動
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1
if not valid_space(current_piece, grid):
current_piece.x += 1
elif event.key == pygame.K_RIGHT:
current_piece.x += 1
if not valid_space(current_piece, grid):
current_piece.x -= 1
elif event.key == pygame.K_DOWN:
current_piece.y += 1
if not valid_space(current_piece, grid):
current_piece.y -= 1
elif event.key == pygame.K_UP:
current_piece.rotation = (current_piece.rotation + 1) % len(current_piece.shape)
if not valid_space(current_piece, grid):
current_piece.rotation = (current_piece.rotation - 1) % len(current_piece.shape)
# 將當前方塊加入已鎖定方塊列表
shape_pos = convert_shape_format(current_piece)
for pos in shape_pos:
x, y = pos
if y > -1:
grid[y][x] = current_piece.color
if change_piece:
for pos in shape_pos:
p = (pos[0], pos[1])
locked_positions[p] = current_piece.color
current_piece = next_piece
next_piece = Piece(5, 0, random.choice(shapes))
change_piece = False
score += check_clear_rows(grid, locked_positions)
# 清除整行并更新得分
draw_window(screen, grid, score)
draw_next_shape(next_piece, screen)
pygame.display.update()
if check_lost(locked_positions):
draw_game_over(screen)
pygame.time.delay(1500)
run = False
pygame.display.quit()
quit()
if __name__ == '__main__':
main()