hallo, Ich versuche ein kleines Spiel zu programmieren mit Hilfe von Sourcecode den ich im Internet gefunden habe,den ich abgeändert habe:
Das Apllet lässt mehrere Bilder über der bildschirm wandern, bei klick auf ein bild wird dieses gelöscht und ein neues erzeugt -> Treffer !
(Teil 1)
Jetzt habe ich versucht ein (quasi)menü zu implementieren. 5 Bilder- bei klick auf eines soll der maus cursur verändert werden. (Teil2)
Mein Problem ist das ich (Teil 1) und (Teil2)
unabhängig angegangen bin und es mir jetzt nicht gelingt die beiden zusammenzufügen.
Ich hoffe ihr wisst eine Lösung!
Vielen dank schon mal fü die Mühe. :toll:
Mfg Halo_Player
Teil 1:
Teil2:
[Edit by Beni: den Code einmal durch einen Formater gejagt, damit er eingerückt ist.]
Das Apllet lässt mehrere Bilder über der bildschirm wandern, bei klick auf ein bild wird dieses gelöscht und ein neues erzeugt -> Treffer !
Jetzt habe ich versucht ein (quasi)menü zu implementieren. 5 Bilder- bei klick auf eines soll der maus cursur verändert werden. (Teil2)
Mein Problem ist das ich (Teil 1) und (Teil2)
unabhängig angegangen bin und es mir jetzt nicht gelingt die beiden zusammenzufügen.
Ich hoffe ihr wisst eine Lösung!
Vielen dank schon mal fü die Mühe. :toll:
Mfg Halo_Player
Teil 1:
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class SplashShooter extends Applet {
int x1;
int y1;
int x2;
int y2;
int x3; // Ganzahlvariablen
int y3;
int x4;
int y4;
int x5;
int y5;
private Image dbImage;
private Graphics dbg;
Image scheisse, flasche, dose, granate, tomate;
Image hintergrund; // Bildvariablen
Image bilder;
int delay = 50; // timernoistartzeit(bewegung)
javax.swing.Timer los = new javax.swing.Timer(delay, new starterAlle());// macht
// nen
// timer
Image huhn; // imageobjekt für huhn
Moorhuhn[] hennen = new Moorhuhn[5]; // max. moorhuhnzahl
AudioClip schuss, anfang; // ton
int bodycount = 0;
Label counter = new Label("Los geht's!!! - Wähle deine Waffe aus: ");
public void init() {
anfang = getAudioClip(getCodeBase(), "doomsday.au");
anfang.play();
huhn = getImage(getCodeBase(), "huhn2.gif"); // bild vom huhn
setSize(1024, 768); // appletgröße
setBackground(Color.black);
setForeground(Color.white);
scheisse = getImage(getCodeBase(), "Scheisshaufen.gif");// laden einer
// Imagedatei
hintergrund = getImage(getCodeBase(), "stage.gif");
flasche = getImage(getCodeBase(), "flasche.gif");
tomate = getImage(getCodeBase(), "Tomate.gif");
granate = getImage(getCodeBase(), "Granate.gif");
dose = getImage(getCodeBase(), "Dose.gif");
add(counter);
for (int i = 0; i < hennen.length; i++) { // mach die hennen
hennen[i] = new Moorhuhn(huhn);
}
los.start(); // starte den timer
addMouseListener(new schuss());
}
public void start() {
}
public void paint( Graphics g ) {
counter.setBounds(this.getWidth() * 1 / 5 + 20, this.getHeight() - 100,
250, 20);
for (int i = 0; i < hennen.length; i++) { // hennen anmalen
hennen[i].paint(g);
}
}
class starterAlle implements ActionListener { // timeraction
public void actionPerformed( ActionEvent evt ) {
for (int i = 0; i < hennen.length; i++) { // alle hennen
// fliegenlassen
hennen[i].move();
}
repaint();
}
}
class schuss implements MouseListener {
public void mousePressed( MouseEvent e ) {
int xg = e.getX();
int yg = e.getY();
for (int i = 0; i < hennen.length; i++) { // hennen fliegenlassen
if (hennen[i].erschossen(xg, yg)) {
hennen[i] = new Moorhuhn(huhn);
bodycount++;
counter.setText(bodycount + " Sidos");
if (bodycount >= 100) {
counter.setText("100 Sidos- Ausrottung erfolgreich");
}
;
}
}
schuss.play();
}
public void mouseReleased( MouseEvent e ) {
}
public void mouseEntered( MouseEvent e ) {
}
public void mouseExited( MouseEvent e ) {
}
public void mouseClicked( MouseEvent e ) {
}
}
}
class Moorhuhn extends Canvas implements ActionListener {
static Random r = new Random();
javax.swing.Timer weiter;
Image h2;
int x = -30;
int y = 0;
int richtung;
private Image dbImage;
private Graphics dbg;
public Moorhuhn( Image h ) {
h2 = h;
y = zufall(380);
richtung = zufall(2) % 2;
if (richtung == 0) {
x = (-30);
}
if (richtung == 1) {
x = 530;
}
}
public void paint( Graphics g2 ) {
g2.drawImage(h2, x, y, this);
}
public void update( Graphics g2 ) {
y = zufall(380);
g2.drawImage(h2, x, y, this);
// Initialisierung des DoubleBuffers
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// Bildschirm im Hintergrund löschen
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// Auf gelöschten Hintergrund Vordergrund zeichnen
dbg.setColor(getForeground());
paint(dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm
// anzeigen
g2.drawImage(dbImage, 0, 0, this);
}
public static int zufall( int grenze ) { // zufallszahl zw. 0 und grenze
return r.nextInt(grenze);
}
public void move() {
if (richtung == 0) {
x = x + 5;
}
if (richtung == 1) {
x = x - 5;
}
y = y + zufall(10) - 5;
if (x > 530) {
x = -30;
}
if (x < -30) {
x = 500;
}
if (y > 399) {
y = 0;
}
if (y < -20) {
y = 399;
}
}
public boolean erschossen( int xs, int ys ) {
if (x <= xs && xs <= (x + h2.getWidth(this)) && y <= ys
&& ys <= (y + h2.getHeight(this))) {
return true;
} else {
return false;
}
}
public void noi() {
weiter = new javax.swing.Timer(zufall(10000), this);
weiter.start();
}
public void actionPerformed( ActionEvent e ) {
move();
weiter.stop();
}
}
Code:
import java.applet.AudioClip;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class SidoShooter implements MouseListener {
// Deklarationsteil:
int mausX, mausY;
int x1;
int y1;
int x2;
int y2;
int x3; // Ganzahlvariablen
int y3;
int x4;
int y4;
int x5;
int y5;
int sidoX, sidoY;
Image dbImage;
Graphics dbg;
Image scheisse, flasche, dose, granate, tomate;
Image hintergrund; // Bildvariablen
Image sido;
Cursor cursorFlasche, cursorDose, cursorGranate, cursorTomate,
cursorScheisse;
// Cursorvariablen
AudioClip glas, explosion, furz;
AudioClip random; // Audiovariablen
AudioClip anfang;
Label counter;
public void init() {// Konstruktor vom Applet
addMouseListener(this);
counter = new Label("Los geht's!!! - Wähle deine Waffe aus: ");
add(counter);
x1 = this.getWidth() * 1 / 5 - 100;// Ganzzahlvariablen werden Werte
// übergeben
y1 = this.getHeight() - 65;
x2 = this.getWidth() * 2 / 5 - 125;
y2 = this.getHeight() - 60;
x3 = this.getWidth() * 3 / 5 - 135;
y3 = this.getHeight() - 68;
x4 = this.getWidth() * 4 / 5 - 125;
y4 = this.getHeight() - 60;
x5 = this.getWidth() * 5 / 5 - 140;
y5 = this.getHeight() - 70;
anfang = getAudioClip(getCodeBase(), "doomsday.au");// laden einer
// Audiodatei
anfang.loop();// abspielen der Audiodatei in einer Loop-Schleife
furz = getAudioClip(getCodeBase(), "fart.wav");
explosion = getAudioClip(getCodeBase(), "explosion.wav");
glas = getAudioClip(getCodeBase(), "breaking glass.wav");
scheisse = getImage(getCodeBase(), "Scheisshaufen.gif");// laden einer
// Imagedatei
hintergrund = getImage(getCodeBase(), "stage.gif");
flasche = getImage(getCodeBase(), "flasche.gif");
tomate = getImage(getCodeBase(), "Tomate.gif");
granate = getImage(getCodeBase(), "Granate.gif");
dose = getImage(getCodeBase(), "Dose.gif");
sido = getImage(getCodeBase(), "huhn2.gif");
cursorFlasche = getToolkit().createCustomCursor(flasche,
new Point(0, 0), "Cursor");// laden
// von
// CursorDateien
cursorScheisse = getToolkit().createCustomCursor(scheisse,
new Point(0, 0), "Cursor");
cursorDose = getToolkit().createCustomCursor(dose, new Point(0, 0),
"Cursor");
cursorGranate = getToolkit().createCustomCursor(granate,
new Point(0, 0), "Cursor");
cursorTomate = getToolkit().createCustomCursor(tomate, new Point(0, 0),
"Cursor");
}
public void paint( Graphics g )// paint-Methode
{
x1 = this.getWidth() * 1 / 5 - 100;
y1 = this.getHeight() - 65;
x2 = this.getWidth() * 2 / 5 - 125;
y2 = this.getHeight() - 60;
x3 = this.getWidth() * 3 / 5 - 135;
y3 = this.getHeight() - 68;
x4 = this.getWidth() * 4 / 5 - 125;
y4 = this.getHeight() - 60;
x5 = this.getWidth() * 5 / 5 - 140;
y5 = this.getHeight() - 70;
counter.setBounds(this.getWidth() * 1 / 5 + 20, this.getHeight() - 100,
250, 20);
g.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 24));// font
// erzeugen
// +
// erstellen
g
.drawString(
"Du befindest dich auf dem Splash - Deine Lieblingsrapper sind auf der Bühne: ",
45, 50);
// text schreiben
g.drawLine(50, this.getHeight() - 10, this.getWidth() - 50, this
.getHeight() - 10);// line zeichnen
g.drawLine(50, this.getHeight() - 80, this.getWidth() - 50, this
.getHeight() - 80);
g.drawLine(50, this.getHeight() - 10, 50, this.getHeight() - 80);
g.drawLine(this.getWidth() - 50, this.getHeight() - 10,
this.getWidth() - 50, this.getHeight() - 80);
g.drawLine(this.getWidth() * 1 / 5, this.getHeight() - 10, this
.getWidth() * 1 / 5, this.getHeight() - 105);
g.drawLine(this.getWidth() * 2 / 5, this.getHeight() - 10, this
.getWidth() * 2 / 5, this.getHeight() - 80);
g.drawLine(this.getWidth() * 3 / 5, this.getHeight() - 10, this
.getWidth() * 3 / 5, this.getHeight() - 80);
g.drawLine(this.getWidth() * 4 / 5, this.getHeight() - 10, this
.getWidth() * 4 / 5, this.getHeight() - 105);
g.drawLine(this.getWidth() * 1 / 5, this.getHeight() - 105, this
.getWidth() * 4 / 5, this.getHeight() - 105);
g.drawImage(flasche, x1, y1, this);// Bild positionieren
g.drawImage(scheisse, x2, y2, this);
g.drawImage(tomate, x3, y3, this);
g.drawImage(granate, x4, y4, this);
g.drawImage(dose, x5, y5, this);
g.drawImage(hintergrund, 130, 70, this);
}
public void update( Graphics g ) {
// Initialisierung des DoubleBuffers
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// Bildschirm im Hintergrund löschen
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// Auf gelöschten Hintergrund Vordergrund zeichnen
dbg.setColor(getForeground());
paint(dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm
// anzeigen
g.drawImage(dbImage, 0, 0, this);
}
public boolean flascheClick() {
if (x1 <= mausX && mausX <= (x1 + flasche.getWidth(this))
&& y1 <= mausY && mausY <= (y1 + flasche.getHeight(this))) {
return true;
} else {
return false;
}
}
public boolean scheisseClick() {
if (x2 <= mausX && mausX <= (x2 + scheisse.getWidth(this))
&& y2 <= mausY && mausY <= (y2 + scheisse.getHeight(this))) {
return true;
} else {
return false;
}
}
public boolean tomateClick() {
if (x3 <= mausX && mausX <= (x3 + tomate.getWidth(this)) && y3 <= mausY
&& mausY <= (y3 + tomate.getHeight(this))) {
return true;
} else {
return false;
}
}
public boolean granateClick() {
if (x4 <= mausX && mausX <= (x4 + granate.getWidth(this))
&& y4 <= mausY && mausY <= (y4 + granate.getHeight(this))) {
return true;
} else {
return false;
}
}
public boolean doseClick() {
if (x5 <= mausX && mausX <= (x5 + dose.getWidth(this)) && y5 <= mausY
&& mausY <= (y5 + dose.getHeight(this))) {
return true;
}
else {
return false;
}
}
public void mousePressed( MouseEvent e ) {
}
public void mouseReleased( MouseEvent e ) {
}
public void mouseEntered( MouseEvent e ) {
}
public void mouseExited( MouseEvent e ) {
}
public void mouseClicked( MouseEvent e ) {
mausX = e.getX();
mausY = e.getY();
if (flascheClick() == true) {
setCursor(cursorFlasche);
} else if (granateClick() == true) {
setCursor(cursorGranate);
} else if (doseClick() == true) {
setCursor(cursorDose);
} else if (scheisseClick() == true) {
setCursor(cursorScheisse);
} else if (tomateClick() == true) {
setCursor(cursorTomate);
}
;
}
}
[Edit by Beni: den Code einmal durch einen Formater gejagt, damit er eingerückt ist.]