Created
July 12, 2018 12:27
-
-
Save amigojapan/e6d5f6a5797b22ce7da1bf5c807e50c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Fundamentals of OOP by amigojapan (Usmar Padow usmpadow@gmail.com ) copyright 2018 | |
##Classes | |
##A class is like a cookie cutter, it will cut similar cookies for you | |
##the purpose of classes is mainly to organize data into coherent | |
##lumps, and sometimes to model the real world | |
##suppose you are making a game where you have penguins and ducks flying desperately over the ocean | |
##lets see how we can model such a scenario: | |
import random | |
import pyglet | |
duck_image = pyglet.image.load('duck_right.png') | |
penguin_image = pyglet.image.load('penguin_left.png') | |
screen_width=300 | |
screen_height=300 | |
class Bird(): | |
life=100 | |
power=100 | |
direction="right" | |
x=100 | |
y=100 | |
name="undefined bird"; | |
def flap_wings(self): | |
print("flappy flap flap") | |
def move_foward(self): | |
if(self.x>screen_width): | |
self.direction="left" | |
elif(self.x<0): | |
self.direction="right" | |
if(self.direction=="right"): | |
self.x=self.x+10##bird moves forward | |
else: | |
self.x=self.x-10##penguin moves forward | |
def show_data(self): | |
print(self.name + " at x:"+str(self.x)+" y:"+str(self.y)+" direction:"+self.direction+" power"+str(self.power)) | |
def apply_gravity(self): | |
self.y=self.y-3 ##bird falls down | |
class Penguin(Bird): ##this means the Penguins inherit the properties of birds | |
def flap_wings(self): ##we override the bird's native function so that we can give it different behaviour | |
print("flappy flap flap penguin") | |
if(self.y<0):##if the penguins are under the water | |
self.y=self.y-1##penguin swims up | |
self.move_foward() | |
self.power=self.power-5##the penguin gets tired(the penguin gets more tired cause he is swimming) | |
if(self.power<0): | |
print("penguin drowns") | |
self.power=0 | |
class Duck(Bird): ##this means the Ducks inherit the properties of birds | |
def flap_wings(self): ##we override the bird's native function so that we can give it different behaviour | |
print("flappy flap flap ducky") | |
self.y=self.y+5 ## ducks fly up | |
self.move_foward() | |
self.power=self.power-1 ##ducks dont get so tired cause they are flying in the air | |
if(self.y<0):##if the ducks are under the in the water | |
print("duck drowns") | |
self.power=0 | |
Penguins=[] | |
Ducks=[] | |
Penguin_count=10 | |
Duck_count=5 | |
for counter in range(0,Penguin_count): | |
new_penguin=Penguin();##create new instance of penguin (cut a new cookie) | |
new_penguin.sprite = pyglet.sprite.Sprite(penguin_image, x=100, y=100) | |
Penguins.append(new_penguin) | |
Penguins[counter].name="Penguin #" + str(counter) | |
Penguins[counter].x=random.randint(1,screen_width+1) | |
Penguins[counter].y=random.randint(1,screen_height+1) | |
for counter in range(0,Duck_count): | |
new_duck=Duck();##create new instance of duck (cut a new cookie) | |
new_duck.sprite = pyglet.sprite.Sprite(duck_image, x=100, y=100) | |
Ducks.append(new_duck) | |
Ducks[counter].name="Duck #" + str(counter) | |
Ducks[counter].x=random.randint(1,screen_width+1) | |
Ducks[counter].y=random.randint(1,screen_height+1) | |
print("initial coordinates of birds:") | |
for counter in range(0,Penguin_count): | |
Penguins[counter].show_data() | |
for counter in range(0,Duck_count): | |
Ducks[counter].show_data() | |
window = pyglet.window.Window(screen_height,screen_width) | |
@window.event | |
def on_draw(): | |
window.clear() | |
for counter in range(0,Penguin_count): | |
Penguins[counter].sprite.draw() | |
for counter in range(0,Duck_count): | |
Ducks[counter].sprite.draw() | |
def update(dt): | |
for counter in range(0,Penguin_count): | |
Penguins[counter].apply_gravity() | |
Penguins[counter].flap_wings() | |
Penguins[counter].sprite.position=(Penguins[counter].x,Penguins[counter].y) | |
for counter in range(0,Duck_count): | |
Ducks[counter].apply_gravity() | |
Ducks[counter].flap_wings() | |
Ducks[counter].sprite.position=(Ducks[counter].x,Ducks[counter].y) | |
##end game condition, all birds died | |
sum_power=0 | |
for counter in range(0,Penguin_count): | |
sum_power=sum_power+Penguins[counter].power | |
for counter in range(0,Duck_count): | |
sum_power=sum_power+Ducks[counter].power | |
if(sum_power<=0): | |
print("all birds are dead") | |
quit() | |
pyglet.clock.schedule_interval(update, 0.1) | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment