Python 打地鼠小游戏

对于打地鼠游戏需要有一个清晰的逻辑思路,当然要制作打地鼠游戏还需要在Python中导入背景图片、地鼠图片等等。好了我们废话不多说,直接上代码。

class GameManager:
    def __init__(self):
        # Define constants
        self.SCREEN_WIDTH = 800
        self.SCREEN_HEIGHT = 600
        self.FPS = 60
        self.MOLE_WIDTH = 90
        self.MOLE_HEIGHT = 81
        self.FONT_SIZE = 31
        self.FONT_TOP_MARGIN = 26
        self.LEVEL_SCORE_GAP = 4
        self.LEFT_MOUSE_BUTTON = 1
        self.GAME_TITLE = "Whack A Mole - Game Programming - Assignment 1"
        # Initialize player's score, number of missed hits and level
        self.score = 0
        self.misses = 0
        self.level = 1
        # Initialize screen
        self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT))
        pygame.display.set_caption(self.GAME_TITLE)
        self.background = pygame.image.load("images/bg.png")
        # Font object for displaying text
        self.font_obj = pygame.font.Font('./fonts/GROBOLD.ttf', self.FONT_SIZE)
        # Initialize the mole's sprite sheet
        # 6 different states
        sprite_sheet = pygame.image.load("images/mole.png")
        self.mole = []
        self.mole.append(sprite_sheet.subsurface(169, 0, 90, 81))
        self.mole.append(sprite_sheet.subsurface(309, 0, 90, 81))
        self.mole.append(sprite_sheet.subsurface(449, 0, 90, 81))
        self.mole.append(sprite_sheet.subsurface(575, 0, 116, 81))
        self.mole.append(sprite_sheet.subsurface(717, 0, 116, 81))
        self.mole.append(sprite_sheet.subsurface(853, 0, 116, 81))

我们首先要做的就是先将我们的界面设定好,以及一些后面会用到的参数,将我们的图片导入进来。

接下来,我们需要建立一个循环让我们的界面跑起来,能够实时更新游戏界面,并加入鼠标的操作。

    def start(self):
        cycle_time = 0
        num = -1
        loop = True
        is_down = False
        interval = 0.1
        initial_interval = 1
        frame_num = 0
        left = 0
        # Time control variables
        clock = pygame.time.Clock()

        for i in range(len(self.mole)):
            self.mole[i].set_colorkey((0, 0, 0))
            self.mole[i] = self.mole[i].convert_alpha()

        while loop:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    loop = False
                if event.type == MOUSEBUTTONDOWN and event.button == self.LEFT_MOUSE_BUTTON:
                    self.soundEffect.playFire()
                    if self.is_mole_hit(mouse.get_pos(), self.hole_positions[frame_num]) and num > 0 and left == 0:
                        num = 3
                        left = 14
                        is_down = False
                        interval = 0
                        self.score += 1  # Increase player's score
                        self.level = self.get_player_level()  # Calculate player's level
                        # Stop popping sound effect
                        self.soundEffect.stopPop()
                        # Play hurt sound
                        self.soundEffect.playHurt()
                        self.update()
                    else:
                        self.misses += 1
                        self.update()

好了以上是给制作打地鼠游戏的思路,代码不是全的,可以借鉴。

如果有想要完整版的可以加我微信HZ19980419,网盘密码¥10。

https://pan.baidu.com/s/1F6Pewwvnw1i4xHX75ahYyg

 

 

 

 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是一个简单的Python地鼠小游戏示例: ```python import random import time # 打印游戏说明 print("欢迎来到打地鼠游戏!") print("你需要在规定时间内打中尽可能多的地鼠。") print("每打中一个地鼠会得到1分,时间到了游戏结束。") print("祝你好运!") # 定义游戏时间和得分 game_time = 30 score = 0 # 开始游戏 start_time = time.time() while time.time() - start_time < game_time: # 生成地鼠位置 hole = random.randint(1, 9) # 打印地鼠洞 print("====" * 3) print("|{} {} {}|".format("O" if hole==1 else " ", "O" if hole==2 else " ", "O" if hole==3 else " ")) print("|{} {} {}|".format("O" if hole==4 else " ", "O" if hole==5 else " ", "O" if hole==6 else " ")) print("|{} {} {}|".format("O" if hole==7 else " ", "O" if hole==8 else " ", "O" if hole==9 else " ")) print("====" * 3) # 玩家输入猜测的地鼠位置 guess = int(input("请输入你猜测的地鼠位置(1-9):")) if guess == hole: print("恭喜你,打中了地鼠!") score += 1 else: print("很遗憾,你没有打中地鼠。") # 等待一段时间再继续游戏 time.sleep(1) # 游戏结束,输出得分 print("时间到了,游戏结束!你的得分是:{}".format(score)) ``` 这个游戏会在9个洞口中随机生成一个地鼠,玩家需要猜测地鼠躲在哪个洞口,如果猜中了就得到1分。游戏时间为30秒,时间到了游戏结束,输出得分。你可以根据自己的需求修改游戏的时间、难度等参数,让游戏更加有趣。 ### 回答2: Python地鼠小游戏是一款简单而有趣的游戏,可以让玩家通过控制鼠标点击地鼠图片来获得分数。实现这款游戏的方法如下: 首先,需要导入pygame模块和random模块。 ```python import pygame import random ``` 接下来,创建游戏窗口,并设置窗口的大小和标题。 ```python pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("打地鼠游戏") ``` 然后,定义地鼠类。地鼠类中包含地鼠的图片和位置坐标,以及一个方法用于更新地鼠的位置。 ```python class Mole(pygame.sprite.Sprite): def __init__(self, image, pos): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(image) self.rect = self.image.get_rect() self.rect.topleft = pos def update(self): self.rect.topleft = pos ``` 再定义一个方法用于生成随机的地鼠出现位置。 ```python def generate_mole_pos(): x = random.randint(0, width - 100) y = random.randint(0, height - 100) return (x, y) ``` 接着,初始化分数和地鼠列表。 ```python score = 0 mole_list = pygame.sprite.Group() ``` 然后,创建游戏循环。 ```python running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((255, 255, 255)) if random.randint(1, 100) < 5: mole = Mole('mole.png', generate_mole_pos()) mole_list.add(mole) for mole in mole_list: screen.blit(mole.image, mole.rect) mole.update() pygame.display.flip() pygame.quit() ``` 最后,按照需求添加玩家点击地鼠得分以及游戏结束的逻辑即可。 这就是使用Python实现打地鼠小游戏的基本步骤,你可以根据自己的需要进行更多的定制和添加更多的功能。希望对你有帮助! ### 回答3: Python地鼠小游戏可以通过使用Pygame模块来实现。首先,我们需要创建一个游戏窗口,并设置窗口的标题、尺寸和背景色。接下来,我们可以使用Pygame的精灵类来创建地鼠和锤子的精灵对象,并设置它们的初始位置和动画。然后,使用Pygame的事件循环来监测玩家的输入,当玩家点击地鼠时,我们可以通过判断鼠标点击位置和地鼠精灵对象的碰撞来判断是否成功捕捉到地鼠。如果捕捉成功,我们可以增加得分并更新得分显示。最后,我们可以设置一个时间限制,当时间到达后游戏结束,并显示最终得分。 在游戏过程中,我们可以通过调整地鼠的出现位置和速度,增加游戏的难度。此外,我们还可以设置一个倒计时计时器,提醒玩家剩余的时间。如果玩家想要重新开始游戏,我们可以通过添加一个"重新开始"按钮来实现。 Python地鼠小游戏不仅可以提供娱乐,还可以帮助玩家提高反应和手眼协调能力。它也是一个很好的示例,可以用来学习Python编程和游戏开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值