1. 程式人生 > 實用技巧 >類和物件(課後題)

類和物件(課後題)

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('棧為空!')