|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
零基础入门学习python中37讲作业,乌龟吃小鱼的游戏,运行为什么老是显示索引错误。。。求大神解答
import random
class turtle:
x = random.randint(0,10)
y = random.randint(0,10)
life = 100
def move(self):
self.x += random.randint(-2,2)
self.y += random.randint(-2,2)
self.life -= 1
def check(self):
if self.x > 10 :
self.x -= 2
if self.x < 0:
self.x += 2
if self.y > 10:
self.y -= 2
if self.y < 0:
self.y += 2
if self.life > 100:
self.life = 100
class fish:
x = random.randint(0,10)
y = random.randint(0,10)
def move(self):
self.x += random.randint(-1,1)
self.y += random.randint(-1,1)
def check(self):
if self.x > 10 :
self.x -= 1
if self.x < 0:
self.x += 1
if self.y > 10:
self.y -= 1
if self.y < 0:
self.y += 1
tt = turtle()
f0 = fish()
f1 = fish()
f2 = fish()
f3 = fish()
f4 = fish()
f5 = fish()
f6 = fish()
f7 = fish()
f8 = fish()
f9 = fish()
list1 = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
number = 10
while True:
tt.move()
tt.check()
print('小乌龟位置是:%d,%d'%(tt.x,tt.y))
print('小乌龟生命值是:',tt.life)
for i in range(number):
list1[i].move()
list1[i].check()
if list1[i].x == tt.x and list1[i].y == tt.y:
del list1[i]
number -= 1
tt.life += 20
if number < 0:
print('游戏结束,小鱼没了T T')
break
if tt.life <= 0 :
print('游戏结束,小乌龟饿死了T T')
break
lenth = len(list1)
print('现在还有%d条鱼'%lenth)
本帖最后由 ba21 于 2017-12-21 18:27 编辑
删除一只后就可以退出 for i in range(number):
- import random
- class turtle:
- x = random.randint(0,10)
- y = random.randint(0,10)
- life = 100
- def move(self):
- self.x += random.randint(-2,2)
- self.y += random.randint(-2,2)
- self.life -= 1
- def check(self):
- if self.x > 10 :
- self.x -= 2
- if self.x < 0:
- self.x += 2
- if self.y > 10:
- self.y -= 2
- if self.y < 0:
- self.y += 2
- if self.life > 100:
- self.life = 100
- class fish:
- x = random.randint(0,10)
- y = random.randint(0,10)
-
- def move(self):
- self.x += random.randint(-1,1)
- self.y += random.randint(-1,1)
-
- def check(self):
- if self.x > 10 :
- self.x -= 1
- if self.x < 0:
- self.x += 1
- if self.y > 10:
- self.y -= 1
- if self.y < 0:
- self.y += 1
- tt = turtle()
- f0 = fish()
- f1 = fish()
- f2 = fish()
- f3 = fish()
- f4 = fish()
- f5 = fish()
- f6 = fish()
- f7 = fish()
- f8 = fish()
- f9 = fish()
- list1 = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
- number = 10
- while True:
-
- tt.move()
- tt.check()
- print('小乌龟位置是:%d,%d'%(tt.x,tt.y))
- print('小乌龟生命值是:',tt.life)
-
- for i in range(number):
- list1[i].move()
- list1[i].check()
-
- if list1[i].x == tt.x and list1[i].y == tt.y:
- del list1[i]
- number -= 1
- tt.life += 20
- break
- if number < 0:
- print('游戏结束,小鱼没了T T')
- break
- if tt.life <= 0 :
- print('游戏结束,小乌龟饿死了T T')
- break
- lenth = len(list1)
- print('现在还有%d条鱼'%lenth)
复制代码
|
|