就在程序里加了个多进程碰撞检测功能
import pygame
import random
from multiprocessing import Process, Pool
# 基础设置
# 屏幕高度
SCREEN_HEIGHT = 480
# 屏幕宽度
SCREEN_WIDTH = 600
# 小方格大小
GRID_SIZE = 20
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# 初始化 pygame
pygame.init()
# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")
# 游戏时钟
clock = pygame.time.Clock()
# 贪吃蛇类
class Snake:
def __init__(self):
self.positions = [(100, 100)]
self.direction = (1, 0)
self.is_move = False
def move(self, food_pos):
cur = self.positions[0]
x, y = cur
dx, dy = self.direction
if self.is_move:
new = ((x + dx * GRID_SIZE) % SCREEN_WIDTH, (y + dy * GRID_SIZE) % SCREEN_HEIGHT)
else:
new = (x, y)
self.is_move = False
self.positions[0] = new
# fixme 这里本来只是想并行执行碰撞检测功能,为什么会打开多个游戏窗口?
pool = Pool(10)
for i in range(10):
pool.apply_async(self.multi_process, args=(i))
pool.close()
pool.join()
def change_direction(self, direction):
self.direction = direction
def multi_process(self, i):
print("snake multi_process, i: " + str(i))
# 食物类
class Food:
def __init__(self):
self.position = (random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE,
random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE)
def generate_new(self):
while True:
new_pos = (random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE,
random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE)
if new_pos not in snake.positions:
self.position = new_pos
break
# 游戏主循环
def game_loop():
global snake, food
snake = Snake()
food = Food()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction((0, -1))
snake.is_move = True
elif event.key == pygame.K_DOWN:
snake.change_direction((0, 1))
snake.is_move = True
elif event.key == pygame.K_LEFT:
snake.change_direction((-1, 0))
snake.is_move = True
elif event.key == pygame.K_RIGHT:
snake.change_direction((1, 0))
snake.is_move = True
snake.move(food.position)
if snake.positions[0] == food.position:
food.generate_new()
screen.fill(BLACK)
for pos in snake.positions:
pygame.draw.rect(screen, GREEN, [pos[0], pos[1], GRID_SIZE, GRID_SIZE])
pygame.draw.rect(screen, WHITE, [food.position[0], food.position[1], GRID_SIZE, GRID_SIZE])
pygame.display.flip()
clock.tick(10)
pygame.quit()
if __name__ == "__main__":
game_loop()
import pygame
import random
from multiprocessing import Process, Pool
# 基础设置
# 屏幕高度
SCREEN_HEIGHT = 480
# 屏幕宽度
SCREEN_WIDTH = 600
# 小方格大小
GRID_SIZE = 20
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# 初始化 pygame
pygame.init()
# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")
# 游戏时钟
clock = pygame.time.Clock()
# 贪吃蛇类
class Snake:
def __init__(self):
self.positions = [(100, 100)]
self.direction = (1, 0)
self.is_move = False
def move(self, food_pos):
cur = self.positions[0]
x, y = cur
dx, dy = self.direction
if self.is_move:
new = ((x + dx * GRID_SIZE) % SCREEN_WIDTH, (y + dy * GRID_SIZE) % SCREEN_HEIGHT)
else:
new = (x, y)
self.is_move = False
self.positions[0] = new
# fixme 这里本来只是想并行执行碰撞检测功能,为什么会打开多个游戏窗口?
pool = Pool(10)
for i in range(10):
pool.apply_async(self.multi_process, args=(i))
pool.close()
pool.join()
def change_direction(self, direction):
self.direction = direction
def multi_process(self, i):
print("snake multi_process, i: " + str(i))
# 食物类
class Food:
def __init__(self):
self.position = (random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE,
random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE)
def generate_new(self):
while True:
new_pos = (random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE,
random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE)
if new_pos not in snake.positions:
self.position = new_pos
break
# 游戏主循环
def game_loop():
global snake, food
snake = Snake()
food = Food()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction((0, -1))
snake.is_move = True
elif event.key == pygame.K_DOWN:
snake.change_direction((0, 1))
snake.is_move = True
elif event.key == pygame.K_LEFT:
snake.change_direction((-1, 0))
snake.is_move = True
elif event.key == pygame.K_RIGHT:
snake.change_direction((1, 0))
snake.is_move = True
snake.move(food.position)
if snake.positions[0] == food.position:
food.generate_new()
screen.fill(BLACK)
for pos in snake.positions:
pygame.draw.rect(screen, GREEN, [pos[0], pos[1], GRID_SIZE, GRID_SIZE])
pygame.draw.rect(screen, WHITE, [food.position[0], food.position[1], GRID_SIZE, GRID_SIZE])
pygame.display.flip()
clock.tick(10)
pygame.quit()
if __name__ == "__main__":
game_loop()