Skip to content

Commit

Permalink
Sheep running!!
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeGSimoes committed Jan 27, 2024
1 parent c1001f0 commit 82f70f1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
83 changes: 77 additions & 6 deletions counting_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,85 @@
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, "assets")

class CountingObjects(pg.sprite.Sprite):

class SpriteSheet:
def __init__(self, filename):
"""Load the sheet."""
try:
self.sheet = pg.image.load(filename).convert()
except pg.error as e:
print(f"Unable to load spritesheet image: {filename}")
raise SystemExit(e)

def image_at(self, rectangle, colorkey=None):
"""Load a specific image from a specific rectangle."""
# Loads image from x, y, x+offset, y+offset.
rect = pg.Rect(rectangle)
image = pg.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pg.RLEACCEL)
return image

def images_at(self, rects, colorkey=None):
"""Load a whole bunch of images and return them as a list."""
return [self.image_at(rect, colorkey) for rect in rects]

def load_strip(self, rect, image_count, colorkey=None):
"""Load a whole strip of images, and return them as a list."""
tups = [
(rect[0] + rect[2] * x, rect[1], rect[2], rect[3])
for x in range(image_count)
]
return self.images_at(tups, colorkey)

def load_2dstrip(self, rect, image_count, colorkey=None):
"""Load a whole strip of images, and return them as a list."""
tupsUp = [
(rect[0] + rect[2] * x, rect[1], rect[2], rect[3])
for x in range(image_count)
]
tupsLeft = [
(rect[0] + rect[2] * x, rect[1] + rect[3] * 1, rect[2], rect[3])
for x in range(image_count)
]
tupsDown = [
(rect[0] + rect[2] * x, rect[1] + rect[3] * 2, rect[2], rect[3])
for x in range(image_count)
]
tupsRight = [
(rect[0] + rect[2] * x, rect[1] + rect[3] * 3, rect[2], rect[3])
for x in range(image_count)
]

print (tupsLeft)
return {
"up": self.images_at(tupsUp, colorkey),
"left": self.images_at(tupsLeft, colorkey),
"down": self.images_at(tupsDown, colorkey),
"right": self.images_at(tupsRight, colorkey),
}


class CountingObject(pg.sprite.Sprite):
"""adds an object at a given position and with a given movement"""

currentSpriteIndex = 0
ramSprites = None

def __init__(self):
pg.sprite.Sprite.__init__(self) # call Sprite initializer
self.image, self.rect = load_image("diamond.jpg", -1, 0.4)
ramSS = SpriteSheet('assets/ram_walk.png')
ramSprites = ramSS.load_2dstrip((0, 0, 128, 128), 4, colorkey=(0, 0, 0))
self.ramSprites = ramSprites
self.image = ramSprites["right"][0]
self.rect = ramSprites["right"][0].get_rect()
screen = pg.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 10, 90
self.move = 18
#self.move = 18

def _walk(self):
"""move the object across the screen, and turn at the ends"""
Expand All @@ -24,10 +93,12 @@ def _walk(self):
newpos = self.rect.move((self.move, 0))
self.image = pg.transform.flip(self.image, True, False)
self.rect = newpos

def update(self):
self.rect.x += 10
if self.rect.right > 300:
self.rect.x += 1
self.currentSpriteIndex += 1
self.image = self.ramSprites["right"][int(self.currentSpriteIndex/4)%4]
if self.rect.right > 500:
self.kill()

def load_image(name, colorkey=None, scale=1):
Expand Down
4 changes: 2 additions & 2 deletions main-window.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def main():
pg.display.flip()

# Prepare Game Objects
diamond = co.CountingObjects()
allobjects = pg.sprite.Group(diamond)
sheep = co.CountingObject()
allobjects = pg.sprite.Group(sheep)
allobjects.draw(screen)
clock = pg.time.Clock()

Expand Down
37 changes: 0 additions & 37 deletions spritesheet.py

This file was deleted.

0 comments on commit 82f70f1

Please sign in to comment.