|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wuqramy 于 2020-3-11 14:37 编辑
@Judie 又更新了哦!
经过一个星期的奋战,终于编出了"乌龟与小鱼"游戏,具体题目:
1、游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。(初学者不一定可以完整实现,但请务必先自己动手,你会从中学习到很多知识的)
a.假设游戏场景为范围(x,y)为0<=x<=10,0<=y<=10
b.游戏生成1只乌龟和10条鱼
c.它们的移动方向均随机
d.乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
e.当移动到场景边缘,自动向反方向移动
f.乌龟初始化体力为100(上限)
g.乌龟每移动一次,体力消耗1
h.当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
i.鱼暂不计算体力
j.当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
以下为代码:
- import random
- class TurtleandFish():
- x = 10
- y = 10
- turtle_count = 1
- def __init__(self):
- self.yao = 3
- self.turtlewz = [0,0]
- self.fishwz = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
- self.turtle_life = 50
- self.fish_count = 10
- self.txdire = 0
- self.tydire = 0
- self.fxdire = [0,0,0,0,0,0,0,0,0,0]
- self.fydire = [0,0,0,0,0,0,0,0,0,0]
- def turtlemove(self):
- movedire = 0
- movedire = random.randint(0,1)
- turtle_move = random.randint(1,2)
- if movedire == 0:
- if self.txdire == 0:
- self.turtlewz[0] += turtle_move
- if self.turtlewz[0] > 10:
- self.txdire = 1
- self.turtlewz[0] = 20 - self.turtlewz[0]
- else:
- self.turtlewz[0] -= turtle_move
- if self.turtlewz[0] < 0:
- self.txdire = 0
- self.turtlewz[0] = 0 - self.turtlewz[0]
- else:
- if self.tydire == 0:
- self.turtlewz[1] += turtle_move
- if self.turtlewz[1] > 10:
- self.tydire = 1
- self.turtlewz[1] = 20 - self.turtlewz[1]
- else:
- self.turtlewz[1] -= turtle_move
- if self.turtlewz[1] < 0:
- self.tydire = 0
- self.turtlewz[1] = 0 - self.turtlewz[1]
- self.turtle_life -= 1
- def fishmove(self):
- for i in range(self.fish_count):
- movedire = 0
- movedire = random.randint(0,1)
- if movedire == 0:
- if self.fxdire[i] == 0:
- self.fishwz[i][0] += 1
- if self.fishwz[i][0] > 10:
- self.fxdire[i] = 1
- self.fishwz[i][0] = 9
- else:
- self.fishwz[i][0] -= 1
- if self.fishwz[i][0] < 0:
- self.fxdire[i] = 0
- self.fishwz[i][0] = 1
- else:
- if self.fydire[i] == 0:
- self.fishwz[i][1] += 1
- if self.fishwz[i][1] > 10:
- self.fydire[i] = 1
- self.fishwz[i][1] = 9
- else:
- self.fishwz[i][1] -= 1
- if self.fishwz[i][1] < 0:
- self.fydire[i] = 0
- self.fishwz[i][1] = 1
- def fandtxy(self):
- n = 0
- for each in self.fishwz:
- if each == self.turtlewz:
- self.fish_count -= 1
- self.turtle_life += 20
- if self.turtle_life > 50:
- self.turtle_life = 50
- print(str(each) + '位置的小鱼被乌龟吃掉了,还剩' + str(self.fish_count) + '条小鱼')
- print('乌龟体力+20,乌龟还剩' + str(self.turtle_life) + '点体力')
- print('==============================================================')
- self.fishwz.remove(each)
- del self.fxdire[n]
- del self.fydire[n]
- n += 1
- print('剩下的' + str(self.fish_count) + '条小鱼正在移动...')
- print('乌龟正在寻找小鱼...,体力-1,剩余体力' + str(self.turtle_life))
- print('==============================================================')
- def gamestop(self):
- if self.turtle_life == 0:
- print('乌龟体力为0,小鱼胜利!')
- return True
- elif self.fish_count == 0:
- print('没有小鱼了,乌龟胜利!')
- return True
- else:
- return False
- def eatyao(self):
- if self.yao != 0:
- self.yao -= 1
- self.turtle_life += 10
- if self.turtle_life > 50:
- self.turtle_life = 50
- print('==============================================================')
- print('乌龟吃了药,体力+10,还剩' + str(self.turtle_life) + '点体力,乌龟还有' + str(self.yao) + '份药')
- print('==============================================================')
- else:
- print('==============================================================')
- print('乌龟没有药了!')
- print('==============================================================')
- a = TurtleandFish()
- print('====================欢迎来到乌龟与小鱼游戏!====================')
- print('''游戏规则:
- a.假设游戏场景为范围x和y
- b.游戏生成1只乌龟和10条小鱼
- c.它们的移动方向均随机
- d.乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),小鱼的最大移动能力是1
- e.当移动到场景边缘,自动向反方向移动
- f.乌龟初始化体力为50(上限)
- g.乌龟每移动一次,体力消耗1
- h.当乌龟和鱼坐标重叠,乌龟吃掉小鱼,乌龟体力增加20
- i.小鱼无体力
- j.当乌龟体力值为0或者小鱼的数量为0游戏结束
- k.游戏开始时,乌龟手持3份药,吃一份药可恢复10点体力''')
- input('按回车开始游戏')
- print('==============================================================')
- while True:
- a.turtlemove()
- a.fishmove()
- a.fandtxy()
- if a.gamestop():
- break
- n = input('是否吃药(y/n)?:')
- if n == 'y':
- a.eatyao()
- else:
- input('按回车继续游戏')
- print('==============================================================')
复制代码
游戏效果
(当时的我和老爸)
开心,终于可以开始下一课的学习了! |
|