Python and pygame implement rabbit catching game

Python and pygame implement rabbit catching game

Python and pygame implement the rabbit catching game. You need to install and use the third-party library pygame. For information on the installation and use of the pygame game module in Python, seehttps://blog.csdn.net /cnds123/article/details/119514520

The following is a game created using Python and Pygame. There is a grass background picture and a randomly moving small picture - a bunny. Players have to use the mouse to click on the little bunny picture. Each successful click means catching a bunny. Score plus one point. There is a timer to the right of the score, which displays the running time of the game in seconds.

Let’s look at the renderings first:

Two pictures are needed

The background.jpg image is as follows:

The small_image.jpg picture is as follows:

Please note that the above code assumes that the background image is background.jpg and the small image is small_image.png. Please make sure to place these two files in the same directory as the source code files given below. You can replace these images yourself, just make sure the file names are correct.

The source code is given below:

import pygame
import random
import time

# 初始化游戏
pygame.init()

# 设置游戏窗口尺寸
WIDTH = 800
HEIGHT = 600
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("捉小兔游戏(用鼠标点击)")

# 加载背景图片
background_img = pygame.image.load("background.jpg")

# 加载小图片
small_img = pygame.image.load("small_image.jpg")
small_rect = small_img.get_rect()

# 设置小图片的初始位置
small_rect.x = random.randint(0, WIDTH - small_rect.width)
small_rect.y = random.randint(0, HEIGHT - small_rect.height)

# 初始化得分
score = 0
# 计时器
start_time = time.time()

# 游戏主循环
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 检查是否点击到了小图片
            if small_rect.collidepoint(event.pos):
                # 点击成功,得分加一
                score += 1
                # 重新设置小图片的位置
                small_rect.x = random.randint(0, WIDTH - small_rect.width)
                small_rect.y = random.randint(0, HEIGHT - small_rect.height)

    # 移动小图片
    small_rect.x += random.randint(-35, 35)  #
    small_rect.y += random.randint(-35, 35)  #
    
    # 确保小图片在窗口内部
    if small_rect.left < 0:
        small_rect.left = 0
    if small_rect.right > WIDTH:
        small_rect.right = WIDTH
    if small_rect.top < 0:
        small_rect.top = 0
    if small_rect.bottom > HEIGHT:
        small_rect.bottom = HEIGHT

    # 更新背景图片
    window.blit(background_img, (0, 0))

    # 更新小图片
    window.blit(small_img, (small_rect.x, small_rect.y))

    # 显示得分
    font = pygame.font.Font(None, 36)
    score_text = font.render("Score: " + str(score), True, (255, 0, 0))  #
    window.blit(score_text, (10, 10))

    # 显示计时器
    elapsed_time = int(time.time() - start_time)
    time_text = font.render("Time: " + str(elapsed_time) + "s", True, (255, 0, 0))
    window.blit(time_text, (WIDTH - 150, 10))

    # 刷新窗口
    pygame.display.flip()

    # 添加延迟时间,处理小图移动太快晃眼
    pygame.time.delay(200)  #200毫秒

    # 控制游戏帧率
    clock.tick(60)

# 退出游戏
pygame.quit()

Guess you like

Origin blog.csdn.net/cnds123/article/details/134996061