Programmieren – die besten Beiträge

Einfaches JavaScript-Spiel per Tastendruck neu starten?

Wir hatten in der Schule den Auftrag ein einfaches JavaScript-Spiel zu programmieren. Ich will das Programm nun durch einen Druck auf die Taste "R" neu starten. Hier ist der Code:

let start,
ziel,
ballradius,
rgbFarbwerte,
x,
y;
function setup() {
  createCanvas(400, 400);
  background('black');
  stroke("white");
  start = circle(60, 390, 10);
  ziel = circle(40, 390, 10);
  ballradius = 2;
  x = 60;
  y = 390;
}
function draw() {
  //lösche bild
  background("black");
  //zeichne Spielfeld
  fill('white');
  stroke("white");
  rect(20, 300, 80, 100);
  rect(50, 300, 100, 60);
  rect(140, 260, 50, 100);
  rect(140, 240, 120, 40);
  rect(230, 170, 30, 100);
  rect(160, 160, 100, 30);
  rect(160, 40, 20, 120);
  rect(160, 40, 100, 10);
  rect(260, 1, 5, 49);
  //zeichne Start-,Endpunkt
  fill("blue");
  stroke("blue");
  circle(60, 390, 10);
  fill("red");
  stroke("red");
  circle(262.25, 10, 10);
  rgbFarbwerte = get(x, y);
  if (rgbFarbwerte[0] === 0 && rgbFarbwerte[1] === 0 && rgbFarbwerte[2] === 0) {
    background("red");
    console.clear();
    console.log("Game over");
  }
  if (rgbFarbwerte[0] === 255 && rgbFarbwerte[2] === 0) {
    background("green");
    console.clear();
    console.log("You win");
  }
  //zeichne Kreis
  stroke("lightgreen");
  fill("lightgreen");
  circle(x, y, 4);
  //steuere Kreis
  if (keyIsDown(68)) { //taste d -> nach rechts
    x = x + 2;
  }
  if (keyIsDown(65)) { //taste a -> nach links
    x = x - 2;
  }
  if (keyIsDown(87)) { // taste w -> nach oben
    y = y - 2;
  }
  if (keyIsDown(83)) { // taste s -> nach unten
    y = y + 2;
  }
}
Computer, Schule, Technik, programmieren, JavaScript, Informatik

Wie kann ich mehrere Münzen in PyGame einsammeln?

Hallo,

ich habe eine Frage zu meinem Programm.

Nachdem ich die dritte Münze in meinem Spiel eingesammelt habe, wird komischerweise die zweite Münze anstatt der Dritten gespawnt. Wie kann ich das ändern? Bzw. hat jemand eine Idee, wie sich das beheben lässt?

Hier ist mein Code:

from random import randint

WIDTH = 600
HEIGHT = 600
score = 0
game_over = False

mario = Actor("mario.png")
mario.pos = 100, 100

coin = Actor("money.png")
coin.pos = 0, 0

def draw():
  screen.fill("green")
  mario.draw()
  coin.draw()
  screen.draw.text("Punkte: " + str(score), color="black", topleft=(10, 10))

  if game_over:
    screen.fill("pink")
    screen.draw.text("Endstand:" + str(score), topleft=(10, 10), fontsize = 60)

def place_coin():
  coin.x = 100
  coin.y = 200

def place_coin2():
  coin.x = 100
  coin.y = 300

def place_coin3():
  coin.x = 100
  coin.y = 400

def place_coin4():
  coin.x = 100
  coin.y = 500

def place_coin5():
  coin.x = 300
  coin.y = 100

def place_coin6():
  coin.x = 300
  coin.y = 300

def place_coin7():
  coin.x = 500
  coin.y = 200

def place_coin8():
  coin.x = 500
  coin.y = 300

def place_coin9():
  coin.x = 500
  coin.y = 400

def place_coin10():
  coin.x = 500
  coin.y = 500

def place_coin11():
  coin.x = 700
  coin.y = 100

def place_coin12():
  coin.x = 700
  coin.y = 300

def time_up():
  global game_over
  game_over = True

def update():
  global score

  if keyboard.left:
    mario.x = mario.x - 4
  elif keyboard.right:
    mario.x = mario.x + 4
  elif keyboard.up:
    mario.y = mario.y - 4
  elif keyboard.down:
    mario.y = mario.y + 4

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin2()

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin10()

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin4()

  coin_collectet = mario.colliderect(coin)

  if coin_collectet:
    score = score + 10
    place_coin8()

clock.schedule(time_up, 20.0)
place_coin()
programmieren, Pygame

Meistgelesene Beiträge zum Thema Programmieren