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张相同即可消除。可自己修改增加难度哦。

相关推荐
落羽凉笙12 分钟前
Python学习笔记(3)|数据类型、变量与运算符:夯实基础,从入门到避坑(附图解+代码)
笔记·python·学习
Quintus五等升17 分钟前
深度学习①|线性回归的实现
人工智能·python·深度学习·学习·机器学习·回归·线性回归
ytttr87321 分钟前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
天远Date Lab22 分钟前
Python实战:对接天远数据手机号码归属地API,实现精准用户分群与本地化运营
大数据·开发语言·python
listhi52024 分钟前
基于Gabor纹理特征与K-means聚类的图像分割(Matlab实现)
开发语言·matlab
哈里谢顿43 分钟前
Python异常链:谁才是罪魁祸首?一探"The above exception"的时间顺序
python
qq_433776421 小时前
【无标题】
开发语言·php
哈里谢顿1 小时前
验证 list() 会调用 `__len__` 方法的深度解析
python·django
Davina_yu2 小时前
Windows 下升级 R 语言至最新版
开发语言·windows·r语言
阿珊和她的猫2 小时前
IIFE:JavaScript 中的立即调用函数表达式
开发语言·javascript·状态模式