首页 >游戏 > 正文

python做的游戏有哪些

青海通 2025-06-26 19:39游戏 1385 0

python做的游戏有哪些

Python 做的游戏种类丰富多样,涵盖各种类型和玩法,Python作为一种易于学习且功能强大的编程语言,已经被广泛应用于开发各种游戏,以下是一些使用Python开发的著名游戏及其特点。

Pong

  • 简介:《Pong》是一款经典的电子游戏,最早出现在1972年,它是一个简单的乒乓球游戏,玩家控制一方的球拍来击打对方的球。

  • Python实现:通过Pygame库可以轻松地创建一个基本的Pong游戏,Pygame是一个开源的游戏开发库,支持多种操作系统(Windows、Linux、MacOS)。

  • 示例代码

    import pygame
    # 初始化pygame
    pygame.init()
    # 设置窗口大小
    screen = pygame.display.set_mode((800, 600))
    # 加载背景图像
    background = pygame.image.load("background.jpg").convert()
    # 定义球的位置和速度
    ball_x, ball_y = 400, 300
    ball_speed = [5, 5]
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
        # 获取键盘输入
        keys = pygame.key.get_pressed()
        # 移动球
        if keys[pygame.K_LEFT]:
            ball_x -= ball_speed[0]
        if keys[pygame.K_RIGHT]:
            ball_x += ball_speed[0]
        # 碰撞检测
        if ball_x <= 0 or ball_x >= 800:
            ball_speed[0] *= -1
        if ball_y <= 0 or ball_y >= 600:
            ball_speed[1] *= -1
        # 绘制背景和球
        screen.blit(background, (0, 0))
        pygame.draw.circle(screen, (255, 255, 255), (int(ball_x), int(ball_y)), 20)
        pygame.display.flip()

Snake

  • 简介:《蛇》是一款经典的横版动作类游戏,玩家控制一条蛇在屏幕上移动并吃掉食物,同时避免碰撞墙壁或自己。

  • Python实现:同样通过Pygame库可以很容易地创建Snake游戏。

  • 示例代码

    import pygame
    from pygame.locals import *
    # 初始化pygame
    pygame.init()
    # 设置窗口大小
    screen = pygame.display.set_mode((800, 600))
    # 定义颜色
    WHITE = (255, 255, 255)
    # 定义蛇的身体
    body = [(400, 300)]
    # 定义方向
    direction = 'right'
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
        # 更新蛇的位置
        if direction == 'up':
            new_head = (body[0][0], body[0][1] + 10)
        elif direction == 'down':
            new_head = (body[0][0], body[0][1] - 10)
        elif direction == 'left':
            new_head = (body[0][0] - 10, body[0][1])
        else:  # right
            new_head = (body[0][0] + 10, body[0][1])
        # 添加新头到蛇的身体中
        body.insert(0, new_head)
        # 清空旧位置
        for i in range(len(body) - 1):
            body.pop(-1)
        # 检查是否吃到食物
        food = pygame.Rect(500, 500, 20, 20)
        if body[0] == food.center:
            print('Eat!')
            food = pygame.Rect(random.randint(1, 790), random.randint(1, 590), 20, 20)
        else:
            body.pop(-1)
        # 绘制屏幕
        screen.fill(WHITE)
        for pos in body:
            pygame.draw.rect(screen, (0, 255, 0), Rect(pos, (20, 20)))
        pygame.draw.rect(screen, (255, 0, 0), food)
        pygame.display.update()

Space Invaders

  • 简介:《太空侵略者》是一款经典的射击类游戏,玩家需要控制飞船消灭敌机,并尽可能多地收集星星以获得分数。

  • Python实现:利用Pygame库可以快速创建这款经典游戏。

  • 示例代码

    import pygame
    from pygame.locals import *
    # 初始化pygame
    pygame.init()
    # 设置窗口大小
    screen = pygame.display.set_mode((800, 600))
    # 定义颜色
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    # 定义飞船
    ship_width, ship_height = 50, 10
    ship_pos = [400, 590]
    # 定义子弹
    bullet_width, bullet_height = 10, 30
    bullets = []
    def draw_ship(ship_pos):
        pygame.draw.rect(screen, WHITE, Rect(ship_pos, (ship_width, ship_height)))
    def draw_bullet(bullet_pos):
        pygame.draw.rect(screen, WHITE, Rect(bullet_pos, (bullet_width, bullet_height)))
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == KEYDOWN:
                if event.key == K_SPACE and len(bullets) < 5:
                    x = ship_pos[0] + ship_width // 2
                    y = ship_pos[1] - ship_height // 2
                    bullets.append([x, y])
        # 更新子弹位置
        for bullet in bullets:
            bullet[1] -= 10
            if bullet[1] < 0:
                bullets.remove(bullet)
        # 渲染
        screen.fill(BLACK)
        draw_ship(ship_pos)
        for bullet in bullets:
            draw_bullet(bullet)
        pygame.display.update()

Tetris

  • 简介:《塔利斯》是一种基于块状形状的拼图游戏,玩家通过旋转和移动块状图形来填满网格中的空格。

  • Python实现:使用Pygame可以创建塔利斯游戏的基本版本。

  • 示例代码

    import pygame
    from pygame.locals import *
    # 初始化pygame
    pygame.init()
    # 设置窗口大小
    screen = pygame.display.set_mode((800, 600))
    # 定义颜色
    BLACK = (0, 0, 0)
    RED = (255, 0, 0)
    # 定义块状图形
    block_size = 50
    blocks = [
        [(i * block_size, j * block_size) for i in range(block_size)] for j in range(block_size)
    ]
    # 定义行数和列数
    rows = 20
    cols = 10
    # 初始化游戏状态
    game_over = False
    score = 0
    while not game_over:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                exit()
            if event.type == KEYDOWN:
                if event.key == K_UP:
                    rotate_blocks(blocks)
                elif event.key == K_DOWN:
                    move_down(blocks)
                elif event.key == K_LEFT:
                    move_left(blocks)
                elif event.key == K_RIGHT:
                    move_right(blocks)
        # 更新游戏状态
        score += check_score(blocks)
        check_game_over(blocks)
        # 渲染
        screen.fill(BLACK)
        for row in blocks:
            for col in row:
                pygame.draw.rect(screen, RED, Rect(col, (block_size, block_size)))
        font = pygame.font.SysFont(None, 30)
        text_surface = font.render(f'Score: {score}', True, WHITE)
        screen.blit(text_surface, (10, 10))
        pygame.display.update()

这些仅是Python中部分流行游戏的一个小样本,Python可以用来开发几乎任何类型的程序,从桌面应用到网络服务再到嵌入式系统,随着Python的不断发展和扩展,其应用范围将会更加广泛,无论你是想创作一款新的电子游戏,还是想要使用Python进行数据科学分析或者机器学习,Python都提供了丰富的工具和支持。


顶部