Java Applet ile yapılmış ördek vurma oyunu

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javafx.scene.input.KeyCode;

public class ördekvurma extends Applet implements KeyListener, Runnable {

    int x, y, i, kusx, kusy, score = 0;
    boolean fırlat, vurdumu;
    double x1, y1, atx, aty;

    public void init() {
        addKeyListener(this);
        setSize(500, 500);
        x = getWidth() / 2;
        y = getHeight();
        x1 = getWidth() / 2;
        y1 = getHeight() - 50;
        vurdumu = true;
        i = 270;
        kusx = 0;
        kusy = 20;
        atx=getWidth()/2;
        aty= getHeight()-50;
        fırlat = true;
    }

    public void paint(Graphics g) {
        g.drawLine(x, y, (int) x1, (int) y1);
        g.drawString("Score= " + score, getWidth() - 50, getHeight() - 20);
        if (atx >= kusx && aty > kusy && atx < kusx + 30 && aty < kusy + 20) {
            g.drawString("Vurdunuz!", getWidth() / 2 - 20, getHeight() / 2);
            score++;
            vurdumu = false;
        }
        if (vurdumu) {
            g.fillOval(kusx, kusy, 30, 20);
            g.fillOval((int) atx-5, (int) aty-5, 10, 10);
        }
        if (!vurdumu) {
            init();
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if (fırlat) {
                x1 = getWidth() / 2 + 50 * (Math.cos((Math.PI / 180) * i));
                y1 = getHeight() + 50 * (Math.sin((Math.PI / 180) * i));
                i++;
                System.out.println(i);
                if (fırlat) {
                    atx = getWidth() / 2 + 50 * (Math.cos((Math.PI / 180) * i));
                    aty = getHeight() + 50 * (Math.sin((Math.PI / 180) * i));
                }
                repaint();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            if (fırlat) {

                x1 = getWidth() / 2 + 50 * (Math.cos((Math.PI / 180) * i));
                y1 = getHeight() + 50 * (Math.sin((Math.PI / 180) * i));
                i--;
                System.out.println(i);
                atx = getWidth() / 2 + 50 * (Math.cos((Math.PI / 180) * i));
                aty = getHeight() + 50 * (Math.sin((Math.PI / 180) * i));
                if (fırlat) {
                    atx = getWidth() / 2 + 50 * (Math.cos((Math.PI / 180) * i));
                    aty = getHeight() + 50 * (Math.sin((Math.PI / 180) * i));
                }
            }
            repaint();
        }
        if (fırlat) {
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                Thread t = new Thread(this);
                if (!vurdumu) {
                    t.stop();
                }
                t.start();
                fırlat = false;
            }
            repaint();
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void run() {
        int at = 51;
        try {
            while (vurdumu) {
                Thread.sleep(50);
                atx = getWidth() / 2 + at * Math.cos((Math.PI / 180) * i);
                aty = getHeight() + at * (Math.sin((Math.PI / 180) * i));
                at += 5;
                kusx += 5;
                repaint();
            }
        } catch (Exception e) {
        }
    }

}

Ekran Görüntüsü;

 

 

Yorumlar

Bu gönderi için yorum yapılmadı.