Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

in der Schule sollen wir ein einfaches Moorhuhnspiel mit BlueJ erstellen. Ich bin jetzt soweit, dass ich ein Spielfeld und ein Fadenkreuz habe, dass ich mit 4 Tasten bewegen kann. Die 5. Taste (Schießen) ist noch ohne Bedeutung. Ich habe auch schon einen Kreis (Huhn), dass sich aber noch ruckartig bewegt. Wie kann ich jetzt hinzufügen, dass sich der Kreis fließend (zufällig) auf dem Spielfeld (Rechteck) bewegt?

Hier der Quelltext:

Das Spiel:

//Moorhuhnjagd:

import java.util.Random;


public class Spiel  implements ITuWas

{

   Rechteck r;

   Fadenkreuz f;  

   Taste t1;

   Taste t2;

   Taste t3;

   Taste t4;

   Taste t5; 

   Random z;

   int xPos;

   int yPos;

   Kreis k;



    //Konstruktor:

    public Spiel()

    {

        r = new Rechteck(0,0,400,300);

        r.setzeFarbe("gruen");

        f = new Fadenkreuz(10,10);

        t1 = new Taste ("^",250,350,60,50);

        t1.setzeLink(this,0);

        t1.setzeSchriftgroesse(25);

        t2 = new Taste ("<",190,400,60,50);

        t2.setzeLink(this,1);

        t3 = new Taste ("V",250,450,60,50);

        t3.setzeLink(this,2);

        t4 = new Taste (">",310,400,60,50);

        t4.setzeLink(this,3);

        t5 = new Taste ("Schuss",70,400,100,50);

        t5.setzeLink(this,4);

        k = new Kreis(50,50,20);

        k.setzeFarbe("rot");

        Moorhuhn();

    }


    //Methoden:

    //Tu was:


    public void tuWas(int ID)

    {

        switch (ID) 

        {

            case 0:

            f.nachOben();

            break;

            case 1:

            f.nachLinks();

            break;

            case 2:

            f.nachUnten();

            break;

            case 3:

            f.nachRechts();

            break;

        } 

   }


   //Moorhuhn:

   public void Moorhuhn()

   {

       while (true) {

       z = new Random();

       xPos = z.nextInt(359)+1;

       k.setzePosition(xPos,yPos);

       z = new Random();

       yPos = z.nextInt(259)+1;

       k.setzePosition(xPos,yPos);

       StaticTools.warte(5000);}

   }

}
Das Fadenkreuz:
public class Fadenkreuz

{

    int xPosB, yPosB, xB, d, radius;   

    Behaelter b;

    Kreis k;

    Linie ver;

    Linie hor;

    int step;


    public Fadenkreuz(int xPosB, int yPosB)

    {

       this.xPosB = xPosB;

       this.yPosB = yPosB;

       xB = 50;

       d = 5;

       radius = 20;

       b = new Behaelter(xPosB,yPosB,xB,xB);

       k = new Kreis(b,d,d,radius);

       k.setzeFarbe("schwarz");

       k.rand();

       hor = new Linie(b,0,radius+d, xB,radius+d);

       hor.setzeFarbe("schwarz");

       hor.setzeLinienDicke(2);

       ver = new Linie(b,radius+d,0,radius+d, xB);

       ver.setzeFarbe("schwarz");

       ver.setzeLinienDicke(2);

       step = 20;

    }


    public void nachLinks()

    {

        b.setzePosition(xPosB-step, yPosB);

        xPosB = xPosB-step;

    }

    public void nachOben()

    {

        b.setzePosition(xPosB, yPosB-step);

        yPosB = yPosB-step;

    }

    public void nachUnten()

    {

        b.setzePosition(xPosB, yPosB+step);

        yPosB = yPosB+step;

    }   

    public void nachRechts()

    {

        b.setzePosition(xPosB+step, yPosB);

        xPosB = xPosB+step;

    }

}

Viele Grüße,

Manuel

Wie kann ich jetzt hinzufügen, dass sich der Kreis fließend (zufällig) auf dem Spielfeld (Rechteck) bewegt?

Indem Du die Positionen entsprechend berechnest und zeichnest. Ich würde aber bei so einem Projekt von BlueJ abraten, da man hier mit mehreren Threads arbeiten sollte und das ganze unter BlueJ schwierig wird umzusetzen.

Für die graphische darstellung würde ich zu https://java3d.dev.java.net/ raten, wobei man die 3D Fähigkeit nicht nutzen muss, aber eben die graphische Modellierung sinnvoll verwenden kann

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.