鱼C论坛

 找回密码
 立即注册
查看: 4073|回复: 3

python中关于easygui做一个乌龟和鱼的游戏

[复制链接]
发表于 2017-2-26 10:44:35 | 显示全部楼层 |阅读模式
20鱼币
import random as r
import easygui as g

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

class Turtle:
    g.msgbox('你是乌龟,你的初始体力为100')
    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])
        g.msgbox('你的初始位置是:x = %s, y = %s' % (self.x,self.y))

    def move(self):
        msg = '请选择移动方向'
        title = None
        choices = ('上','下','左','右')
        direction = g.buttonbox(msg,title,choices)
        msg = '请选择移动距离'
        title = None
        choices = ('1','2')
        distance = g.buttonbox(msg,title,choices)
        distance = int(distance)
        if direction == '上':
            new_x = self.x
            new_y = self.y + distance
        if direction == '下':
            new_x = self.x
            new_y = self.y - distance
        if direction == '左':
            new_x = self.x - distance
            new_y = self.y 
        if direction == '右':
            new_x = self.x + distance
            new_y = self.y 

        #检查是否超出边界
        if new_x > 10 :
            self.x = 10 - (new_x - 10)
        elif new_x <0:
            self.x = -new_x
        else:
            self.x = new_x
        if new_y >10:
            self.y = 10 - (new_y - 10)
        elif new_y < 0:
            self.y = - new_y
        else:
            self.y = new_y

        
        #体力消耗
        self.power -= 10
        msg = '你现在所处位置是:x=%s,y=%s.你目前体力为:%d'% (new_x,new_y,self.power)
        g.msgbox(msg)
        
        return (self.x,self.y)
    
    def eat(self):
        self.power += 20
        if self.power > 100:
            self.power = 100

class Fish:
    def init_pos(self):
        #初始位置随机
        self.x = r.randint(legal_x[0],legal_x[1])
        self.y = r.randint(legal_y[0],legal_y[1])
        return self.x,self.y

    def move(self):
        new_x = self.x + r.choice([1,-1])
        new_y = self.y + r.choice([1,-1])
        #检查是否超出边界
        if new_x > 10 :
            self.x = 10 - (new_x - 10)
        elif new_x <0:
            self.x = -new_x
        else:
            self.x = new_x
        if new_y >10:
            self.y = 10 - (new_y - 10)
        elif new_y < 0:
            self.y = - new_y
        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)
    #此处本应该在easygui上显示每个小鱼的位置,但是我只会每个窗口弹出一个小鱼的位置
    print(new_fish.init_pos())


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

    pos = turtle.move()

    for each_fish in fish[:]:
        if each_fish.init_pos() == pos:
            turtle.eat()
            fish.remove(each_fish)
            g.msgbox('你吃掉了一条鱼')



这是我改编了一下乌龟吃鱼的代码,我想让它能够在easygui上实现,然而出了一些问题想要求教各位鱼油。
1、easygui是不是没法用格式化字符啊 就是%s 这种东西
2、我这里想要有一个box显示10条鱼的初始位置,能让玩家可以看到,但是发现我只能每次显示一个鱼的坐标,虽已被迫无奈用了print
3、我想设置为鱼都不要动,只有乌龟能动。然而我再把乌龟移动到鱼的位置后发现没能吃鱼,而有时候又莫名其妙的会吃到鱼。
4、发现乌龟到边界后并不会马上弹回来,比如我现在位置(0,9),向上移动两格,它会显示位置是(0,11),我再向上移动两格才会变成(0,9)。
麻烦各位能给我讲讲啦。

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

使用道具 举报

发表于 2017-2-27 10:05:23 | 显示全部楼层
没学过的亚历山大啊@小甲鱼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-3 21:10:26 | 显示全部楼层
不是太清晰,写的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-3-3 23:40:28 | 显示全部楼层
sunnychou 发表于 2017-3-3 21:10
不是太清晰,写的

啊是说代码吗,还是我的问题不太清晰
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 22:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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