python 打地鼠小游戏

python 打地鼠小游戏

首页休闲益智打地鼠游戏机更新时间:2024-04-11

给大家分享一段AI自动生成的代码(在这个游戏中,玩家需要在有限时间内打中尽可能多的出现在地图上的地鼠),由于我现在用的这个电脑没有安装sublime或pycharm等工具,所以还没有测试,有兴趣的朋友可以跑一下试试。

注意,在代码的第28行,“mole_image = pygame.image.load("mole.png").convert_alpha()”需要先准备一个png格式的图片,并将图片路径完整准确复制到代码中。

import pygame import random # 初始化 Pygame pygame.init() # 设置游戏窗口尺寸 WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) # 设置游戏标题 pygame.display.set_caption("打地鼠") # 设置游戏背景色 BACKGROUND_COLOR = (255, 255, 255) # 设置地鼠的大小 MOLE_SIZE = 80 # 设置地鼠出现的时间间隔(单位:毫秒) MOLE_INTERVAL = 1000 # 设置游戏时长(单位:秒) GAME_DURATION = 30 # 加载地鼠的图片 mole_image = pygame.image.load("mole.png").convert_alpha() # 定义地鼠类 class Mole: def __init__(self, x, y): self.x = x self.y = y self.visible = False self.timer = 0 # 在游戏窗口上绘制地鼠 def draw(self): if self.visible: window_surface.blit(mole_image, (self.x, self.y)) # 更新地鼠的状态 def update(self, delta_time): self.timer -= delta_time if self.timer <= 0: self.visible = not self.visible self.timer = MOLE_INTERVAL # 创建地鼠列表 moles = [] for i in range(3): for j in range(3): x = 100 i * 200 y = 100 j * 200 moles.append(Mole(x, y)) # 初始化游戏计时器 game_timer = pygame.time.Clock() game_start_time = pygame.time.get_ticks() game_end_time = game_start_time GAME_DURATION * 1000 # 初始化得分 score = 0 # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() # 处理鼠标事件 if event.type == pygame.MOUSEBUTTONDOWN: for mole in moles: if mole.visible: mouse_x, mouse_y = pygame.mouse.get_pos() if mouse_x >= mole.x and mouse_x < mole.x MOLE_SIZE and mouse_y >= mole.y and mouse_y < mole.y MOLE_SIZE: mole.visible = False score = 1 # 更新游戏状态 current_time = pygame.time.get_ticks() delta_time = current_time - game_timer.tick(60) for mole in moles: mole.update(delta_time) # 在游戏窗口上绘制游戏元素 window_surface.fill(BACKGROUND_COLOR) for mole in moles: mole.draw() # 在游戏窗口上绘制得分 font = pygame.font.SysFont(None, 48) text_surface = font.render("得分:" str(score), True, (0, 0, 0)) # 绘制得分 text = font.render("得分: " str(sum([mole.points for mole in all_moles])), True, BLACK) screen.blit(text, (10, 10)) # 刷新屏幕 pygame.display.flip() # 控制帧率 clock.tick(FPS) # 退出pygame pygame.quit()

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved