Skip to content

Commit

Permalink
Reorganize code and check for keypresses
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-borges committed Jan 27, 2024
1 parent 8f3c359 commit 9ff60c4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
Binary file added assets/diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 60 additions & 32 deletions main-window.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
print("Warning, sound disabled")

main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, "data")
data_dir = os.path.join(main_dir, "assets")

def load_image(name, colorkey=None, scale=1):
fullname = os.path.join(data_dir, name)
Expand All @@ -25,11 +25,11 @@ def load_image(name, colorkey=None, scale=1):
return image, image.get_rect()

class CountingObjects(pg.sprite.Sprite):
"""moves the objects to be counted around the screen"""
"""adds an object at a given position and with a given movement"""

def __init__(self):
pg.sprite.Sprite.__init__(self) # call Sprite initializer
self.image, self.rect = load_image("diamond.png", -1, 4)
self.image, self.rect = load_image("diamond.jpg", -1, 0.4)
screen = pg.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 10, 90
Expand All @@ -45,37 +45,65 @@ def _walk(self):
self.image = pg.transform.flip(self.image, True, False)
self.rect = newpos

# pygame setup
pg.init()
screen = pg.display.set_mode((1000, 1000), pg.SCALED)
pg.display.set_caption("Counting Simulator")
clock = pg.time.Clock()
running = True

background = pg.Surface(screen.get_size())
background = background.convert()
background.fill((102, 204, 10))

if pg.font:
font = pg.font.Font(None, 64)
text = font.render("How many sheep are there?", True, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width() / 2, y=background.get_height() - 100)
background.blit(text, textpos)

while running:
# poll for events
# pg.QUIT event means the user clicked X to close your window
for event in pg.event.get():
if event.type == pg.QUIT:
running = False

def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
# Initialize Everything
pg.init()
screen = pg.display.set_mode((1000, 1000), pg.SCALED)
pg.display.set_caption("Counting Simulator")

# Create The Background
background = pg.Surface(screen.get_size())
background = background.convert()
background.fill((102, 204, 10))

# Put Text On The Background, Centered
if pg.font:
font = pg.font.Font(None, 64)
text = font.render("How many objects are there?", True, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width() / 2, y=background.get_height() - 100)
background.blit(text, textpos)

# Display The Background
screen.blit(background, (0, 0))
pg.display.flip()

# RENDER YOUR GAME HERE
# Prepare Game Objects
diamond = CountingObjects()
allobjects = pg.sprite.RenderPlain((diamond))
allobjects.draw(screen)
clock = pg.time.Clock()

# flip() the display to put your work on screen
pg.display.flip()
# Main Loop
going = True
while going:
clock.tick(60)

# Handle Input Events
for event in pg.event.get():
if event.type == pg.QUIT:
going = False
elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
# Close window if user presses ESC
going = False
elif event.type == pg.KEYDOWN:
print("A key! A key, I say!")

allobjects.update()

# Draw Everything
screen.blit(background, (0, 0))
allobjects.draw(screen)
pg.display.flip()

pg.quit()


# Game Over

clock.tick(60) # limits FPS to 60

pg.quit()
# this calls the 'main' function when this script is executed
if __name__ == "__main__":
main()

0 comments on commit 9ff60c4

Please sign in to comment.