代码!!!
import pygame
import random
import time
# 游戏初始化
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("打地鼠游戏")
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 游戏变量
score = 0
hole_size = 100
hole_pos = (random.randint(hole_size, 800-hole_size),
random.randint(hole_size, 600-hole_size))
start_time = time.time()
game_over = False
# 游戏循环
while not game_over:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击事件
mouse_pos = pygame.mouse.get_pos()
distance = ((mouse_pos[0]-hole_pos[0])**2 +
(mouse_pos[1]-hole_pos[1])**2)**0.5
if distance < hole_size:
# 鼠标点中地鼠
score += 1
hole_pos = (random.randint(hole_size, 800-hole_size),
random.randint(hole_size, 600-hole_size))
# 绘制背景
screen.fill(WHITE)
# 绘制地鼠洞
pygame.draw.circle(screen, BLACK, hole_pos, hole_size)
# 绘制分数和时间
elapsed_time = time.time() - start_time
if not game_over:
font = pygame.font.Font(None, 36)
text = font.render("分数: " + str(score), True, BLACK)
screen.blit(text, (10, 10))
text = font.render("时间: " + str(int(elapsed_time)) + "秒", True, BLACK)
screen.blit(text, (10, 50))
# 更新屏幕
pygame.display.flip()
# 结束条件
if elapsed_time >= 30:
game_over = True
# 游戏结束
screen.fill(WHITE)
font = pygame.font.Font(None, 48)
text = font.render("游戏结束!你的得分是" + str(score), True, BLACK)
text_rect = text.get_rect(center=(400, 300))
screen.blit(text, text_rect)
pygame.display.flip()
# 等待退出
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
本篇文章到此结束。