拍蚊子游戏
下面是一个简单的"拍蚊子游戏"的Python实现,使用Pygame库创建图形界面。
python
import pygame
import sys
import random
import time
# 初始化pygame
pygame.init
# 游戏常量
WIDTH, HEIGHT = 800, 600
BG_COLOR = (240, 248, 255) # 淡蓝色背景
MOSQUITO_COLOR = (0, 100, 0) # 深绿色蚊子
TEXT_COLOR = (25, 25, 112) # 深蓝色文字
HIGHLIGHT_COLOR = (255, 215, 0) # 金色高亮
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("拍蚊子游戏")
# 加载字体
try:
font_large = pygame.font.SysFont('simhei', 48)
font_medium = pygame.font.SysFont('simhei', 36)
font_small = pygame.font.SysFont('simhei', 24)
except:
font_large = pygame.font.Font(None, 48)
font_medium = pygame.font.Font(None, 36)
font_small = pygame.font.Font(None, 24)
class Mosquito:
def __init__(self):
self.radius = 20
self.reset
self.speed_x = random.choice([-3, -2, 2, 3])
self.speed_y = random.choice([-3, -2, 2, 3])
def reset(self):
self.x = random.randint(self.radius, WIDTH
self.y = random.randint(self.radius, HEIGHT
self.active = True
def move(self):
if not self.active:
return
self.x += self.speed_x
self.y += self.speed_y
# 边界检查
if self.x = WIDTH
self.speed_x *= -1
if self.y = HEIGHT
self.speed_y *= -1
def draw(self):
if not self.active:
return
# 绘制蚊子身体
pygame.draw.circle(screen, MOSQUITO_COLOR, (self.x, self.y), self.radius)
# 绘制蚊子腿
leg_length = 15
for angle in [30, 150, 210, 330]:
end_x = self.x + leg_length * pygame.math.Vector2(1, 0).rotate(angle).x
end_y = self.y + leg_length * pygame.math.Vector2(1, 0).rotate(angle).y
pygame.draw.line(screen, MOSQUITO_COLOR, (self.x, self.y), (end_x, end_y), 2)
# 绘制蚊子眼睛
eye_radius = 4
pygame.draw.circle(screen, (255, 255, 255), (self.x
pygame.draw.circle(screen, (255, 255, 255), (self.x + 8, self.y
pygame.draw.circle(screen, (0, 0, 0), (self.x
pygame.draw.circle(screen, (0, 0, 0), (self.x + 8, self.y
def is_hit(self, pos):
if not self.active:
return False
distance = ((pos[0]
return distance
def draw_text(text, font, color, x, y, centered=True):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect
if centered:
text_rect.center = (x, y)
else:
text_rect.topleft = (x, y)
screen.blit(text_surface, text_rect)
def game_intro:
intro = True
while intro:
for event in pygame.event.get:
if event.type == pygame.QUIT:
pygame.quit
sys.exit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
intro = False
if event.key == pygame.K_q:
pygame.quit
sys.exit
screen.fill(BG_COLOR)
draw_text("拍蚊子游戏", font_large, TEXT_COLOR, WIDTH//2, HEIGHT//4)
draw_text("用鼠标点击出现的蚊子", font_medium, TEXT_COLOR, WIDTH//2, HEIGHT//2
draw_text("每只蚊子得10分,限时30秒", font_medium, TEXT_COLOR, WIDTH//2, HEIGHT//2)
draw_text("按空格键开始游戏", font_medium, HIGHLIGHT_COLOR, WIDTH//2, HEIGHT*3//4)
draw_text("按Q键退出", font_small, TEXT_COLOR, WIDTH//2, HEIGHT*3//4 + 50)
pygame.display.flip
def main_game:
score = 0
mosquitoes = [Mosquito for _ in range(5)]
start_time = time.time
game_time = 30 # 30秒游戏时间
running = True
while running:
current_time = time.time
elapsed_time = current_time
remaining_time = max(0, game_time
if remaining_time
running = False
for event in pygame.event.get:
if event.type == pygame.QUIT:
pygame.quit
sys.exit
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos
for mosquito in mosquitoes:
if mosquito.is_hit(pos):
mosquito.reset
score += 10
# 移动蚊子
for mosquito in mosquitoes:
mosquito.move
# 绘制
screen.fill(BG_COLOR)
# 绘制蚊子
for mosquito in mosquitoes:
mosquito.draw
# 绘制分数和时间
draw_text(f"分数: {score}", font_medium, TEXT_COLOR, 100, 30)
draw_text(f"时间: {int(remaining_time)}秒", font_medium, TEXT_COLOR, WIDTH
pygame.display.flip
pygame.time.Clock.tick(60)
return score
def game_over(final_score):
over = True
while over:
for event in pygame.event.get:
if event.type == pygame.QUIT:
pygame.quit
sys.exit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
return True
if event.key == pygame.K_q:
pygame.quit
sys.exit
screen.fill(BG_COLOR)
draw_text("游戏结束!", font_large, TEXT_COLOR, WIDTH//2, HEIGHT//4)
draw_text(f"最终分数: {final_score}", font_medium, TEXT_COLOR, WIDTH//2, HEIGHT//2)
draw_text("按R键重新开始", font_medium, HIGHLIGHT_COLOR, WIDTH//2, HEIGHT*3//4)
draw_text("按Q键退出", font_small, TEXT_COLOR, WIDTH//2, HEIGHT*3//4 + 40)
pygame.display.flip
# 主游戏循环
def main:
while True:
game_intro
final_score = main_game
if not game_over(final_score):
break
if __name__ == "__main__":
main
pygame.quit
如何运行游戏
1. 确保你已安装Python和Pygame库。如果没有安装Pygame,可以使用以下命令安装:
pip install pygame
QQ扑克官网2. 将上面的代码保存为`mosquito_game.py`
3. 运行Python文件即可开始游戏
游戏说明
游戏特点
祝你游戏愉快!