| 总目标:实现大鱼吃小鱼游戏 功能概括: 鼠标控制鱼的游动:运动方向的改变和鱼头朝向(即图片旋转角度)的改变。npc鱼的生命进程:随机从左或右,在随机高度生成随机品种的npc鱼。被吃或游出画面外时内存释放。吃鱼:用玩家鱼和npc鱼的间距是否低于某值作为判断标准
 
import pygame
import random
from pygame.locals import *
from pygame.math import *
from sys import exit
background = pygame.image.load("drawable/fishgame/background.png")  
img_fish = pygame.image.load("drawable/fishgame/thefish.png")       
img_small = pygame.image.load("drawable/fishgame/small.png")        
img_middle1 = pygame.image.load("drawable/fishgame/middle1.png")    
img_middle2 = pygame.image.load("drawable/fishgame/middle2.png")    
img_big1 = pygame.image.load("drawable/fishgame/big1.png")          
img_big2 = pygame.image.load("drawable/fishgame/big2.png")          
img_shark = pygame.image.load("drawable/fishgame/shark.png")        
new_fish = pygame.transform.rotozoom(img_fish,180,0.2)       
new_small1 = pygame.transform.rotozoom(img_small,180,0.18)   
new_small2 = pygame.transform.rotozoom(img_small,0,0.18)
new_middle12 = pygame.transform.rotozoom(img_middle1,0,0.35) 
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.init()                                        
screen = pygame.display.set_mode((1000, 500), 0, 0)  
pygame.display.set_caption("大鱼吃小鱼21.10.5")        
clock = pygame.time.Clock()                          
sp = Vector2(480,240)          
speed = 3.0                    
mouse_xy = (480,240)
distanse = Vector2(0,0)
fish_dir = Vector2(-1,0)
time_i=0
fish_list=[] 
npc_fish=[]
while True:
    clock.tick_busy_loop(60)
    time_i=time_i+1
    screen.blit(background, (0, 0))  
    if time_i%100 == 0:
        ram_num1=random.randint(0,1)  
        ram_num2=random.randint(1,20)  
        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)
    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 == QUIT:
            exit()
    screen.blit(new_fish, sp)  
    pygame.display.update()
 效果:
  感言:这是我第一次做出一个小游戏,虽然还有很多很多不足,但挺有成就感的。 |