鱼C论坛

 找回密码
 立即注册
查看: 730|回复: 2

求助失败原因

[复制链接]
发表于 2019-3-15 20:49:41 | 显示全部楼层 |阅读模式

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

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

x
原题:
游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束

个人代码:
import random
class Fish:
    x = random.randint(1,10)
    y = random.randint(1,10)
    def move(self):
        a = random.randint(1,4)
        if a ==1:
            if self.x != 10:
                self.x += 1
            else:
                self.x -= 1
        elif a == 2:
            if self.y != 10:
                self.y += 1
            else:
                self.y -= 1
        elif a == 3:
            if self.x != 0:
                self.x -= 1
            else:
                self.x +=1
        elif a == 4:
            if self.y != 0:
                self.y -= 1
            else:
                self.y += 1
        return (self.x,self.y)
      

class Turtle:
    health = 100
    x = random.randint(1,10)
    y = random.randint(1,10)
    def move(self):
        b = random.randint(1,4)
        step = random.randint(1,2)
        if b ==1:
            if self.x != 10 and self.x != 9:
                self.x += step
            elif self.x == 10:
                self.x -= step
            elif self.x == 9 and step == 1:
                self.x += step
        elif b == 2:
            if self.y != 10 and self.y != 9:
                self.y += step
            elif self.y == 10:
                self.y -= step
            elif self.y == 9 and step == 1:
                self.y += step
        elif b == 3:
            if self.x != 0 and self.x != 1:
                self.x -= step
            elif self.x == 0:
                self.x += step
            elif self.x == 1 and step == 1:
                self.x -= step
        elif b == 4:
            if self.y != 0 and self.y != 1:
                self.y -= step
            elif self.y == 0:
                self.y += step
            elif self.y == 1 and step ==1:
                self.y -= step
        self.health -= 1
        return (self.x,self.y)


t = Turtle()
a = Fish()
b = Fish()
c = Fish()
d = Fish()
e = Fish()
f = Fish()
g = Fish()
h = Fish()
i = Fish()
j = Fish()
count = 10
   

while True:
    if t.health == 0:
        print("游戏结束,乌龟被饿死了")
        break
    if count == 0:
        print("游戏结束,小鱼被吃光了")
        break
    t.move()
    for each in [a,b,c,d,e,f,g,h,i,j]:
        each.move()
        if each.x == t.x and each.y == t.y:
            del each
            count -= 1
            t.health += 20
            if t.health > 100:
                t.health = 100
            print("有一条小鱼被吃掉了")

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

使用道具 举报

发表于 2019-3-16 11:25:03 | 显示全部楼层
显示什么错误?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-16 13:27:17 | 显示全部楼层
本帖最后由 伏惜寒 于 2019-3-16 13:29 编辑

你把定义类当成定义函数用了
定义一个类不是这样用的
参考飞机大战生成敌机的类方法来修改代码吧
  1. class SmallEnemy(pygame.sprite.Sprite):
  2.     #魔法方法,传参自动调用
  3.     def __init__(self, bg_size):
  4.         pygame.sprite.Sprite.__init__(self)

  5.         self.image = pygame.image.load("images/enemy1.png").convert_alpha()
  6.         self.destroy_images = []
  7.         self.destroy_images.extend([\
  8.             pygame.image.load("images/enemy1_down1.png").convert_alpha(),\
  9.             pygame.image.load("images/enemy1_down2.png").convert_alpha(),\
  10.             pygame.image.load("images/enemy1_down3.png").convert_alpha(),\
  11.             pygame.image.load("images/enemy1_down4.png").convert_alpha()])
  12.         self.rect = self.image.get_rect()
  13.         self.width, self.height = bg_size[0], bg_size[1]
  14.         #初始速度为2
  15.         self.speed = 2
  16.         self.active = True
  17.         #在屏幕上方生成
  18.         self.rect.left, self.rect.top = \
  19.                         randint(0,self.width - self.rect.width), \
  20.                         randint(-5 * self.height, 0)
  21.         self.mask = pygame.mask.from_surface(self.image)
  22.         
  23.     def move(self):
  24.         #使敌方飞机移动
  25.         if self.rect.top < self.height:
  26.             self.rect.top += self.speed
  27.         else:
  28.             self.reset()

  29.     def reset(self):
  30.         #屏幕上方随机生成敌机
  31.         self.active = True
  32.         self.rect.left, self.rect.top = \
  33.                         randint(0,self.width - self.rect.width), \
  34.                         randint(-5 * self.height,0)
复制代码

__init__(self)是魔法方法,传递参数用
如果需要的话我可以把你这个程序的修改完成的代码发给你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-14 14:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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