zoukankan      html  css  js  c++  java
  • hausaufgabe--python 37 -- self in Class

    00-- requirements:
    a. new a Turtle and 10 fishes with the moving area of x(0,10), y(0,10)
    b. turtle can move 1 or 2 step within the defined area and fish with 1 step, every step will result in turtle power minus 1 with original = 100
    c. while the location of turtle is the same as fish, then turtle will eat the fish with 20 power increased.
    d. finish the game when fishes have been eaten all or power = 0.
    import random as r
    
    legal_x = [0, 10]
    legal_y = [0, 10]
    
    class Turtle:
        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])
    
        def move(self):
             
            new_x = self.x + r.choice([1, 2, -1, -2])
            new_y = self.y + r.choice([1, 2, -1, -2])
            # check if exceed the x boundary
            if new_x < legal_x[0]:
                self.x = legal_x[0] - (new_x - legal_x[0])
            elif new_x > legal_x[1]:
                self.x = legal_x[1] - (new_x - legal_x[1])
            else:
                self.x = new_x
            # check if exceed the y boundary
            if new_y < legal_y[0]:
                self.y = legal_y[0] - (new_y - legal_y[0])
            elif new_y > legal_y[1]:
                self.y = legal_y[1] - (new_y - legal_y[1])
            else:
                self.y = new_y        
            # power reduction
            self.power -= 1
            # return new location after movement
            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(legal_x[0], legal_x[1])
            self.y = r.randint(legal_y[0], legal_y[1])
            
        def move(self):
            # calculate the direction and move to new location
            new_x = self.x + r.choice([1, -1])
            new_y = self.y + r.choice([1, -1])
            # check if exceed the x boundary
            if new_x < legal_x[0]:
                self.x = legal_x[0] - (new_x - legal_x[0])
            elif new_x > legal_x[1]:
                self.x = legal_x[1] - (new_x - legal_x[1])
            else:
                self.x = new_x
            # check if exceed the y boundary
            if new_y < legal_y[0]:
                self.y = legal_y[0] - (new_y - legal_y[0])
            elif new_y > legal_y[1]:
                self.y = legal_y[1] - (new_y - legal_y[1])
            else:
                self.y = new_y
            # return new location
            return (self.x, self.y)
    
    turtle = Turtle()
    fish = []
    for i in range(10):
        new_fish = Fish()
        fish.append(new_fish)
    
    while True:
        if not len(fish):
            print("No Fish, Game over!")
            break
        if not turtle.power:
            print("No power for Turtle, Game over!")
            break
    
        pos = turtle.move()
        # 在迭代器中删除列表元素是非常危险的,经常会出现意想不到的问题,因为迭代器是直接引用列表的数据进行引用
        # 这里我们把列表拷贝给迭代器,然后对原列表进行删除操作就不会有问题了^_^
        for each_fish in fish[:]:
            if each_fish.move() == pos:
                # fish be eaten
                turtle.eat()
                fish.remove(each_fish)
                print("there is 1 fish has been eaten...")

    01--Define a class for ticket price calculation

    Running Result:

     

    referred:

    http://outofmemory.cn/code-snippet/3841/python-datetime-weekday-get-day-of-week

    class Person:
        __name = 'Reedy'
    
        def getName(self):
            return self.__name

    running result:

  • 相关阅读:
    Memcached Tip 1:使用Memcached Providers
    MVC TIP8:为控制器增加有参构造函数(为了注入等其它用途)
    压力测试的轻量级具体做法
    Memcached Tip 2:Session同步
    ASP.NET性能优化之分布式Session
    ASP.NET性能优化之让浏览器缓存动态网页
    最精简领域驱动设计开发模版(针对WPF)
    MOQ TIP1:简介加基础
    ASP.NET性能优化之减少请求
    MOQ TIP2:匹配参数
  • 原文地址:https://www.cnblogs.com/Shareishappy/p/7482170.html
Copyright © 2011-2022 走看看