pythonGame-实现羊了个羊简易字母版

通过python简单复现 羊了个羊 游戏。

使用到的库函数:
python 复制代码
import pygame
import random
游戏源码:
python 复制代码
import pygame
import random

# 初始化pygame
pygame.init()

# 设置窗口大小
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("羊了个羊")

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (128, 128, 128)]

# 方块大小
BLOCK_SIZE = 40

# 创建不同的图案
PATTERNS = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

# 创建游戏板
board = []

def add_blocks(num):
    for _ in range(num):
        pattern = random.choice(PATTERNS)
        color = COLORS[PATTERNS.index(pattern)]
        x = random.randint(0, (WIDTH - BLOCK_SIZE) // BLOCK_SIZE)
        y = random.randint(0, (HEIGHT - 2*BLOCK_SIZE) // BLOCK_SIZE)
        board.append((pattern, x, y, random.randint(0, 2), color))

# 初始添加100个方块
add_blocks(100)

# 选择区
selected = []

# 游戏循环
clock = pygame.time.Clock()
running = True

def draw_board():
    for item in board:
        pattern, x, y, z, color = item
        pygame.draw.rect(screen, color, (x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
        font = pygame.font.Font(None, 36)
        text = font.render(pattern, True, WHITE)
        text_rect = text.get_rect(center=(x*BLOCK_SIZE + BLOCK_SIZE//2, y*BLOCK_SIZE + BLOCK_SIZE//2))
        screen.blit(text, text_rect)

def draw_selected():
    for i, item in enumerate(selected):
        pattern, _, _, _, color = item
        pygame.draw.rect(screen, color, (i*BLOCK_SIZE, HEIGHT-BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
        font = pygame.font.Font(None, 36)
        text = font.render(pattern, True, WHITE)
        text_rect = text.get_rect(center=(i*BLOCK_SIZE + BLOCK_SIZE//2, HEIGHT-BLOCK_SIZE//2))
        screen.blit(text, text_rect)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            for item in board:
                pattern, bx, by, _, color = item
                if bx*BLOCK_SIZE < x < (bx+1)*BLOCK_SIZE and by*BLOCK_SIZE < y < (by+1)*BLOCK_SIZE:
                    if len(selected) < 7:
                        selected.append(item)
                        board.remove(item)
                        # 增加新的方块,直到总数达到200
                        if len(board) + len(selected) < 200:
                            add_blocks(1)
                    break
            
            # 检查是否有三个相同的图案
            if len(selected) >= 3:
                for i in range(len(selected)-2):
                    if selected[i][0] == selected[i+1][0] == selected[i+2][0]:
                        del selected[i:i+3]
                        break

    screen.fill(BLACK)
    draw_board()
    draw_selected()
    
    # 显示当前方块数量
    font = pygame.font.Font(None, 36)
    text = font.render(f"Blocks: {len(board) + len(selected)}/200", True, WHITE)
    screen.blit(text, (10, 10))
    
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
运行效果:

这个游戏对图片资源要求太多了,就用字母替代了,做出来好像简单的连连看,3张相同即可消除。可自己修改增加难度哦。

相关推荐
emplace_back1 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk1 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶1 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl1 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
收破烂的小熊猫~1 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
蹦蹦跳跳真可爱5892 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij2 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien2 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
阿蒙Amon2 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#