Krok 5: meteor i życia

Sterowanie postacią klawiaturą i punktacja - Informatyka - klasa 8 (Python)

Gra potrzebuje przeszkody. `Meteor` spada szybciej niż gwiazdy (o 3), a gdy trafi rakietę, zabiera jej życie (`ship.lives`, na początku 3) i wraca na górę. Napis pokazuje teraz punkty i życia. Zaznaczone linie są nowe względem kroku 4. Pracujcie w zespole: jedna osoba pisze kod, reszta czyta polecenie, sprawdza szczegóły i testuje. Po każdym zadaniu zmieńcie role.

import random

class Ship(Sprite):
  def __init__(self):
    self.image = 12
    self.size = 20
    self.position = (0, -80)
    self.points = 0
    self.lives = 3

  def update(self):
    if world.key('right'):
      self.x = self.x + 4
    if world.key('left'):
      self.x = self.x - 4

class Star(Sprite):
  def __init__(self):
    self.image = 4
    self.color = (250, 204, 21)
    self.size = 15
    self.position = (random.randint(-90, 90), 90)

  def update(self):
    self.y = self.y - 2
    if self.touches(ship):
      ship.points = ship.points + 1
      self.y = 90
      self.x = random.randint(-90, 90)
    if self.y < -95:
      self.y = 90
      self.x = random.randint(-90, 90)

class Meteor(Sprite):
  def __init__(self):
    self.image = 1
    self.color = (120, 113, 108)
    self.size = 25
    self.position = (random.randint(-90, 90), 90)

  def update(self):
    self.y = self.y - 3
    if self.touches(ship):
      ship.lives = ship.lives - 1
      self.y = 90
      self.x = random.randint(-90, 90)
    if self.y < -95:
      self.y = 90
      self.x = random.randint(-90, 90)

class Score(Sprite):
  def __init__(self):
    self.text = "Punkty: 0"
    self.size = 10
    self.position = (-55, 90)

  def update(self):
    self.text = f"Punkty: {ship.points}, życia: {ship.lives}"

ship = Ship()
world.add(ship)
stars = []
for i in range(3):
  star = Star()
  star.x = -60 + 60 * i
  world.add(star)
  stars.append(star)
meteor = Meteor()
world.add(meteor)
score = Score()
world.add(score)
world.start()

Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.

Wszystkie lekcje kursu Informatyka - klasa 8·Zaloguj się, aby zapisać postęp na koncie.