以下是部分代码预览:
"""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]
pass
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
pass
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")
pass
pygame.quit()
if __name__ == "__main__":
main()
如需要下载完整源代码及素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

