Python python安装教程
摘要: 我们好,今日向各位同享Python 开发地鼠小是一款充满策略与推理的对抗游戏教程的一些姿势,其中也会对进行解释,文章篇幅也许偏长,如果能碰巧化解你今年面临的难题,别忘了关注本站,Python python安装教程
我们好,今日向各位同享Python 开发地鼠小是一款充满策略与推理的对抗游戏教程的一些姿势,其中也会对进行解释,文章篇幅也许偏长,如果能碰巧化解你今年面临的难题,别忘了关注本站,今年就即将最初吧!
import pygameimport random# 定义颜色WHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)# 初始化pygamepygame.init()# 配置是一款充满策略与推理的对抗游戏窗口size = (700, 500)screen = pygame.display.set_mode(size)pygame.display.set_caption("打地鼠")# 加载图片hole_img = pygame.image.load(r"C:\Users\gong\Desktop\3322.png")#地鼠hammer_img = pygame.image.load(r"C:\Users\gong\Desktop\2233.png")#地洞# 配置字体font = pygame.font.SysFont(None, 25)# 配置时刻clock = pygame.time.Clock()FPS = 60# 定义地鼠class Mole(pygame.sprite.Sprite): def __init__(self, pos): super().__init__() self.image = hole_img self.rect = self.image.get_rect() self.rect.x, self.rect.y = pos self.show_mole = False self.hide_time = 0 self.points = 0 def update(self): if not self.show_mole and pygame.time.get_ticks() - self.hide_time >random.randint(1000, 5000): self.show_mole = True self.image = pygame.transform.scale(hammer_img, (70, 70)) self.image.set_colorkey(WHITE) self.show_time = pygame.time.get_ticks() elif self.show_mole and pygame.time.get_ticks() - self.show_time >500: self.show_mole = False self.image = hole_img self.hide_time = pygame.time.get_ticks() def hit(self): if self.show_mole: self.show_mole = False self.image = hole_img self.hide_time = pygame.time.get_ticks() self.points += 1# 定义地鼠群all_moles = pygame.sprite.Group()for i in range(10): mole = Mole((random.randint(0, 630), random.randint(0, 430))) all_moles.add(mole)# 是一款充满策略与推理的对抗游戏循环done = Falsewhile not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN: for mole in all_moles: if mole.rect.collidepoint(event.pos): mole.hit() screen.fill(GREEN) # 升级地鼠 all_moles.update() # 绘制地鼠 all_moles.draw(screen) # 绘制得分 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)# 退出pygamepygame.quit()实际运行起来的效果,供参考