zoukankan      html  css  js  c++  java
  • Python 基础实战 -- 小游戏之乌龟吃鱼(其实只能看不能玩.....)

      1 import random
      2 import os
      3 import time
      4 
      5 class Fish(object):
      6 
      7     coord = {}
      8     moveSpeep = 0
      9     
     10     def __init__(self,posX,posY,moveSpeep = 1):
     11         self.moveSpeep = 1
     12         self.coord = { "X":posX, "Y":posY}
     13 
     14     def move(self, direction, scene = (10,10)):
     15         speep = random.randint(1,self.moveSpeep)
     16 
     17         while not direction == "":
     18             if direction == "":
     19                 
     20                 if self.coord["Y"] - speep < 0:
     21                     direction = ""
     22                     
     23                 else:
     24                     self.coord["Y"] = self.coord["Y"] - speep
     25                     direction = ""
     26 
     27             if direction == "":
     28                 
     29                 if self.coord["Y"] + speep >= scene[1]:
     30                     direction = ""
     31                 else:
     32                     self.coord["Y"] = self.coord["Y"] + speep
     33                     direction = ""
     34                 
     35             if direction == "":
     36                 
     37                 if self.coord["X"] - speep < 0:
     38                     direction = ""
     39                 else:
     40                     self.coord["X"] = self.coord["X"] - speep
     41                     direction = ""
     42                     
     43             if direction == "":
     44                
     45                 if self.coord["X"] + speep >= scene[0]:
     46                     direction = ""
     47                 else:
     48                     self.coord["X"] = self.coord["X"] + speep
     49                     direction = ""
     50     
     51 
     52     def getPos(self):
     53         return self.coord["X"],self.coord["Y"]
     54 
     55 #定义乌龟类
     56 class Tortoise(Fish):
     57 
     58     def __init__(self, posX, posY):
     59         self.__power = 100
     60         Fish.__init__(self, posX, posY,moveSpeep = 2)
     61 
     62     def move(self,direction,scene):
     63         self.__power -= 1
     64         Fish.move(self,direction,scene)
     65     
     66     def eat(self):
     67         self.__power += 20
     68 
     69     def death(self):
     70         isDie = False if self.__power >= 0 else True
     71         return isDie
     72 
     73 #生成游戏角色(乌龟和鱼)
     74 def createRole(tortoiseCount = 1,fishCount = 10, scene = (10,10)):
     75     coordList = []
     76     tortoises = []
     77     fishs = []
     78     while not len(coordList) >= tortoiseCount + fishCount:
     79         pos = random.randint(0,scene[0]- 1),random.randint(0,scene[1] - 1)
     80         if pos not in coordList:
     81             coordList.append(pos)
     82     else:
     83         while tortoiseCount:
     84             #生成一只乌龟
     85             posX,posY = coordList.pop()
     86             tortoises.append(Tortoise(posX,posY))
     87             tortoiseCount -= 1
     88         while len(coordList):
     89             #生成十只鱼
     90             posX,posY = coordList.pop()
     91             fishs.append(Fish(posX,posY))
     92     return tortoises,fishs
     93 
     94 
     95 #主游戏程序
     96 def main(scene = (10,10)):
     97     
     98     tortoises,fishs = createRole(scene = (10,10))
     99     try:
    100         #初始化地图定义一个二维数组用来表示地图
    101         scene_map = []
    102         for i in range(scene[1]):
    103             scene_map.append(["" for x in range(scene[0])])
    104 
    105         for tortoise in tortoises:
    106             posX,posY = tortoise.getPos()
    107             scene_map[posY][posX] = ""
    108         for fish in fishs:
    109             posX,posY = fish.getPos()
    110             scene_map[posY][posX] = ""
    111         tips = ""
    112         while True:
    113             time.sleep(0.05)
    114             #清屏
    115             os.system("cls")
    116             
    117             for i in range(len(scene_map)):
    118                 for j in range(len(scene_map[i])):
    119                     print(scene_map[j][i],end=(""))
    120                 print()
    121 
    122             print(tips)
    123             
    124             for tortoise in tortoises:
    125                 #乌龟移动
    126                 direction = random.sample("上下左右",1)
    127                 tortoise.move(direction[0],scene)
    128 
    129                 #如果坐标与鱼的坐标重叠,乌龟吃掉鱼
    130                 for fish in fishs:
    131                     if fish.getPos() == tortoise.getPos():
    132                         tortoise.eat()
    133                         fishs.remove(fish)
    134                         tips += "有一只鱼被吃掉了!
    "
    135 
    136                 if tortoise.death():
    137                     tortoises.remove(tortoise)
    138                     tips += "乌龟体力耗尽死掉了!
    "
    139                         
    140             for fish in fishs:
    141                 #鱼移动
    142                 direction = random.sample("上下左右",1)
    143                 fish.move(direction[0],scene)
    144                 
    145                 #如果坐标与鱼的坐标重叠,乌龟吃掉鱼
    146                 for tortoise in tortoises:
    147                     if fish.getPos() == tortoise.getPos():
    148                         tortoise.eat()
    149                         fishs.remove(fish)
    150                         tips += "有一只鱼被吃掉了!
    "
    151 
    152             for i in range(len(scene_map)):
    153                 for j in range(len(scene_map[i])):
    154                     scene_map[j][i] = ""
    155             
    156             for tortoise in tortoises:
    157                 posX,posY = tortoise.getPos()
    158                 scene_map[posY][posX] = ""
    159             for fish in fishs:
    160                 posX,posY = fish.getPos()
    161                 scene_map[posY][posX] = ""
    162 
    163             if len(fishs) == 0 or len(tortoises) == 0:
    164                 break
    165                                         
    166     except:
    167         print("游戏程序产生异常!")
    168     finally:
    169         print("游戏结束!")
    170 
    171 
    172 main()
  • 相关阅读:
    arcgis10.2 sde配置
    创建只读账号oracle
    oracle 的tnsnames.ora,listener.ora
    gp工具的许可
    复制整个数据库
    显示二维表格的位置计算
    windows文件名格式的中文+数字混合字符串排序
    supergridcontrol记录,分页
    outlook-由于本机的限制,该操作已被取消。请与系统管理员联系。
    自定义单元格式
  • 原文地址:https://www.cnblogs.com/jiangchenxi/p/8058155.html
Copyright © 2011-2022 走看看