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.

kleines spiel als java app... geht irgendwie nicht?

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

ich programmier grad das Spiel Breakout (Arkanoid) und bleib bei einer Fehlermeldung stecken. Kann mir jemand helfen? Wäre total nett. Hier sind die Codes:

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class Notarkanoid{

/**

* @param args

*/

private Ball ball;

public Notarkanoid() {

ball = new Ball(this);

}

public static void main(String[] args){

Notarkanoid notarkanoid = new Notarkanoid();

}

}

///////////////////////////UND:

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class Ball extends JPanel implements Runnable

{

private Thread t;

private int d;

private int x;

private int y;

private int dx;

private int dy;

private int size = 10;

private Block[] block= new Block;

private Bat bat;

public Ball()

{

super();

bat = new Bat();

this.x = bat.x+bat.w+10; // control the start point of x

this.y = bat.y+(bat.l/2); // control the start point of y

this.d = 20; // control the size of the ball

this.dx = 5; // control the speed of x

this.dy = 5; // control the speed of y

for (int i = 0; i < size; i++) {

block = new Block(400, 50+i*50, 49);

}

this.setOpaque(false);

this.setDoubleBuffered(true);

t = new Thread(this);

t.start();

}

protected void paintComponent(Graphics g)

{

g.setColor(Color.green);

g.fillRect(bat.x, bat.y, bat.w, bat.l);

for (int i = 0; i < size; i++) {

if (!block.i****()){

g.setColor(Color.green);

g.fillRect(block.x, block.y, block.w, block.w);

}

}

g.setColor(Color.red);

g.fillOval(this.x, this.y, this.d, this.d);

}

public void animate()

{

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent event){

bat=new Bat();

}

});

addMouseMotionListener(new MouseMotionAdapter(){

public void mouseMoved(MouseEvent event){

bat.y=event.getY();

}

});

Rectangle bounds = this.getBounds();

for (int i = 0; i < size; i++) {

if (!block.i****()){

if ((this.x > block.x)&&(this.x+this.d<block.x+block.w)){

if ((this.y + this.d+dy > block.y)&&(this.y +dy <block.y+block.w)){

dy = -dy;

block.setI****(true);

}

}

if((this.y > block.y)&&(this.y +this.d <block.y+block.w)){

if ((this.x +this.d+dx > block.x)&&(this.x+dx <block.x+block.w)) {

dx = -dx;

block.setI****(true);

}

}

}

}

if(((this.y > bat.y)&&(this.y+this.d<bat.y+bat.l)) &&(this.x<bat.x+bat.w))dx= -dx;

if((this.x + this.d + dx > bounds.width))

dx = -dx;

if((this.y + dy < 0) || (this.y + this.d + dy > bounds.height))

dy = -dy;

if((this.x<10)) return;

this.x += dx;

this.y += dy;

this.repaint();

}

public void run()

{

while(true)

{

this.animate();

try

{

Thread.sleep(10);

}

catch (InterruptedException e) { }

}

}

}

class Block {

int w;

int x;

int y;

boolean hit;

public boolean i****(){

return hit;

}

public void setI****(boolean hit){

this.hit=hit;

}

public Block(int x, int y, int w) {

hit = false;

this.x = x;

this.y = y;

this.w = w;

}

}

class Bat {

int x;

int y;

int w;

int l;

public Bat() {

x = 10;

y = 200;

w = 20;

l = 100;

}

}

Danke schonmal! :)

Die Fehlermeldung wäre mal interessant - dann bekommt man u.U. auch raus was das Problem ist, ohne dass man deinen Code compilieren muss. Da in der Fehlermeldung wahrscheinlich auch die Zeile der Fehlerquelle angegeben ist solltest du die auch gleich weiterreichen. Hast du dir mal den Exception-Stack angeschaut ?

sorry.

das ist die fehlermeldung:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

The constructor Ball(Notarkanoid) is undefined

at Notarkanoid.<init>(Notarkanoid.java:12)

at Notarkanoid.main(Notarkanoid.java:16)

Hmm die Fehlermeldung sagt doch schon alles ;)

Du rufst in Zeile 12 einen Konstruktor für die Klasse Ball mit einem Parameter der Klasse Notarkanoid auf. Allerdings existiert so ein Konstruktor nicht. In der Klasse Ball ist nur ein Standardkonstruktor ohne Parameter definiert. Du könntest zum Beispiel den Konstruktor anpassen.

public Ball(Notarkanoid notarkanoid)

{

super();

this.notarkanoid = notarkanoid;

[...]

}

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

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.