tcppunk
Erfahrenes Mitglied
Morgen! Ich möchte hier mal meinen Moorhuhnclon "ausstellen".
Das ganze ist nur zur Übung entstanden, deshalb ist es auch nicht unbedingt professionell also entschuldigt bitte das geflimmere, den unübersichtlichen Quellcode etc.
Fragen, Anregungen, Kritiken etc. sind herzlich wilkommen.
(Die Bilder in der Zipdatei müssen im gleichen Ordner wie die Classdatei liegen)
Das ganze ist nur zur Übung entstanden, deshalb ist es auch nicht unbedingt professionell also entschuldigt bitte das geflimmere, den unübersichtlichen Quellcode etc.
Fragen, Anregungen, Kritiken etc. sind herzlich wilkommen.
(Die Bilder in der Zipdatei müssen im gleichen Ordner wie die Classdatei liegen)
Code:
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import javax.imageio.ImageIO;
public class Moorhuhn extends Frame implements WindowListener, ActionListener{
Image erde, himmel, amsel, patrone;
MenuBar mb1;
Menu m1;
MenuItem neu, stop, restart, schwierigkeit;
int birdcount = 50, birddone = 0, abomb = 1,
bomblauf = 0, treffer = 0, mousex = 0,
mousey = 0, schuesse = 0, mag = 8, letit = 0,
moveit = 3;
int birdx[] = new int[birdcount];
int birdy[] = new int[birdcount];
long zeit = System.currentTimeMillis()/1000, newzeit, lasttime;
boolean run1 = true, comparehit = false, mousemade = true, checkabomb = false, loading = true;
boolean birdkill[] = new boolean[birdcount];
Button bt1,bt2,bt3;
public Moorhuhn(){
this.setBounds(100,100,800,600);
this.setBackground(new Color(230,230,230));
this.setTitle("Dumdidum");
this.setCursor(Cursor.CROSSHAIR_CURSOR );
this.setVisible(true);
this.setLayout(null);
this.setResizable(false);
bt1 = new Button("schwerer");
bt1.setVisible(false);
bt1.setBounds(250, 280, 90, 30);
bt1.setBackground(new Color(220,220,220));
bt2 = new Button("einfacher");
bt2.setVisible(false);
bt2.setBounds(350, 280, 90, 30);
bt2.setBackground(new Color(220,220,220));
bt3 = new Button("einstellen");
bt3.setVisible(false);
bt3.setBounds(450, 280, 90, 30);
bt3.setBackground(new Color(220,220,220));
try{
erde = ImageIO.read(new File("erde.jpg"));
himmel = ImageIO.read(new File("himmel.jpg"));
amsel = ImageIO.read(new File("amsel.gif"));
patrone = ImageIO.read(new File("patrone.gif"));
}catch(Exception ex){
}
mb1 = new MenuBar();
m1 = new Menu("Spiel");
mb1.add(m1);
neu = new MenuItem("Start");
stop = new MenuItem("Stop");
restart = new MenuItem("Neustarten");
schwierigkeit = new MenuItem("Schwierigkeit");
m1.insert(neu,1);
m1.insert(stop,2);
m1.insert(restart,3);
m1.insert(schwierigkeit,4);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.addWindowListener(this);
this.setMenuBar(mb1);
bt1.addActionListener(this);
bt3.addActionListener(this);
bt2.addActionListener(this);
neu.addActionListener(this);
stop.addActionListener(this);
restart.addActionListener(this);
schwierigkeit.addActionListener(this);
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton() == e.BUTTON1 && mag > 0){
mousex = e.getX();
mousey = e.getY();
comparehit = true;
schuesse++;
mag--;
}
if(e.getButton() == e.BUTTON2){
if(abomb > 0){
checkabomb = true;
abomb--;
comparehit = true;
}
}
if(e.getButton() == e.BUTTON3){
if(mag == 0){
mag = 8;
}
}
}
});
}
public static void main(String args[]){
Moorhuhn mt1= new Moorhuhn();
mt1.masterRun();
}
public void update(Graphics g){
paint (g);
}
public void masterRun(){
for(int y = 0; y < birdcount; y++){
birdx[y] = (int)(Math.random()*- (birdcount * 40));
birdy[y] = (int)(Math.random()*470 + 70);
}
try{
while(1<2){
Thread.sleep(70);
if(run1){
for(int y = 0; y < birdcount; y++){
if(!birdkill[y]){
if (birdx[y] < 800){
birdx[y] = birdx[y] + moveit;
} else {
birdx[y] = 0;
birdy[y] = (int)(Math.random()*470 + 70);
birddone++;
}
} else {
birdx[y] = birdx[y] + 3;
birdy[y] = birdy[y] +3;
}
}
if (comparehit){
if(!checkabomb){
mousemade = true;
for( int k = 0; k < birdcount; k++){
if (mousex + 10 >= birdx[k] && mousex <= birdx[k] + 40 && mousey + 15 >= birdy[k] && mousey <= birdy[k] +20 && !birdkill[k]){
treffer++;
birdkill[k] = true;
}
}
} else{
for( int k = 0; k < birdcount; k++){
if (birdx[k] > -150 && birdx[k] < 800 && !birdkill[k]){
treffer++;
birdkill[k] = true;
}
}
}
comparehit = false;
}
newzeit = System.currentTimeMillis() / 1000 - zeit;
if (newzeit > 1){
loading = false;
}
this.setTitle(treffer+" Treffer, "+ schuesse +" Schuesse, noch " + (birdcount - treffer) +" Voegel, "+ birddone+" Voegel haben es geschafft, "+ newzeit + " Sekunden, FPS: "+ (1000/(System.currentTimeMillis() - lasttime)));
lasttime = System.currentTimeMillis();
this.repaint();
}
}
} catch (Exception ex){
}
}
public void paint(Graphics g){
if(loading){
g.setFont(new Font("ARIAL",1,30));
g.drawString("Am Laden............. (hoho Nobel!)", 40,290);
}else{
g.drawImage(himmel,0,3,null);
g.setColor(Color.BLACK);
g.drawString("Moorhuhn 0.7b Copyright: Tcppunk", 10,70);
for(int z = 0; z < birdcount; z++){
if(birdx[z] > 0){
g.drawImage(amsel,birdx[z], birdy[z],null);
}
}
g.drawImage(erde,0,550,null);
for (int z = 1; z <= mag; z++){
g.drawImage(patrone,220 + z*20, 550,null);
}
for (int z = 1; z <= abomb; z++){
g.setColor(Color.BLACK);
g.fillOval(370 + z*40, 565, 30, 30);
g.fillRect(380 + z*40, 555, 10, 20);
}
if (mousemade){
g.setColor(Color.RED);
g.fillOval(mousex-5,mousey-5,10,10);
letit++;
if (letit == 2){
mousemade = false;
letit = 0;
}
}
if (checkabomb || bomblauf > 0){
checkabomb = false;
if (bomblauf < 20){
g.setColor(new Color(13*bomblauf,13*bomblauf,10*bomblauf));
g.fillRect(0,0,800,600);
bomblauf++;
} else {
bomblauf = 0;
}
}
}
}
//windowListener
public void windowActivated(WindowEvent e){
}
public void windowClosed(WindowEvent e){
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowDeactivated(WindowEvent e){
}
public void windowDeiconified(WindowEvent e){
}
public void windowIconified(WindowEvent e){
}
public void windowOpened(WindowEvent e){
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == neu){
run1 = true;
}
if (e.getSource() == stop){
run1 = false;
}
if (e.getSource() == restart){
for(int y = 0; y < birdcount; y++){
birdx[y] = (int)(Math.random()*-800);
birdy[y] = (int)(Math.random()*500 + 20);
birdkill[y] = false;
}
treffer = 0;
zeit = System.currentTimeMillis()/1000;
mousex = 0;
mousey = 0;
schuesse = 0;
mag = 8;
letit = 0;
birddone = 0;
moveit = 3;
abomb = 1;
bomblauf = 0;
}
if (e.getSource() == schwierigkeit){
bt2.setVisible(true);
bt1.setVisible(true);
bt3.setVisible(true);
run1 = false;
}
if (e.getSource() == bt1){
moveit++;
this.setTitle("Schwierigkeit: " +moveit);
}
if (e.getSource() == bt2){
moveit--;
this.setTitle("Schwierigkeit: " +moveit );
}
if (e.getSource() == bt3){
bt2.setVisible(false);
bt1.setVisible(false);
bt3.setVisible(false);
this.setTitle(treffer+" Treffer, bei "+ schuesse +" Schuessen");
run1 = true;
}
}
//ende
}