### 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)
```