Skip to content

Instantly share code, notes, and snippets.

@benjaminnow
Last active Jun 24, 2018
Embed
What would you like to do?
wombat_game
class Leaf:
def __init__(self, x, y, num):
self.image_obj = loadImage("leaf.png")
self.x = x * 50
self.y = y * 50
self.num = num
def display(self):
image(self.image_obj, self.x, self.y, 50, 50)
if self.num > 1:
textSize(18)
fill(255)
text(self.num, self.x+20, self.y+25)
class Player:
def __init__(self, wombat):
self.wombat = wombat
global bob
bob = self.wombat
def main(self):
bob.walk()
bob.walk()
bob.walk()
bob.walk()
class Rock:
def __init__(self, x, y):
self.image_obj = loadImage("rock.png")
self.x = x * 50
self.y = y * 50
def display(self):
image(self.image_obj, self.x, self.y, 50, 50)
from leaf import Leaf
from rock import Rock
from time import sleep
class Wombat:
def __init__(self, x, y, tile_objects, wombat_world):
self.image_obj = loadImage("wombat1.png")
self.dir = 1
self.leaves = 0
self.broken = False
self.x = x * 50
self.y = y * 50
self.tile_objects = tile_objects
self.world = wombat_world
def display_world(self):
redraw()
print("called redraw")
sleep(0.5)
# self.world.display()
# print("displayed world")
# sleep(0.5)
def get_objects_at_offset(self, x, y):
for i, obj in ennumerate(self.tile_objects):
if obj.x == x and obj.y == y:
return i
def facing_north(self):
if self.broken:
return
if self.dir == 0:
return True
else:
return False
def has_leaf(self):
if self.broken:
return
if self.leaves > 0:
return True
else:
return False
def found_leaf_index(self):
if self.broken:
return
for i, obj in enumerate(self.tile_objects):
if obj.x == self.x and obj.y == self.y and isinstance(obj, Leaf):
return i
return -1
def found_leaf(self):
if self.broken:
return
for obj in self.tile_objects:
if obj.x == self.x and obj.y == self.y and isinstance(obj, Leaf):
return True
return False
def can_move(self):
if self.broken:
return
for obj in self.tile_objects:
if self.dir == 0:
if self.x == obj.x and self.y-50 == obj.y and isinstance(obj, Rock):
return False
elif self.dir == 1:
if self.x+50 == obj.x and self.y == obj.y and isinstance(obj, Rock):
return False
elif self.dir == 2:
if self.x == obj.x and self.y+50 == obj.y and isinstance(obj, Rock):
return False
elif self.dir == 3:
if self.x-50 == obj.x and self.y == obj.y and isinstance(obj, Rock):
return False
if self.x / 50 == 15 and self.dir == 1:
return False
elif self.x / 50 == 0 and self.dir == 3:
return False
elif self.y / 50 == 11 and self.dir == 2:
return False
elif self.y / 50 == 0 and self.dir == 0:
return False
else:
return True
def pick_leaf(self):
if self.broken:
return
leaf_index = -1
leaf_index = self.found_leaf_index()
if not leaf_index == -1:
self.tile_objects[leaf_index].num -= 1
else:
self.broken = True
self.change_to_broken_image()
print("broken, tried to pick non existant leaf")
self.display_world()
def place_leaf(self):
if self.broken:
return
in_tile_list = False
if self.has_leaf():
for obj in self.tile_objects:
if obj.x == self.x and obj.y == self.y:
obj.num += 1
in_tile_list = True
if not in_tile_list:
self.tile_objects.append(Leaf(self.x/50, self.y/50, 1))
else:
self.broken = True
self.change_to_broken_image()
print("broke, tried to place leaf even though didn't have any")
def add_leaves(self, num):
if self.broken:
return
self.leaves = num
def change_to_broken_image(self):
self.image_obj = loadImage("ball.png")
def walk(self):
if self.broken:
return
if not self.can_move():
self.broken == True
print("broken, hit edge or rock")
self.change_to_broken_image()
else:
if self.dir == 0:
self.y -= 50
elif self.dir == 1:
self.x += 50
elif self.dir == 2:
self.y += 50
elif self.dir == 3:
self.x -= 50
print("x: {} y: {}").format(self.x, self.y)
self.display_world()
def turn_left(self):
if self.dir == 0:
self.dir = 3
else:
self.dir -= 1
self.turned_wombat()
self.display_world()
def turned_wombat(self):
if self.dir == 0:
self.image_obj = loadImage("wombat0.png")
elif self.dir == 1:
self.image_obj = loadImage("wombat1.png")
elif self.dir == 2:
self.image_obj = loadImage("wombat2.png")
elif self.dir == 3:
self.image_obj = loadImage("wombat3.png")
def display(self):
print("drew wombat")
image(self.image_obj, self.x, self.y, 50, 50)
from world import World
from wombat import Wombat
from time import sleep
def setup():
size(800, 600)
global wombat, world
world = World(0, 0, 1) #adds wombat at 1,2 and setup plan 1
world.display()
noLoop()
def draw():
#world.play()
world.display()
print("here")
from leaf import Leaf
from rock import Rock
from wombat import Wombat
from player import Player
from random import randint
from time import sleep
class World:
def __init__(self, wombat_x, wombat_y, setup_num):
self.tile_objects = []
self.wombat = Wombat(wombat_x, wombat_y, self.tile_objects, self)
if setup_num == 1:
self.setup_1()
def draw_grid(self):
box_size = 50
colnum = 800 / box_size
rownum = 600 / box_size
for i in range(0, colnum):
for j in range(0, rownum):
x = i * box_size
y = j * box_size
fill(255, 221, 153)
stroke(0)
rect(x, y, box_size, box_size)
print("drew grid")
def something_on_spot(self, x, y):
for obj in self.tile_objects:
if obj.x == x*50 and obj.y == y*50:
return True
return False
def random_leaves(self):
for i in range(0, 25):
chosen_x_y = [randint(0, 15), randint(0, 11)]
while (chosen_x_y[0]*50 == self.wombat.x and chosen_x_y[1]*50 == self.wombat.y) or self.something_on_spot(chosen_x_y[0], chosen_x_y[1]):
chosen_x_y = [randint(0, 15), randint(0, 11)]
leaf = Leaf(chosen_x_y[0], chosen_x_y[1], randint(0, 10))
self.tile_objects.append(leaf)
def random_rocks(self):
for i in range(0, 25):
chosen_x_y = [randint(0, 15), randint(0, 11)]
while (chosen_x_y[0]*50 == self.wombat.x and chosen_x_y[1]*50 == self.wombat.y) or self.something_on_spot(chosen_x_y[0], chosen_x_y[1]):
chosen_x_y = [randint(0, 15), randint(0, 11)]
rock = Rock(chosen_x_y[0], chosen_x_y[1])
self.tile_objects.append(rock)
#this will just be the students collected as many leaves as possible
def setup_1(self):
self.random_leaves()
def draw_tile_objects(self):
for obj in self.tile_objects:
obj.display()
print("drew objs")
def play(self):
player = Player(self.wombat)
player.main() #runs student code they did in the player class
def display(self):
self.draw_grid() #draws lines
self.draw_tile_objects() #draws objects on tiles(leaves, rocks)
self.wombat.display() #calls wombat display class which creates a wombat image at a certain location
print("called world.display")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment