def __init__(self): super(PlayState, self).__init__() self.ducks = [Duck(self.registry), Duck(self.registry)] self.roundTime = 10 # Seconds in a round self.frame = 0 self.dogCanComeOut = False self.dogPosition = DOG_REPORT_POSITION self.dogdy = 5
def __init__(self): super(PlayState, self).__init__() #amount of ducks self.ducks = [Duck(self.registry), Duck(self.registry)] #amt of ducks plus 2^ xyz = 0 while xyz < config_master.amtDucks: self.ducks.append(Duck(self.registry)) xyz = xyz + 1 self.roundTime = config_master.timer # Seconds in a round self.frame = 0 self.dogCanComeOut = False self.dogPosition = DOG_REPORT_POSITION self.dogdy = 5
def run_game(): pygame.init() screen = pygame.display.set_mode() pygame.display.set_caption("12-2 游戏角色") duck = Duck(screen) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill([0, 206, 255]) duck.blitme() # 让最近绘制的屏幕可见 pygame.display.flip()
def get_animal(self, animal_type): """Factory method for the Animal objects. Args: animal_type: instance of a str to identify the type. Returns: Initialised animal object. """ if animal_type is not None and type(animal_type) == str: animal_type = animal_type.lower() if animal_type is None: return NullAnimal() elif animal_type == "duck": self.__factory_objects += 1 return Duck() elif animal_type == "dog": self.__factory_objects += 1 return Dog() elif animal_type == "cat": self.__factory_objects += 1 return Cat() else: return NullAnimal()
def generateDucks(self): if(len(self.duckList) < self.maxDucks): if(self.timeInMillis() - self.lastDuckSpawn > self.randomDuckTimer): self.lastDuckSpawn = self.timeInMillis() self.randomDuckTimer = random.randint(self.randomDuckMin, self.randomDuckMax) duckSpeedX = random.randint(self.duckSpeedMin, self.duckSpeedMax) duckSpeedY = random.randint(self.duckSpeedMin, -1) randomPosX = random.randint(self.duckSize, self.screenX-self.duckSize) posY = self.screenY - self.screenY/5 - self.duckSize - 2 self.duckList.append(Duck(randomPosX, posY, duckSpeedX, duckSpeedY))
from duck import Duck piyosuke = Duck("piyosuke") piyosuke.say()
def render(self): timer = int(time.time()) surface = self.registry.get('surface') font = pygame.font.SysFont("Arial", 16, bold=True) #Font / size PEARDECK text = font.render( "Variables used: Outside & inside temp, setpoints, window tint, Building kW/h", True, (255, 255, 255)) #RGB values / text color text2 = font.render("Effects: Speed of ducks, timer, bullets, score", True, (255, 255, 255)) #surface.blit(text,(320 - text.get_width() // 2, 240 - text.get_height() // 2)) #Location surface.blit(text, ( 300, 400, )) #changes location surface.blit(text2, (300, 420)) sprites = self.registry.get('sprites') # Show the controls self.renderControls() # Show the ducks for duck in self.ducks: duck.render() # Show the dog if self.dogCanComeOut: self.frame += 3 ducksShot = 0 for duck in self.ducks: if duck.isDead: ducksShot += 1 # One duck shot if ducksShot == 1: x2, y2, width, height = DOG_ONE_DUCK_RECT if self.frame == 3: self.registry.get('soundHandler').enqueue('hit') # Two ducks shot elif ducksShot == 2: x2, y2, width, height = DOG_TWO_DUCKS_RECT if self.frame == 3: self.registry.get('soundHandler').enqueue('hit') # No ducks shot else: x2, y2, width, height = DOG_LAUGH_RECT if self.frame == 3: self.registry.get('soundHandler').enqueue('flyaway') # Dog comes up and goes back down x1, y1 = self.dogPosition if self.frame < height: self.dogPosition = x1, (y1 - 3) height = self.frame else: self.dogPosition = x1, (y1 + 3) height -= (self.frame - height) # If dog is no longer visible, move on if height <= 0: self.dogPosition = DOG_REPORT_POSITION self.frame = 0 self.dogCanComeOut = False self.ducks = [Duck(self.registry), Duck(self.registry)] self.timer = timer self.gun.reloadIt() else: surface.blit(sprites, self.dogPosition, (x2, y2, width, height)) # Show the cross hairs self.gun.render()
# by Chris Proctor # This file tests the Duck class, which you will have to create. Make sure duck.py # is in the same folder so it can be imported. from duck import Duck from animal import Animal class Ogre(Animal): species = "ogre" def expect(expectation, message): if not expectation: raise Exception("A test failed: {}".format(message)) daffy = Duck("Daffy") diffy = Duck("Diffy") ogre = Ogre("Oggie") duck_encounter = daffy.encounter(diffy) ogre_encounter = daffy.encounter(ogre) expect(daffy.name == "Daffy", "Daffy's name should be Daffy") expect(daffy.species == "duck", "Daffy's species should be 'duck'") expect("{}".format(daffy) == "Daffy the duck", "Daffy's string representation should be 'Daffy the duck'") expect(duck_encounter['action'] == None, "Ducks should not have actions when they meet each other") expect(duck_encounter['message'] == "Quack! Daffy sees Diffy!", "The message is wrong for when Daffy meets Diffy") expect(ogre_encounter['action'] == 'die',
from duck import Duck from behaviors import quackQuietly, FlyMoon donald = Duck() donald.quack() donald.fly() donald.setFlyBehavior(FlyMoon) donald.setQuackBehavior(quackQuietly) donald.quack() donald.fly()
def test_duckCanQuack(self): test_duck = Duck(feathers='green') self.assertEqual('Quaaack!', test_duck.quack())
# -*- coding: utf-8 -*- __author__ = 'vladimir.pekarsky' from duck import Duck, SpeechlessDuck class FlyingDuck(Duck): def fly(self): print("{} is flying".format(self.name)) if __name__ == "__main__": duck_list = [ Duck("McDuck"), SpeechlessDuck("SpeechlessDuck"), FlyingDuck("Zigzag McDuck"), ] for duck in duck_list: duck.say("hello") print("What about flying?") for duck in duck_list: duck.fly()
from duck import Duck from quack_behavior import SimpleQuacking, AdvancedQuacking, NoQuacking from fly_behavior import SimpleFlying, JetFlying, NoFlying if __name__ == "__main__": wild_duck = Duck(SimpleFlying, AdvancedQuacking) city_duck = Duck(JetFlying, SimpleQuacking) rubber_duck = Duck(NoFlying, NoQuacking) wild_duck.fly() city_duck.fly() rubber_duck.fly() wild_duck.quack() city_duck.quack() rubber_duck.quack()
d.say_hello() d.get_number_of_legs() d.how_do_i_move() d.how_much_do_i_weigh() d.am_i_extinct() if What_animal == "Cat": c = Cat(4) c.say_hello() c.get_number_of_legs() c.how_do_i_move() c.how_much_do_i_weigh() c.am_i_extinct() if What_animal == "Duck": d = Duck(2) d.say_hello() d.get_number_of_legs() d.how_do_i_move() d.how_much_do_i_weigh() d.am_i_extinct() if What_animal == "Bird": b = Bird(2) b.say_hello() b.get_number_of_legs() b.how_do_i_move() b.how_much_do_i_weigh() b.am_i_extinct() if What_animal == "Giraffe":
# is in the same folder so it can be imported. from duck import Duck from animal import Animal class Ogre(Animal): species = "ogre" def expect(expectation, message): if not expectation: raise Exception("A test failed: {}".format(message)) daffy = Duck("Daffy") diffy = Duck("Diffy") ogre = Ogre("Oggie") duck_encounter = daffy.encounter(diffy) ogre_encounter = daffy.encounter(ogre) expect(daffy.name == "Daffy", "Daffy's name should be Daffy") expect(daffy.species == "duck", "Daffy's species should be 'duck'") expect("{}".format(daffy) == "Daffy the duck", "Daffy's string representation should be 'Daffy the duck'") expect(duck_encounter['action'] == None, "Ducks should not have actions when they meet each other") expect(duck_encounter['message'] == "Quack! Daffy sees Diffy!", "The message is wrong for when Daffy meets Diffy") expect(ogre_encounter['action'] == 'die',
def __init__(self): Duck.__init__(self) self.setFlyBehavior(FlyWithWings()) self.setQuackBehavior(RealQuack())
def render(self): timer = int(time.time()) surface = self.registry.get('surface') sprites = self.registry.get('sprites') # Show the controls self.renderControls() # Show the ducks for duck in self.ducks: duck.render() # Show the dog if self.dogCanComeOut: self.frame += 3 ducksShot = 0 for duck in self.ducks: if duck.isDead: ducksShot += 1 # One duck shot if ducksShot == 1: x2, y2, width, height = DOG_ONE_DUCK_RECT if self.frame == 3: self.registry.get('soundHandler').enqueue('hit') # Two ducks shot elif ducksShot == 2: x2, y2, width, height = DOG_TWO_DUCKS_RECT if self.frame == 3: self.registry.get('soundHandler').enqueue('hit') # No ducks shot else: x2, y2, width, height = DOG_LAUGH_RECT if self.frame == 3: self.registry.get('soundHandler').enqueue('flyaway') # Dog comes up and goes back down x1, y1 = self.dogPosition if self.frame < height: self.dogPosition = x1, (y1 - 3) height = self.frame else: self.dogPosition = x1, (y1 + 3) height -= (self.frame - height) # If dog is no longer visible, move on if height <= 0: self.dogPosition = DOG_REPORT_POSITION self.frame = 0 self.dogCanComeOut = False self.ducks = [Duck(self.registry), Duck(self.registry)] self.timer = timer self.gun.reloadIt() else: surface.blit(sprites, self.dogPosition, (x2, y2, width, height)) # Show the cross hairs self.gun.render()
def test_newDuck(self): test_duck = Duck(feathers='green') self.assertEqual(True, type(test_duck) is Duck) self.assertEqual('green', test_duck.get_variable('feathers'))
from wallet import Wallet from duckcoin import Duckcoin from duck import Duck from hashutils import hash_object, encoded_hash_object from transaction import CoinCreation, Payment import json import os if __name__ == '__main__': duck = Duck() user_dir = './user' tran_dir = './transaction' block_dir = './block' key_dir = './keypair' for file in os.listdir(user_dir): if file.endswith('.json'): file_name = user_dir + "/" + file with open(file_name, encoding='utf_8_sig') as f: d = json.load(f) user = d['user'] value = d['default_value'] f.close() user_name = user user = Wallet() coins = [Duckcoin(value=value, wallet_id=user.id)] duck.create_coins(coins)
def __init__(self): Duck.__init__(self) self.setFlyBehavior(FlyNoWay()) self.setQuackBehavior(NoQuack())
#!/usr/bin/env python # (c)2015 John Strickler from duck import Duck from quacklib import MuteQuack, Squeak from flylib import Flightless d1 = Duck('Mallard') d2 = Duck('Robot', quack=MuteQuack()) d3 = Duck('Rubber Duckie', quack=Squeak(), fly=Flightless()) for d in d1, d2, d3: d.display() d.quack() d.fly() print('-' * 60)