鱼C论坛

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

[已解决]零基础学Python 37章课后作业 游戏编程

[复制链接]
发表于 2020-11-26 16:38:59 | 显示全部楼层 |阅读模式

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

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

x
import random as r

legal_x = [0,10]
legal_y = [0,10]

class Turtle:
    def __init__(self):
        self.power = 100
        self.x = r.randint(legal_x[0],legal_x[1])
        self.y = r.randint(legal_y[0],legal_y[1])

    def move(self):
        direction = r.randint(0,1)
        if direction == 0:
            new_x = self.x + r.choice([-2,-1,1,2])
            if new_x < legal_x[0]:
                self.x = legal_x[0] -( new_x - legal_x[0] )
            elif new_x > legal_x[1]:
                self.x = legal_x[1] -( new_x - legal_x[1] )
            else:
                self.x = new_x
        else:
            new_y = self.y + r.choice([-2,-1,1,2])
            new_x = self.x
            if new_y < legal_y[0]:
                self.y = legal_y[0] -( new_y - legal_y[0] )
            elif new_y > legal_y[1]:
                self.y = legal_y[1] -( new_y - legal_y[1] )
            else:
                self.y = new_y
        self.power -=  1
        return (self.x,self.y)

    def eat(self):
        self.power += 20
        if self.power > 200:
            self.power = 200

class Fish:
    def __init__(self):
        self.x = r.randint(legal_x[0],legal_x[1])
        self.y = r.randint(legal_y[0],legal_y[1])

    def move(self):
        direction = r.randint(0,1)
        if direction == 0:
            new_x = self.x + r.choice([-1,1])
            if new_x < legal_x[0]:
                self.x = legal_x[0] -( new_x - legal_x[0] )
            elif new_x > legal_x[1]:
                self.x = legal_x[1] -( new_x - legal_x[1] )
            else:
                self.x = new_x
        else:
            new_y = self.y + r.choice([-1,1])
            if new_y < legal_y[0]:
                self.y = legal_y[0] -( new_y - legal_y[0] )
            elif new_y > legal_y[1]:
                self.y = legal_y[1] -( new_y - legal_y[1] )
            else:
                self.y = new_y
        return (self.x,self.y)

turtle=Turtle()
fish = []
for i in range(10):
    new_fish = Fish()
    fish.append(new_fish)

while True:
    if not len(fish):
        print('鱼儿都吃完了,游戏结束')
        break
    if not turtle.power:
        print('乌龟体力耗尽,游戏结束')
        break

    pos = turtle.move()
    for each_fish in fish[:]:
        if each_fish.move == pos:
            turtle.eat()
            fish.remove(each_fish)
            print('有一条鱼被吃掉了')

各位大神想知道这样写的那里有问题,改的基本上和小甲鱼的代码一样了,还是每次都把乌龟累死
最佳答案
2020-11-26 16:45:36


最后 for 循环的代码,调用鱼的 move 方法忘记加括号导致的

for 循环代码改成这样即可:

  1. for each_fish in fish[:]:
  2.     if each_fish.move() == pos:
  3.         turtle.eat()
  4.         fish.remove(each_fish)
  5.         print('有一条鱼被吃掉了')
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-26 16:45:36 | 显示全部楼层    本楼为最佳答案   


最后 for 循环的代码,调用鱼的 move 方法忘记加括号导致的

for 循环代码改成这样即可:

  1. for each_fish in fish[:]:
  2.     if each_fish.move() == pos:
  3.         turtle.eat()
  4.         fish.remove(each_fish)
  5.         print('有一条鱼被吃掉了')
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-26 16:51:10 | 显示全部楼层
万分感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-26 17:15:13 | 显示全部楼层
这个游戏不平衡,乌龟大多数都赢不了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-26 21:11:08 | 显示全部楼层
m496447738 发表于 2020-11-26 17:15
这个游戏不平衡,乌龟大多数都赢不了

那就把场景弄小一点哈哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 21:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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