pygame大鱼吃小鱼

"""pygame大鱼吃小鱼,这是本人曾经制作的一个小练习,现在翻出来以飨读者,以前为了方便学生理解程序,用了些中文变量。"""

import pygame
from pygame.locals import *
import math
from random import randint

pygame.init()
screen = pygame.display.set_mode((480,360))
pygame.display.set_caption("pygame大鱼吃小鱼_作者:李兴球")
    
class Bigfish():
    def __init__(self,rightImageList,leftImageList,x,y):
        self.imageindex = 0        # 相当于造型编号
        self.direction=0           # 0表示右,1左
        self.imageList = [rightImageList,leftImageList]
        self.image = self.imageList[self.direction][self.imageindex]        
        self.rect =self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.嘴巴区域=pygame.Rect(0,0,10,10) # 根据对大鱼的观察,确定它的嘴巴宽高为10X10像素
        
    def move(self,mx,my):                    # mx是鼠标指针的x坐标
        dx = self.rect.centerx - mx          # dx大于0,表示鱼在鼠标指针右边,这时它的方向为左.        
        if dx!=0: self.direction= (dx//abs(dx) + 1)//2
        self.rect.centerx = mx
        self.rect.centery = my

        # 需要动态调整嘴巴区域的x,y坐标
        if dx >0:
           self.嘴巴区域.x = self.rect.x
           self.嘴巴区域.y = self.rect.y -10 # 大鱼的高约为30像素,取1/3的位置
        else:
            if dx<0:
                self.嘴巴区域.x = self.rect.right -10
                self.嘴巴区域.y = self.rect.y -10        
         
    def draw(self):
        self.image = self.imageList[self.direction][self.imageindex]
        screen.blit(self.image,self.rect)
        
class smallfish():
    def __init__(self,imageRight,imageLeft,x,y):
        self.imageList = [imageLeft,imageRight]        
        self.imageIndex = randint(0,1)
        self.image = self.imageList[self.imageIndex]
        self.xspeed = ( 2 * self.imageIndex ) - 1
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y        
        self.delete= 0            # 删除标志
        
    def move(self):
        self.rect.x = self.rect.x + self.xspeed
        if self.rect.x<=0 or self.rect.right>=480   or randint(0,1000)==0:
            self.xspeed = -self.xspeed
            self.imageIndex = 1 - self.imageIndex
            self.image = self.imageList[self.imageIndex]
            
    def 碰到(self,rect):
        return self.rect.colliderect(rect)
    def draw(self):
        screen.blit(self.image,self.rect)        
        
def main():
    
    clock = pygame.time.Clock()
    大鱼右开图 = pygame.image.load("鱼开.gif")
    大鱼右合图 = pygame.image.load("鱼合.gif")
    大鱼右开图.set_colorkey((0,0,0))
    大鱼右合图.set_colorkey((0,0,0))

    大鱼右图表 = [大鱼右开图,大鱼右合图]

    大鱼左开图 = pygame.transform.flip(大鱼右开图,True,False)
    大鱼左合图 = pygame.transform.flip(大鱼右合图,True,False)

    大鱼左图表 = [大鱼左开图,大鱼左合图]

    小鱼右图像表 = []
    for i in range(5):
        小鱼右图像表.append(pygame.image.load("小鱼" + str(i) + ".png"))        
    小鱼左图像表 = []
    for i in range(5):
        小鱼左图像表.append(pygame.image.load("小鱼" + str(i) + "_左.png"))
    
    背景图 = pygame.image.load("underwater2.png")
    
    x,y = pygame.mouse.get_pos()
    大鱼 = Bigfish(大鱼右图表,大鱼左图表,x,y)
    pygame.mouse.set_visible(False)              # 隐藏鼠标指针

    # 产生几条小鱼
    小鱼们 = []
    for i in range(10):
        r = randint(0,4)
        小鱼们.append(smallfish(小鱼右图像表[r],小鱼左图像表[r],randint(50,430),randint(50,300)))

    pygame.mixer.init()
    吃的音效 = pygame.mixer.Sound("吃的声音.wav")    
     
    运行中 = True
    while 运行中:
        for event in pygame.event.get():
            if event.type==QUIT:运行中=False
            if event.type==MOUSEBUTTONDOWN:
                大鱼.imageindex=1               # 鱼的嘴巴合起来
                
            if event.type==MOUSEBUTTONUP:
                大鱼.imageindex=0

        screen.blit(背景图,(0,0))
        mx,my = pygame.mouse.get_pos()
        大鱼.move(mx,my)                    

        for 小鱼 in 小鱼们:
            if 小鱼.delete ==0:
                小鱼.move()
                小鱼.draw()
                # 如果小鱼碰到大鱼的嘴巴并且大鱼正在合嘴,则小鱼的删除标志变量标为1,准备弹出列表.
                if 小鱼.碰到(大鱼.嘴巴区域) and 大鱼.imageindex==1: 
                   小鱼.delete = 1
                   吃的音效.play()
            else:
                小鱼们.remove(小鱼)            

        大鱼.draw()
        pygame.display.update()
        clock.tick(30)
        
    pygame.quit()

if __name__ == "__main__":
    main()