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))  # 背景图片

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

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

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

  • 10
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Python小游戏大鱼吃小鱼是一个非常简单的游戏,其主要实现方式是利用Pygame库实现。游戏玩法非常简单,玩家通过控制一条鱼,让它不断地吃小鱼,从而不断变大。以下是代码实现的基本思路: 1.导入pygame库和其他必要的库。 ``` import pygame import random import time ``` 2.定义一些常量,例如窗口大小、游戏帧率等。 ``` SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 BG_COLOR = (50, 150, 255) FPS = 60 ``` 3.初始化pygame并创建窗口。 ``` pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Fish Eat Fish') ``` 4.创建鱼的类和小鱼的类,并定义它们的属性和方法。 ``` class Fish(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load('fish.png').convert_alpha() self.rect = self.image.get_rect() self.rect.centerx = random.randint(0, SCREEN_WIDTH) self.rect.centery = random.randint(0, SCREEN_HEIGHT) self.speed = 5 self.direction = random.randint(0, 360) def update(self): radian = math.radians(self.direction) dx = self.speed * math.cos(radian) dy = self.speed * math.sin(radian) self.rect.move_ip(dx, dy) class SmallFish(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load('small_fish.png').convert_alpha() self.rect = self.image.get_rect() self.rect.centerx = random.randint(0, SCREEN_WIDTH) self.rect.centery = random.randint(0, SCREEN_HEIGHT) def update(self): pass ``` 5.创建鱼和小鱼的对象,并将它们添加到精灵组中。 ``` fish_group = pygame.sprite.Group() small_fish_group = pygame.sprite.Group() for i in range(10): fish = Fish() fish_group.add(fish) for i in range(50): small_fish = SmallFish() small_fish_group.add(small_fish) ``` 6.定义游戏主循环,并在其中处理事件、更新鱼和小鱼的位置、绘制图像等操作。 ``` def main(): clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() fish_group.update() small_fish_group.update() screen.fill(BG_COLOR) fish_group.draw(screen) small_fish_group.draw(screen) pygame.display.update() clock.tick(FPS) if __name__ == '__main__': main() ``` 以上是Python小游戏大鱼吃小鱼的基本代码实现思路,你可以根据自己的需求进行修改和扩展。如果你需要更详细的代码实现,请参考以下链接:https://github.com/liuyubobobo/Play-with-Python/blob/master/12-Fish/Fish.py
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值