python小游戏1:大鱼吃小鱼

2021/10/6 20:12:30

本文主要是介绍python小游戏1:大鱼吃小鱼,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

总目标:实现大鱼吃小鱼游戏

功能概括

  1. 鼠标控制鱼的游动:运动方向的改变和鱼头朝向(即图片旋转角度)的改变。
  2. npc鱼的生命进程:随机从左或右,在随机高度生成随机品种的npc鱼。被吃或游出画面外时内存释放。
  3. 吃鱼:用玩家鱼和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")    # 图片-中鱼1
img_middle2 = pygame.image.load("drawable/fishgame/middle2.png")    # 图片-中鱼2
img_big1 = pygame.image.load("drawable/fishgame/big1.png")          # 图片-大鱼1
img_big2 = pygame.image.load("drawable/fishgame/big2.png")          # 图片-大鱼2
img_shark = pygame.image.load("drawable/fishgame/shark.png")        # 图片-鲨鱼

# 图片资源处理
new_fish = pygame.transform.rotozoom(img_fish,180,0.2)       # 图片旋转180°,并缩放为原来的0.2倍

new_small1 = pygame.transform.rotozoom(img_small,180,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.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)          # 小鱼坐标为二维向量sp
speed = 3.0                    # 速度为三个单位
mouse_xy = (480,240)
distanse = Vector2(0,0)
fish_dir = Vector2(-1,0)
time_i=0
fish_list=[] #四维数组:x,y,方向,品种
npc_fish=[]


while True:
    clock.tick_busy_loop(60)
    time_i=time_i+1

    screen.blit(background, (0, 0))  # 背景图片

    pygame.display.update()

    if time_i%100 == 0:
        ram_num1=random.randint(0,1)  # 左右随机数
        ram_num2=random.randint(1,20)  # 品种随机数:smal-12,midle-5,big-2,shark-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)


    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.flip()

效果:
在这里插入图片描述

感言:这是我第一次做出一个小游戏,虽然还有很多很多不足,但挺有成就感的。



这篇关于python小游戏1:大鱼吃小鱼的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程