帮我写一个脚本关于一个找茬的游戏
以下是一个简单的找茬游戏脚本:
import pygame
import random
# 定义常量
WIDTH = 800
HEIGHT = 600
FPS = 30
# 初始化 Pygame
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("找茬游戏")
clock = pygame.time.Clock()
# 加载图片
img1 = pygame.image.load("image1.png").convert()
img2 = pygame.image.load("image2.png").convert()
# 随机生成两个不同的茬点
diff_x = random.randint(0, WIDTH - 50)
diff_y = random.randint(0, HEIGHT - 50)
diff_x2 = random.randint(0, WIDTH - 50)
diff_y2 = random.randint(0, HEIGHT - 50)
while abs(diff_x - diff_x2) < 50 and abs(diff_y - diff_y2) < 50:
diff_x2 = random.randint(0, WIDTH - 50)
diff_y2 = random.randint(0, HEIGHT - 50)
# 游戏循环
running = True
while running:
# 设置帧率
clock.tick(FPS)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 绘制背景
screen.blit(img1, (0, 0))
# 在第二张图片上绘制茬点
img2_copy = img2.copy()
pygame.draw.rect(img2_copy, (255, 0, 0), (diff_x, diff_y, 50, 50))
pygame.draw.rect(img2_copy, (255, 0, 0), (diff_x2, diff_y2, 50, 50))
screen.blit(img2_copy, (0, 0))
# 更新屏幕
pygame.display.flip()
# 检测鼠标是否点击了茬点
if pygame.mouse.get_pressed()[0]:
mouse_x, mouse_y = pygame.mouse.get_pos()
if abs(mouse_x - diff_x) < 50 and abs(mouse_y - diff_y) < 50:
print("找到第一个茬点!")
elif abs(mouse_x - diff_x2) < 50 and abs(mouse_y - diff_y2) < 50:
print("找到第二个茬点!")
# 退出 Pygame
pygame.quit()
这个脚本使用 Pygame 库来实现找茬游戏的基本功能。它加载两张图片,随机生成两个茬点,然后在屏幕上显示图片并在茬点处绘制红色方块。当玩家点击茬点时,程序会检测鼠标位置并判断是否点击了正确的茬点,然后在控制台输出相应的提示。游戏循环会一直运行直到玩家关闭窗口
原文地址: http://www.cveoy.top/t/topic/ca0J 著作权归作者所有。请勿转载和采集!