import os os.environ['SDL_VIDEO_WINDOW_POS'] = "100,100" os.chdir(os.path.dirname(__file__)) from typing import TYPE_CHECKING if TYPE_CHECKING: from pgzero.builtins import screen, Actor, keyboard, keys import pgzrun import math from pgzhelper import * import random # Screen dimensions (Bigger than Scratch!) WIDTH = 960 HEIGHT = 540 state = { #variables we might use in other functions "score" : 0, "lives" : 100, # How much hp we have "gameover": False } rabbit = Actor("rabbit") rabbit.scale = 0.3 hedgehog = Actor("hedgehog") hedgehog.x = 400 hedgehog.y = 400 bg_original = pygame.image.load("images/grass.jpg") #Gotten from freepik background = pygame.transform.scale(bg_original, (WIDTH, HEIGHT)) # This is called once every frame def draw(): if(state["gameover"]==True): screen.clear() screen.draw.text("You are Dead! (Mr Chuang doesn't want to collect assets for sprites...) \n ", center=(480, 270),fontsize=32,color=(255,255,255)) else: screen.clear() screen.blit(background, (0, 0)) rabbit.draw() hedgehog.draw() # Draw the lives screen.draw.text("HP: " + str(state["lives"]), center=(840, 30),fontsize=32,color=(0,0,0)) # 0, 0 is the top left # This is called every game tick. Think of it as a forever loop def update(): if keyboard[keys.UP] == True: rabbit.y = rabbit.y - 10 if keyboard[keys.DOWN] == True: rabbit.y = rabbit.y + 10 if keyboard[keys.LEFT] == True: rabbit.x = rabbit.x - 10 if keyboard[keys.RIGHT] == True: rabbit.x = rabbit.x + 10 if rabbit.collide_pixel(hedgehog): state["lives"] -= 0.5 if state["lives"] < 0: state["gameover"]=True def on_key_down(): # Detect key presses. Useful to restart, not used currently pass pgzrun.go()