Python 写的几个经典游戏 新年放烟花、 贪吃蛇、俄罗斯方块、超级玛丽、五子棋、蜘蛛纸牌

0、新年放烟花

import pygame
import random
import math

# 初始化Pygame
pygame.init()

# 设置窗口
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("新年放烟花")

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 烟花粒子类
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.size = 2
        angle = random.uniform(0, math.pi * 2)
        speed = random.uniform(2, 6)
        self.vx = math.cos(angle) * speed
        self.vy = math.sin(angle) * speed
        self.lifetime = 100
        
    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.1  # 重力效果
        self.lifetime -= 2
        return self.lifetime > 0
    
    def draw(self, screen):
        if self.lifetime > 0:
            alpha = int(self.lifetime * 2.55)  # 淡出效果
            color = (self.color[0], self.color[1], self.color[2], alpha)
            surface = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
            pygame.draw.circle(surface, color, (self.size, self.size), self.size)
            screen.blit(surface, (int(self.x - self.size), int(self.y - self.size)))

# 烟花类
class Firework:
    def __init__(self, x, y):
        self.x = x
        self.y = HEIGHT
        self.target_y = y
        self.speed = -10
        self.particles = []
        self.exploded = False
        self.color = (
            random.randint(50, 255),
            random.randint(50, 255),
            random.randint(50, 255)
        )
    
    def update(self):
        if not self.exploded:
            self.y += self.speed
            if self.y <= self.target_y:
                self.explode()
                
        particles_to_keep = []
        for particle in self.particles:
            if particle.update():
                particles_to_keep.append(particle)
        self.particles = particles_to_keep
        
        return len(self.particles) > 0 or not self.exploded
    
    def explode(self):
        self.exploded = True
        for _ in range(50):  # 创建50个粒子
            self.particles.append(Particle(self.x, self.y, self.color))
    
    def draw(self, screen):
        if not self.exploded:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3)
        for particle in self.particles:
            particle.draw(screen)

# 主游戏循环
def main():
    clock = pygame.time.Clock()
    fireworks = []
    running = True
    
    while running:
        screen.fill(BLACK)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                fireworks.append(Firework(x, y))
        
        # 随机添加烟花
        if random.random() < 0.03:  # 3%的概率在每一帧添加新烟花
            x = random.randint(0, WIDTH)
            y = random.randint(HEIGHT//3, 2*HEIGHT//3)
            fireworks.append(Firework(x, y))
        
        # 更新和绘制烟花
        fireworks_to_keep = []
        for firework in fireworks:
            if firework.update():
                fireworks_to_keep.append(firework)
            firework.draw(screen)
        fireworks = fireworks_to_keep
        
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()

if __name__ == "__main__":
    main()

 

1、贪吃蛇

pip install pygame

import pygame
import random
import sys

# 初始化Pygame
pygame.init()

# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

# 游戏设置
WINDOW_WIDTH = 800 # 窗口宽度
WINDOW_HEIGHT = 600 # 窗口高度
GRID_SIZE = 20 # 网格大小
GRID_WIDTH = WINDOW_WIDTH // GRID_SIZE # 网格宽度
GRID_HEIGHT = WINDOW_HEIGHT // GRID_SIZE # 网格高度
SNAKE_SPEED = 5 # 速度

# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('贪吃蛇游戏')
clock = pygame.time.Clock()

class Snake:
    def __init__(self):
        self.length = 3
        self.positions = []
        center_x = GRID_WIDTH // 2
        center_y = GRID_HEIGHT // 2
        for i in range(self.length):
            self.positions.append((center_x + i, center_y))
        self.direction = LEFT
        self.color = GREEN
        self.score = 0

    def get_head_position(self):
        return self.positions[0]

    def update(self):
        cur = self.get_head_position()
        x, y = self.direction
        new = ((cur[0] + x) % GRID_WIDTH, (cur[1] + y) % GRID_HEIGHT)
        if new in self.positions[3:]:
            return False
        self.positions.insert(0, new)
        if len(self.positions) > self.length:
            self.positions.pop()
        return True

    def reset(self):
        self.length = 3
        self.positions = []
        center_x = GRID_WIDTH // 2
        center_y = GRID_HEIGHT // 2
        for i in range(self.length):
            self.positions.append((center_x + i, center_y))
        self.direction = LEFT
        self.score = 0

    def render(self):
        for p in self.positions:
            pygame.draw.rect(screen, self.color, 
                           (p[0] * GRID_SIZE, p[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))

class Food:
    def __init__(self):
        self.position = (0, 0)
        self.color = RED
        self.randomize_position()

    def randomize_position(self):
        self.position = (random.randint(0, GRID_WIDTH-1), 
                        random.randint(0, GRID_HEIGHT-1))

    def render(self):
        pygame.draw.rect(screen, self.color,
                        (self.position[0] * GRID_SIZE, 
                         self.position[1] * GRID_SIZE, 
                         GRID_SIZE, GRID_SIZE))

# 定义方向
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

def main():
    snake = Snake()
    food = Food()
    font = pygame.font.Font(None, 36)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and snake.direction != DOW
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值