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" : 0, # not used "gameover":False } fox = Actor("fox") fox.scale = 0.3 fox.x = 960 fox.y = 540 rabbit = Actor("rabbit") rabbit.scale = 0.3 carrot = Actor("carrot") carrot.scale = 0.1 gold_fox = Actor("gold_fox") gold_fox_scale = 0 # make it invisible 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 # draw the background, fox, rabbit and carrot def draw(): if(state['gameover'] == True): screen.clear() screen.draw.text("Ya Lost! \n Final Score: "+str(state["score"])+"\nPress Space to Restart", center=(480, 270),fontsize=32,color=(255,255,255)) else: screen.clear() screen.blit(background, (0, 0)) screen.draw.text("Score: " + str(state["score"]), center=(840, 30),fontsize=32,color=(0,0,0)) # 0, 0 is the top left fox.draw() rabbit.draw() carrot.draw() def on_mouse_move(pos): # 'pos' is a tuple (x, y) provided by Pygame Zero rabbit.pos = pos # Make the fox point towards the rabbit # Move it 5 steps # ------- The above 2 lines uses th pgzhelper library for math ------ # Check collision for carrot and fox - Note also uses pgzhelper for pixel collision def update(): angle_to_target = fox.angle_to(rabbit) fox.angle = angle_to_target fox.point_towards(rabbit) fox.move_forward(5) if(rabbit.collide_pixel(carrot)): # if rabbit touches carrot state["score"] = state["score"]+1 carrot.x = random.randint(0, WIDTH) carrot.y = random.randint(0, HEIGHT) if(rabbit.collide_pixel(fox)): # they lose state['gameover'] = True def on_key_down(): # Reset the Game if keyboard[keys.SPACE] == True: #print("test") state['gameover'] = False state['score'] = 0 fox.x = 960 fox.y = 540 pgzrun.go()