You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
8.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 大鱼吃小鱼
import pygame
import random
from pygame.locals import *
from pygame.math import *
from sys import exit
pygame.init() # 初始化
pygame.mixer.init()
# 图片资源导入
music_botton=pygame.image.load(r'D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\music.png') # 图片-音乐
background = pygame.image.load(r'D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\background.png') # 图片-背景
img_fish = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\thefish.png") # 图片-玩家操控的鱼
img_small = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\small.png") # 图片-小鱼
img_middle1 = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\middle1.png") # 图片-中鱼1
img_middle2 = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\middle2.png") # 图片-中鱼2
img_big1 = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\big1.png") # 图片-大鱼1
img_big2 = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\big2.png") # 图片-大鱼2
img_shark = pygame.image.load(r"D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\shark.png") # 图片-鲨鱼
# 图片资源处理
new_fish = pygame.transform.rotozoom(img_fish,0,0.2) # 图片旋转180°并缩放为原来的0.2倍
new_small1 = pygame.transform.rotozoom(img_small,0,0.18) # 图片旋转180°缩放为原来的0.18倍。并生成水平翻转的图片2
new_small2 = pygame.transform.rotozoom(img_small,0,0.18)
new_middle12 = pygame.transform.rotozoom(img_middle1,0,0.35) # 图片旋转180°缩放为原来的0.35倍。并生成水平翻转的图片2
new_middle11 = pygame.transform.flip(new_middle12,1,0)
new_middle22 = pygame.transform.rotozoom(img_middle2,0,0.35)
new_middle21 = pygame.transform.flip(new_middle22,1,0)
new_big12 = pygame.transform.rotozoom(img_big1,0,0.6)
new_big11 = pygame.transform.flip(new_big12,1,0)
new_big22 = pygame.transform.rotozoom(img_big2,0,0.6)
new_big21 = pygame.transform.flip(new_big22,1,0)
new_shark1 = pygame.transform.rotozoom(img_shark,0,1.2)
new_shark2 = pygame.transform.flip(new_shark1,1,0)
pygame.mixer.music.load(r'D:\网易云\昙轩 - 海の形.mp3') # 加载为背景音乐
pygame.mixer.music.play(-1,0.0) # 播放背景音乐
size = width, height = 1419,640
screen = pygame.display.set_mode(size ) # 设置屏幕
pygame.display.set_caption("大鱼吃小鱼") # 设置标题
clock = pygame.time.Clock() # 设置时钟
sp = Vector2(480,240) # 小鱼坐标为二维向量sp
speed = 2.0 # 速度为两个单位
mouse_xy = (480,240)
distanse = Vector2(0,0)
fish_dir = Vector2(-1,0)
time_i=0
fish_list=[] #四维数组:x,y,方向,品种
npc_fish=[]
score = 0
x1,y1=1379,0
is_music_playing = False # 定义背景音乐的状态,默认是打开
while True:
clock.tick_busy_loop(60)
time_i=time_i+1
screen.blit(background, (0, 0)) # 背景图片
screen.blit(music_botton, (x1,y1)) # 音乐开关图片
if time_i%100 == 0:
ram_num1=random.randint(0,1) # 左右随机数
ram_num2=random.randint(1,20) # 品种随机数smal-12midle-5big-2shark-1
if ram_num1 == 1:
npc_fish.append("left")
else:
npc_fish.append("right")
if ram_num2 == 1:
npc_fish.append("shark")
elif ram_num2 >= 2 | ram_num2 <= 3:
npc_fish.append("big1")
elif ram_num2 >= 4 | ram_num2 <= 5:
npc_fish.append("big2")
elif ram_num2 >= 6 | ram_num2 <= 8:
npc_fish.append("middle1")
elif ram_num2 >= 9 | ram_num2 <= 11 :
npc_fish.append("middle2")
else:
npc_fish.append("small")
if npc_fish[0]=="left":
npc_fish.append(0)
else:
npc_fish.append(1000)
npc_fish.append(random.randint(0,380))
fish_list.append(npc_fish)
print(npc_fish)
npc_fish=[]
for fish in fish_list:
if fish[1] == "small":
if fish[0] == "left":
screen.blit(new_small1,(fish[2],fish[3]))
else:
screen.blit(new_small2,(fish[2],fish[3]))
if fish[1] == "middle1":
if fish[0] == "left":
screen.blit(new_middle11,(fish[2],fish[3]))
else:
screen.blit(new_middle12,(fish[2],fish[3]))
if fish[1] == "middle2":
if fish[0] == "left":
screen.blit(new_middle21,(fish[2],fish[3]))
else:
screen.blit(new_middle22,(fish[2],fish[3]))
if fish[1] == "big1":
if fish[0] == "left":
screen.blit(new_big11,(fish[2],fish[3]))
else:
screen.blit(new_big12,(fish[2],fish[3]))
if fish[1] == "big2":
if fish[0] == "left":
screen.blit(new_big21,(fish[2],fish[3]))
else:
screen.blit(new_big22,(fish[2],fish[3]))
if fish[1] == "shark":
if fish[0] == "left":
screen.blit(new_shark1,(fish[2],fish[3]))
else:
screen.blit(new_shark2,(fish[2],fish[3]))
if fish[0] == "left":
fish[2] = fish[2] + 1
if fish[2] == 1000:
fish_list.remove(fish)
else:
fish[2] = fish[2] - 1
if fish[2] == -200:
fish_list.remove(fish)
distanse = mouse_xy - sp
dis_len=distanse.length()
if dis_len < speed:
mouse_xy=sp
elif dis_len != 0:
distanse.normalize_ip()
distanse=distanse*speed
sp+=distanse
for fish in fish_list:
fish_pos=Vector2(fish[2],fish[3])
distanse2 = sp - fish_pos
if distanse2.length() <= 100:
img_fish = pygame.transform.rotozoom(img_fish,0,1.1)
fish_list.remove(fish)
score += 2
font = pygame.font.Font(r'D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\ziti.ttf', 50)
score_text = font.render("分数:" + str(score), 1, (255, 255, 255)) # 创建文本列表
screen.blit(score_text, (10, 10)) # 在屏幕左上角显示分数
if score > 30: # 判断游戏是否达到分数
game_over = True
else:
game_over = False
if game_over:
font = pygame.font.Font(r'D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\ziti.ttf', 36)
text = font.render("游戏结束了,你的分数是:" + str(score), 1, (255, 255, 255))
text_rect = text.get_rect(center=(width // 2, height // 2 - 100))
screen.blit(text, text_rect)
font = pygame.font.Font(r'D:\pycharm\PycharmProjects\大鱼吃小鱼\.venv\image\ziti.ttf', 36)
quit_button = font.render("结束游戏", 1, (255, 255, 255))
quit_button_rect = quit_button.get_rect(center=(width // 2, height // 2))
pygame.draw.rect(screen, (0, 255, 0), quit_button_rect, 2)
screen.blit(quit_button, quit_button_rect)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos() # 获取鼠标位置
if 630 <= mouse_pos[0] <= 780 and 300 <= mouse_pos[1] <= 340:
pygame.quit()
xit()
pygame.display.flip()
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN: # 鼠标按下事件
mouse_xy=Vector2(event.pos) # 将玩家控制的小鱼移动到鼠标点击的坐标
distanse = mouse_xy - sp
angle = distanse.angle_to(fish_dir)
new_fish = pygame.transform.rotozoom(img_fish,angle,0.2)
if event.type == MOUSEBUTTONDOWN and width - 50 <= mouse_xy[0] <= width and 10 <= mouse_xy[1] <= 60:
if not is_music_playing: # 如果未播放音乐则打开
pygame.mixer.music.play(-1, 0.0)
is_music_playing = True
else: # 如果已经播放了音乐则关闭
pygame.mixer.music.stop()
is_music_playing = False
if event.type == pygame.QUIT:
pygame.mixer.music.stop() # 停止播放背景音乐
exit()
screen.blit(new_fish, sp)
pygame.display.update() # 刷新显示界面