Krok 3: duch
Duchy w zamku - Gry w Pythonie - przykłady (Python)
Przeciwnik, który myśli: duch co tick obraca się w stronę czarodzieja (`point_towards(wizard)`) i robi krok o 1 (`move(1)`). Startuje w losowym rogu wybranym przez `random.choice` z listy czterech punktów. Na razie dotknięcie nic nie robi.
import random
class Game(Sprite):
def __init__(self):
self.size = 12
self.color = (99, 102, 241)
self.position = (0, 88)
self.keys = 0
def update(self):
self.text = f"Klucze: {self.keys}"
class Wizard(Sprite):
def __init__(self):
self.image = 0
self.color = (124, 58, 237)
self.size = 22
self.rotate = False
def update(self):
if world.key('right'):
self.x = self.x + 3
if world.key('left'):
self.x = self.x - 3
if world.key('up'):
self.y = self.y + 3
if world.key('down'):
self.y = self.y - 3
class Key(Sprite):
def __init__(self):
self.image = 42
self.color = (202, 138, 4)
self.size = 16
self.jump()
def jump(self):
self.position = (random.randint(-90, 90), random.randint(-90, 70))
def update(self):
if self.touches(wizard):
game.keys = game.keys + 1
self.jump()
class Ghost(Sprite):
def __init__(self):
self.image = 17
self.color = (100, 116, 139)
self.size = 22
self.rotate = False
self.corner()
def corner(self):
self.position = random.choice([(-90, -90), (90, -90), (-90, 70), (90, 70)])
def update(self):
self.point_towards(wizard)
self.move(1)
game = Game()
wizard = Wizard()
key = Key()
ghost = Ghost()
world.add(game)
world.add(key)
world.add(wizard)
world.add(ghost)
world.start()Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.
Wszystkie lekcje kursu Gry w Pythonie - przykłady·Zaloguj się, aby zapisać postęp na koncie.