import pgzrun import random WIDTH = 800 HEIGHT = 600 apple = Actor("apple") apple_x = WIDTH / 2 apple_y = HEIGHT / 2 apple.pos = (WIDTH / 2 , HEIGHT / 2) sword = Actor("sword") dinosuars = [] corpses = [] spawn_interval = 2.0 min_spawn_interval = 0.2 spawn_timer = 0 game_time = 0 def spawn_dinosaurs(): dino = Actor("dino") side = random.randint(0,3) if side == 0: #up dino.x = random.randint(2,WIDTH) dino.y = -50 elif side == 1: #down dino.x = random.randint(0,WIDTH) dino.y = HEIGHT + 50 elif side == 2: #left dino.x = -50 dino.y = random.randint(0,HEIGHT) else: dino.x = WIDTH + 50 dino.y = random.randint(0,HEIGHT) dinosuars.append(dino) def update(dt): global spawn_timer , spawn_interval , game_time game_time += dt spawn_timer += dt spawn_interval = max(min_spawn_interval , spawn_interval - 0.005 * dt) if spawn_timer >= spawn_interval: spawn_dinosaurs() spawn_timer = 0 sword.pos = mouse.pos for dino in dinosuars[:]: dx = apple_x - dino.x dy = apple_y - dino.y speed = 80 dino.x += dx * speed * dt dino.y += dy * speed * dt if dino.colliderect(sword): dino.image = "dead" corpses.append({"Actor":dino,"die_time": game_time}) sounds.hit.play() dinosuars.remove(dino) for corpse in corpses[:]: if game_time - corpse["die_time"] >= 2: corpses.remove(corpse) def draw(): screen.fill((100,200,100)) apple.draw() sword.draw() for dino in dinosuars: dino.draw() for corpse in corpses: corpse["Actor"].draw() pgzrun.go()