鱼C论坛

 找回密码
 立即注册
查看: 1714|回复: 4

[已解决]零基础入门学习python中37讲作业,乌龟吃小鱼的游戏

[复制链接]
发表于 2017-12-21 17:55:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)
最佳答案
2017-12-21 18:11:45
本帖最后由 ba21 于 2017-12-21 18:27 编辑

删除一只后就可以退出 for i in range(number):
2017-12-21_182537.png

  1. import random

  2. class turtle:
  3.     x = random.randint(0,10)
  4.     y = random.randint(0,10)
  5.     life = 100
  6.     def move(self):
  7.         self.x += random.randint(-2,2)
  8.         self.y += random.randint(-2,2)
  9.         self.life -= 1
  10.     def check(self):
  11.         if self.x > 10 :
  12.             self.x -= 2
  13.         if self.x < 0:
  14.             self.x += 2
  15.         if self.y > 10:
  16.             self.y -= 2
  17.         if self.y < 0:
  18.             self.y += 2
  19.         if self.life > 100:
  20.             self.life = 100
  21. class fish:
  22.     x = random.randint(0,10)
  23.     y = random.randint(0,10)
  24.    
  25.     def move(self):
  26.         self.x += random.randint(-1,1)
  27.         self.y += random.randint(-1,1)
  28.       
  29.     def check(self):
  30.         if self.x > 10 :
  31.             self.x -= 1
  32.         if self.x < 0:
  33.             self.x += 1
  34.         if self.y > 10:
  35.             self.y -= 1
  36.         if self.y < 0:
  37.             self.y += 1
  38. tt = turtle()
  39. f0 = fish()
  40. f1 = fish()
  41. f2 = fish()
  42. f3 = fish()
  43. f4 = fish()
  44. f5 = fish()
  45. f6 = fish()
  46. f7 = fish()
  47. f8 = fish()
  48. f9 = fish()
  49. list1 = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
  50. number = 10
  51. while True:
  52.    
  53.     tt.move()
  54.     tt.check()
  55.     print('小乌龟位置是:%d,%d'%(tt.x,tt.y))
  56.     print('小乌龟生命值是:',tt.life)
  57.    
  58.     for i in range(number):
  59.         list1[i].move()
  60.         list1[i].check()      
  61.         
  62.         if list1[i].x == tt.x and list1[i].y == tt.y:
  63.             del list1[i]
  64.             number -= 1
  65.             tt.life += 20
  66.             break
  67.     if number < 0:
  68.         print('游戏结束,小鱼没了T T')
  69.         break
  70.     if tt.life <= 0 :
  71.         print('游戏结束,小乌龟饿死了T T')
  72.         break
  73.     lenth = len(list1)
  74.     print('现在还有%d条鱼'%lenth)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-12-21 18:11:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ba21 于 2017-12-21 18:27 编辑

删除一只后就可以退出 for i in range(number):
2017-12-21_182537.png

  1. import random

  2. class turtle:
  3.     x = random.randint(0,10)
  4.     y = random.randint(0,10)
  5.     life = 100
  6.     def move(self):
  7.         self.x += random.randint(-2,2)
  8.         self.y += random.randint(-2,2)
  9.         self.life -= 1
  10.     def check(self):
  11.         if self.x > 10 :
  12.             self.x -= 2
  13.         if self.x < 0:
  14.             self.x += 2
  15.         if self.y > 10:
  16.             self.y -= 2
  17.         if self.y < 0:
  18.             self.y += 2
  19.         if self.life > 100:
  20.             self.life = 100
  21. class fish:
  22.     x = random.randint(0,10)
  23.     y = random.randint(0,10)
  24.    
  25.     def move(self):
  26.         self.x += random.randint(-1,1)
  27.         self.y += random.randint(-1,1)
  28.       
  29.     def check(self):
  30.         if self.x > 10 :
  31.             self.x -= 1
  32.         if self.x < 0:
  33.             self.x += 1
  34.         if self.y > 10:
  35.             self.y -= 1
  36.         if self.y < 0:
  37.             self.y += 1
  38. tt = turtle()
  39. f0 = fish()
  40. f1 = fish()
  41. f2 = fish()
  42. f3 = fish()
  43. f4 = fish()
  44. f5 = fish()
  45. f6 = fish()
  46. f7 = fish()
  47. f8 = fish()
  48. f9 = fish()
  49. list1 = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
  50. number = 10
  51. while True:
  52.    
  53.     tt.move()
  54.     tt.check()
  55.     print('小乌龟位置是:%d,%d'%(tt.x,tt.y))
  56.     print('小乌龟生命值是:',tt.life)
  57.    
  58.     for i in range(number):
  59.         list1[i].move()
  60.         list1[i].check()      
  61.         
  62.         if list1[i].x == tt.x and list1[i].y == tt.y:
  63.             del list1[i]
  64.             number -= 1
  65.             tt.life += 20
  66.             break
  67.     if number < 0:
  68.         print('游戏结束,小鱼没了T T')
  69.         break
  70.     if tt.life <= 0 :
  71.         print('游戏结束,小乌龟饿死了T T')
  72.         break
  73.     lenth = len(list1)
  74.     print('现在还有%d条鱼'%lenth)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-21 18:15:38 From FishC Mobile | 显示全部楼层
具体的错误提示贴出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-21 19:29:12 | 显示全部楼层
ba21 发表于 2017-12-21 18:11
删除一只后就可以退出 for i in range(number):

谢谢大神!明白了一些,可是删除一只后,后边的不管了吗= =

     
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-21 19:47:13 | 显示全部楼层
ba21 发表于 2017-12-21 18:11
删除一只后就可以退出 for i in range(number):

看了小甲鱼的答案明白了~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-5 00:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表