Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待
4999元包邮
去购买 >
先看看最后效果
图片的话是从网上小鱼儿那个案例里面拿的,实现的效果是通过方向键操作乌龟,进行吃鱼,吃鱼的时候根据乌龟的x,y坐标和鱼的x,y进行检测,在碰撞区域则干掉这条鱼,给乌龟加点分啥的
实现代码
import random
import pygame
import sys
from pygame.locals import * #导入一些常用的函数
pygame.init()
screen=pygame.display.set_mode([640,480])
pygame.display.set_caption('乌龟吃鱼') #定义窗口的标题为'乌龟吃鱼'
background = pygame.image.load("C:\\Users\\Administrator\\Desktop\\game\\images\\haidi.jpg").convert()
fishImg = pygame.image.load("C:\\Users\\Administrator\\Desktop\\game\\images\\fish.png").convert_alpha()
wuguiImg = pygame.image.load("C:\\Users\\Administrator\\Desktop\\game\\images\\turtle.png").convert_alpha()
w_width = wuguiImg.get_width() #得到乌龟图片的宽度,后面留着吃鱼的时候用
w_height = wuguiImg.get_height() #得到乌龟图片的高度
y_width = fishImg.get_width() #得到鱼图片的宽度
y_height = fishImg.get_height()#得到鱼图片的高度
#乌龟类
class Turtle:
def __init__(self):
self.power=100 #体力
#乌龟坐标
self.x=random.randint(0,640)
self.y=random.randint(0,400)
#乌龟移动的方法:移动方向均随机 第四条
def move(self,new_x,new_y):
#判断移动后是否超出边界
if new_x<0:
self.x=0-new_x
elif new_x>640:
self.x=640-(new_x-640)
else:
#不越界则移动乌龟的位置
self.x=new_x
if new_y<0:
self.y=0-new_y
elif new_y>480:
self.y=480-(new_y-480)
else:
#不越界则移动乌龟的位置
self.y=new_y
self.power-=1 #乌龟每移动一次,体力消耗1
def eat(self):
self.power+=20 #乌龟吃掉鱼,乌龟体力增加20
if self.power>100:
self.power=100 #乌龟体力100(上限)
#鱼类
class Fish:
def __init__(self):
#鱼坐标
self.x=random.randint(0,680)
self.y=random.randint(0,480)
def move(self):
#计算移动后的新位置(只有四种可能)
new_x=self.x+random.choice([1,-1])
new_y=self.y+random.choice([1,-1])
#判断移动后是否超出边界
if new_x<0:
self.x=0-new_x
elif new_x>640:
self.x=640-(new_x-640)
else:
#不越界则移动鱼的位置
self.x=new_x
if new_y<0:
self.y=0-new_y
elif new_y>480:
self.y=480-(new_y-480)
else:
#不越界则移动鱼的位置
self.y=new_y
#开始测试数据
tur=Turtle() #生成1只乌龟
fish=[] #生成10条鱼
for item in range(10):
newfish=Fish()
fish.append(newfish) #把生成的鱼放到鱼缸里
#pygame有一个事件循环,不断检查用户在做什么。事件循环中,如何让循环中断下来(pygame形成的窗口中右边的插号在未定义前是不起作用的)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
#通过上下左右方向键控制乌龟的动向
if event.key==pygame.K_LEFT:
tur.move(tur.x-10,tur.y)
if event.key==pygame.K_RIGHT:
tur.move(tur.x+10,tur.y)
if event.key==pygame.K_UP:
tur.move(tur.x,tur.y-10)
if event.key==pygame.K_DOWN:
tur.move(tur.x,tur.y+10)
screen.blit(background, (0, 0)) #绘制背景图片
for item in fish:
screen.blit(fishImg, (item.x, item.y))#绘制鱼
screen.blit(wuguiImg, (tur.x, tur.y)) #绘制乌龟
#判断游戏是否结束:当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
if tur.power<0 or len(fish)==0:
print("Game Over ~")
sys.exit()
for item in fish:
# print("鱼",item.x,item.y,y_width,y_height)
# print("乌龟",tur.x,tur.y,w_width,w_height)
if ((tur.x < item.x + y_width) and (tur.x + w_width > item.x) and (tur.y < item.y + y_height) and (w_height + tur.y > item.y)) :
tur.eat()
fish.remove(item)
print("死了一只鱼")
print("乌龟最新体力值为 %d"%tur.power)
pygame.display.update() #更新到游戏窗口
原文链接:https://segmentfault.com/a/1190000012773811