给我一个用python做的捕鱼达人游戏代码
时间: 2024-02-01 15:04:23 浏览: 177
捕鱼达人python项目,python游戏项目,直接在软件上运行py文件即可,基础功能与原版类似
4星 · 用户满意度95%
很高兴回答您的问题!以下是一个用Python编写的简单捕鱼达人游戏代码,供您参考:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义游戏窗口尺寸
window_width = 800
window_height = 600
# 创建游戏窗口
screen = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("捕鱼达人")
# 加载游戏素材
background_img = pygame.image.load("background.jpg")
fish1_img = pygame.image.load("fish1.png")
fish2_img = pygame.image.load("fish2.png")
hook_img = pygame.image.load("hook.png")
# 设置字体
font = pygame.font.Font(None, 36)
# 初始化游戏变量
score = 0
hook_x = window_width / 2
hook_y = window_height - 100
hook_speed = 5
fish1_x = random.randint(0, window_width - fish1_img.get_width())
fish1_y = random.randint(0, window_height - fish1_img.get_height())
fish1_speed = random.randint(1, 3)
fish2_x = random.randint(0, window_width - fish2_img.get_width())
fish2_y = random.randint(0, window_height - fish2_img.get_height())
fish2_speed = random.randint(1, 3)
# 游戏循环
running = True
while running:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
hook_x -= hook_speed
elif event.key == pygame.K_RIGHT:
hook_x += hook_speed
# 更新游戏变量
fish1_y += fish1_speed
if fish1_y > window_height:
fish1_x = random.randint(0, window_width - fish1_img.get_width())
fish1_y = -fish1_img.get_height()
fish1_speed = random.randint(1, 3)
score -= 1
fish2_y += fish2_speed
if fish2_y > window_height:
fish2_x = random.randint(0, window_width - fish2_img.get_width())
fish2_y = -fish2_img.get_height()
fish2_speed = random.randint(1, 3)
score -= 1
if hook_x < fish1_x + fish1_img.get_width() and hook_x + hook_img.get_width() > fish1_x and hook_y < fish1_y + fish1_img.get_height() and hook_y + hook_img.get_height() > fish1_y:
fish1_x = random.randint(0, window_width - fish1_img.get_width())
fish1_y = -fish1_img.get_height()
fish1_speed = random.randint(1, 3)
score += 1
if hook_x < fish2_x + fish2_img.get_width() and hook_x + hook_img.get_width() > fish2_x and hook_y < fish2_y + fish2_img.get_height() and hook_y + hook_img.get_height() > fish2_y:
fish2_x = random.randint(0, window_width - fish2_img.get_width())
fish2_y = -fish2_img.get_height()
fish2_speed = random.randint(1, 3)
score += 2
# 绘制游戏场景
screen.blit(background_img, (0, 0))
screen.blit(fish1_img, (fish1_x, fish1_y))
screen.blit(fish2_img, (fish2_x, fish2_y))
screen.blit(hook_img, (hook_x, hook_y))
score_text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 更新屏幕显示
pygame.display.update()
# 退出pygame
pygame.quit()
```
请注意,此代码仅用于演示目的,游戏素材需要您自行准备。如果您想要更好的游戏体验,还需要增加音效、多种鱼类、关卡等元素。
阅读全文