Das ist mein PinballSpiel.
Vor 10min hat es noch funktionier das der Ball das Schild erkennt und zurückfliegt.
Aber nun habe ich ein bisschen am Quellcode herumgebastelt um mehr Funktion zu haben und schon
funktioniert die Erkennung der Schilder nicht.
Was habe ich falsch gemacht?;(
Vor 10min hat es noch funktionier das der Ball das Schild erkennt und zurückfliegt.
Aber nun habe ich ein bisschen am Quellcode herumgebastelt um mehr Funktion zu haben und schon
funktioniert die Erkennung der Schilder nicht.
Was habe ich falsch gemacht?;(
Java:
package Pinball;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class PinballGUI extends javax.swing.JFrame implements ActionListener,KeyListener{
public PinballGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btStart = new javax.swing.JButton();
tfAusgabe = new javax.swing.JTextField();
paZeichne = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
btStart.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
btStart.setText("Start");
btStart.setToolTipText("");
btStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btStartActionPerformed(evt);
}
});
getContentPane().add(btStart, java.awt.BorderLayout.PAGE_START);
tfAusgabe.setEditable(false);
tfAusgabe.setFont(new java.awt.Font("Tahoma", 1, 18));
tfAusgabe.setHorizontalAlignment(javax.swing.JTextField.CENTER);
tfAusgabe.setText("0 : 0");
tfAusgabe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfAusgabeActionPerformed(evt);
}
});
getContentPane().add(tfAusgabe, java.awt.BorderLayout.PAGE_END);
javax.swing.GroupLayout paZeichneLayout = new javax.swing.GroupLayout(paZeichne);
paZeichne.setLayout(paZeichneLayout);
paZeichneLayout.setHorizontalGroup(
paZeichneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 411, Short.MAX_VALUE)
);
paZeichneLayout.setVerticalGroup(
paZeichneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 254, Short.MAX_VALUE)
);
getContentPane().add(paZeichne, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private BufferStrategy strategy;
private boolean[] downKeys = new boolean[4];
final static int UP = 0, DOWN = 1, W = 2, S = 3;
//Kugel Positionen
int x = 40,y =100;
int dx =2,dy=2;
//Breite und Hoehe des Panels
int breite,hoehe;
//Punkte
int tSpieler =0,tGegner=0;
//Tastatureingabe
int tast;
//Backgroundcolor
Color bg;
//Schild1 Positionen
int posX=10,posY=10;
//Schild2 Positionen
int posY2=10;
//Beenden des Spiels
boolean stop=false;
private void btStartActionPerformed(java.awt.event.ActionEvent evt) {
//Beenden des Spieles durch weiters Klicken auf den Knopf
btStart.setText("Beenden");
bg = paZeichne.getBackground();
hoehe = paZeichne.getHeight();
breite = paZeichne.getWidth();
if(stop == true)
{
System.exit(0);
}
Timer timer = new Timer(10,this);
timer.start();
stop = true;
}
private void tfAusgabeActionPerformed(java.awt.event.ActionEvent evt) {
}
public void actionPerformed(ActionEvent e)
{
//Graphics2D einfügen
//
Graphics2D g2 = (Graphics2D) paZeichne.getGraphics();
//Tore zeichnen
//
BasicStroke stroke = new BasicStroke(9.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
g2.setColor(Color.BLUE);
g2.setStroke(stroke);
g2.drawLine(0,0,0,hoehe);
g2.drawLine(breite,0,breite,hoehe);
//Überzeichnen der Kugel
//
g2.setColor(bg);
g2.fillOval(x,y,10,10);
x += dx;
y += dy;
//Wenn Ball auf Wand aufprallt
//
if(y <= 0)
{
dy = -dy;
}
if(y >= hoehe)
{
dy = -dy;
}
if(x <= 0)
{
dx = -dx;
tGegner += 1;
tfAusgabe.setText(tSpieler + " : " + tGegner);
//Startpunkt des Balls
x= 40;
y=hoehe/2;
}
if(x >= breite)
{
dx = -dx;
tSpieler += 1;
tfAusgabe.setText(tSpieler + " : " + tGegner);
//Startpunkt des Balls
x= breite-40;
y= hoehe/2;
}
//Zeichnen der Kugel
//
g2.setColor(Color.red);
g2.fillOval(x,y,10,10);
//Hinzufügen der Tastaturerkennung
//
this.addKeyListener(this);
this.requestFocus();
//Überzeichnen des Schild1
//
g2.setColor(bg);
g2.fillRect(posX,posY,10,50);
//Überzeichnen des Schild2
//
g2.setColor(bg);
g2.fillRect(breite-20,posY2,10,50);
//Tastendruckerkennung Schild1 und Schild2
//
if (downKeys[W]) {
posY -= 2;
}
if (downKeys[S]) {
posY += 2;
}
if (downKeys[UP]) {
posY2 -= 2;
}
if (downKeys[DOWN]) {
posY2 += 2;
}
//Wenn Schild1 Wand berührt soll er stehen bleiben
//
//oben
if(posY < 0)
{
posY = 0;
}
//unten
if(posY+50 >= hoehe)
{
posY = hoehe-50;
}
//Wenn Schild2 Wand berührt soll er stehen bleiben
//
//oben
if(posY2 < 0)
{
posY2 = 0;
}
//unten
if(posY2+50 >= hoehe)
{
posY2 = hoehe-50;
}
//Zeichnen des Schild1
//
g2.setColor(Color.ORANGE);
g2.fillRect(posX,posY,10,50);
//Zeichnen des Schild2
//
g2.setColor(Color.ORANGE);
g2.fillRect(breite-20,posY2,10,50);
//Überprüfen ob Schild2 Ball berührt
//
if(y >= posY2 && y <= posY2+50 && x == breite-30)
{
dx = -dx;
}
//Überprüfen ob Schild1 Ball berührt
//
if(y >= posY && y <= posY+50 && x == posX+10)
{
dx = -dx;
}
//Überprüfen wer gewonnen hat
if(tSpieler == 10)
{
JOptionPane.showMessageDialog(null,"Spieler 1 hat gewonnen!");
System.exit(0);
}
if(tGegner == 10)
{
JOptionPane.showMessageDialog(null,"Spieler 2 hat gewonnen!");
System.exit(0);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PinballGUI().setVisible(true);
}
});
JOptionPane.showMessageDialog(null,"Pinball Version 2 made by Daniel Schweiger\n\nINFO:\nPlayer1: W und S\nPlayer2: Rauf und Runter");
}
// Variables declaration - do not modify
private javax.swing.JButton btStart;
private javax.swing.JPanel paZeichne;
private javax.swing.JTextField tfAusgabe;
// End of variables declaration
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
tast = e.getKeyCode();
delegateKeyCommand(e.getKeyCode(), true);
}
public void keyReleased(KeyEvent e)
{
delegateKeyCommand(e.getKeyCode(), false);
}
private void delegateKeyCommand(int code, boolean isDown)
{
switch (code) {
case KeyEvent.VK_UP:
downKeys[UP] = isDown;
break;
case KeyEvent.VK_DOWN:
downKeys[DOWN] = isDown;
break;
case KeyEvent.VK_W:
downKeys[W] = isDown;
break;
case KeyEvent.VK_S:
downKeys[S] = isDown;
break;
default:
;
}
}
}
Zuletzt bearbeitet von einem Moderator: