zoukankan      html  css  js  c++  java
  • 类和对象(课后题)

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

    import random as r
    
    class Turtle:                 #初始化坐标定位及能力 
        def __init__(self):
            self.power = 100
            self.x = r.randint(0,10)
            self.y = r.randint(0,10)
    
        def move(self): 
            #随机计算位移并算出新的位置,0为横向移动,1为纵向移动
            self.direction = r.randint(0,1)
            if self.direction == 0:
                new_x = self.x + r.choice([1,-1,2,-2])
                new_y = self.y
                #移动后检查x轴是否超出边缘
                if new_x < 0:
                    self.x = 0 - new_x
                elif new_x > 10:
                    self.x = 10 - (new_x - 10)
                else:
                    self.x = new_x
            else:
                new_x = self.x
                new_y = self.y + r.choice([1,-1,2,-2])    
                #移动后检查y轴是否超出边缘
                if new_y < 0:
                    self.y = 0 - new_y
                elif new_y > 10:
                    self.y = 10 - (new_y - 10)
                else:
                    self.y = new_y
                    
            self.power -= 1
            return (self.x,self.y)
    
        def eat(self):
            self.power += 20
            if self.power > 100:
                self.power = 100
    
    class Fish:
        def __init__(self):
            self.x = r.randint(0,10)
            self.y = r.randint(0,10)
    
        def move(self):   #随机计算位移并算出新的位置,0为横向移动,1为纵向移动
            self.direction = r.randint(0,1)
            if self.direction == 0:
                new_x = self.x + r.choice([1,-1])
                new_y = self.y
            else:
                new_x = self.x
                new_y = self.y + r.choice([1,-1])
            #移动后检查x轴是否超出边缘
            if new_x < 0:
                self.x = 0 - new_x
            elif new_x > 10:
                self.x = 10 - (new_x - 10)
            else:
                self.x = new_x
            #移动后检查y轴是否超出边缘
            if new_y < 0:
                self.y = 0 - new_y
            elif new_y > 10:
                self.y = 10 - (new_y - 10)
            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 len(fish) == 0:
            print('小鱼仔都被吃完了,Game Over!')
            break
        if turtle.power == 0:
            print('乌龟体力被耗尽了,Game Over!')
            break
                                                       #开始游戏
        print('乌龟移动前的坐标:',(turtle.x,turtle.y))
        turtle.move()
        print('乌龟移动后的坐标:',(turtle.x,turtle.y))
        for f in fish:
            print('鱼移动前的坐标:',(f.x,f.y))
            f.move()
            print('鱼移动后的坐标:',(f.x,f.y))
            if f.x == turtle and f.y == turtle.y:
                turtle.eat()
                fish.remove(f)
                print('鱼被吃掉一条')
                print('乌龟现在体力值为:'% turtle.power)

    1.定义一个点(Point)类和直线(Line)类,使用getLen方法可以获得直线的长度。

    import random 
    import math 
    
    class Point:
        def __init__(self):
            self.x = random.randint(0,10)
            self.y = random.randint(0,10)
    
    class Line:
        def __init__(self,p1,p2):
            self.x = abs(p1.x - p2.x)
            self.y = abs(p1.y - p2.y)
            self.len = math.sqrt(self.x**2 + self.y**2)
        def getLen(self):
            return self.len
    
    p1 = Point()
    print('点1为:',(p1.x,p1.y))
    p2 = Point()
    print('点2为:',(p2.x,p2.y))
    line = Line(p1,p2)
    print('直线长为:',line.getLen())

    2.请动手在一个类中定义一个变量,用于跟踪类有多少个实例被创建,(当实例化一个对象,这个变量+1,当销毁一个对象,这个变量自动-1)

    class C:
        count = 0
        def __init__(self):
            C.count += 1
            print(C.count)
        def __del__(self):
            C.count -= 1
            print(C.count)

    3.定义一个栈(Stack)类,用于模拟一种具有后进先出(LIFO)特征的数据结构。至少需要有以下办法:

    方法名含义
    isEmpty() 判断当前栈是否为空(返回True或False)
    push() 往栈的顶部压入一个数据项
    pop() 从栈顶弹出一个数据项(并在栈中删除)
    top() 显示当前栈顶的一个数据项
    botton() 显示当前栈底的一个数据项
    class Stack:
        def __init__(self,start=[]):
            self.stack = []
                
        #判断当前栈是否为空(返回True或False)
        def isEmpty(self):
            return not bool(self.stack)
            
        #往栈顶压入一个数据
        def push(self,obj):
            print('入栈数据:',obj)
            self.stack.append(obj)
            
        #把栈顶数据弹出
        def pop(self):
            if self.stack:
                print('出栈数据:',self.stack[-1])
                self.stack.pop()
            else:
                raise LookupError('栈为空!')
            
        #显示当前栈顶数据
        def top(self):
            if self.stack:
                print('栈顶数据:',self.stack[-1])
            else:
                raise LookupError('栈为空!')
            
        #显示当前栈底数据
        def botton(self):
            if self.stack:
                print('栈底数据:',self.stack[0])
            else:
                raise LookupError('栈为空!')
            
        #显示栈的所有数据
        def show(self):
            if self.stack:
                print(self.stack)
            else:
                raise LookupError('栈为空!')
  • 相关阅读:
    get请求数据
    ajax (详细)
    DedeCMS去掉友情链接中“织梦链投放”“织梦链”的方法
    Metro UI CSS可以快速创建一个Windows 8风格的网站
    CSS实现垂直居中的5种方法
    解决jQuery冲突 noConflict
    dedecms pic_scroll.js和jquery-1.9.1.min.js
    js和jquery下拉菜单全攻略
    IE6、IE7、IE8、FireFox css line-height兼容问题
    IE开发利器-IE10中的F12开发者工具
  • 原文地址:https://www.cnblogs.com/mumulucky/p/13664268.html
Copyright © 2011-2022 走看看