|
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)。
麻烦各位能给我讲讲啦。 |
|