/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author ozaytunctan13
*/
public class GameBall extends JFrame implements ActionListener, MouseMotionListener {
private int ballxSpeed = 7;
private int ballySpeed = 5;
private int paddlex = 0;
private JPanel screen;
private int scren_w = 800;
private int scren_h = 600;
private Ball top;
private RectCollision rect;
private int Ball_x = 0;
private int Ball_y = 0;
private int Ball_w = 30;
private int Ball_h = 30;
private int Rect_x = scren_w / 2;
private int Rect_y = scren_h - 30;
private int Rect_w = 100;
private int Rect_h = 20;
private long ball_speed = 1000;
private boolean gameOver = false;
public void initialize() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.setTitle(GameBall.class.getName());
this.setBounds(200, 30, 800, 650);
this.setBackground(Color.blue);
this.setVisible(true);
screen = new JPanel();
this.setResizable(false);
screen.setSize(800, 600);
screen.setBackground(Color.BLACK);
this.add(screen);
Ball_x = (int) (screen.getSize().width) / 2;
this.addMouseMotionListener(this);
this.setLocationRelativeTo(null);
}
public GameBall(String title) {
this.setTitle(title);
initialize();
}
@Override
public void paint(Graphics g) {
super.paint(g);
top = new Ball(Ball_x, Ball_y, Ball_w, Ball_h);
rect = new RectCollision(paddlex, Rect_y, Rect_w, Rect_h);
Graphics fr = screen.getGraphics();
top.onDraw(fr);
Graphics fr2 = screen.getGraphics();
rect.onDraw(fr2);
//Collision();
}
public void update(Graphics gr) {
}
public void Collision() {
Ball_x = Ball_x + ballxSpeed;
Ball_y = Ball_y + ballySpeed;
// Window yukarı
if (Ball_x >= paddlex && Ball_x <= paddlex + 100 && Ball_y >= 475) {
ballySpeed = -7;
// score++;
}
if (Ball_y >= 800) {
// score = 0;
Ball_y = 30;
gameOver = true;
}
// Window aşağı
if (Ball_y <= 0) {
ballySpeed = 7;
}
// Window sağ
if (Ball_x >= 800) {
ballxSpeed = -5;
}
// Window sol
if (Ball_x <= 0) {
ballxSpeed = 5;
}
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
Collision();
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
//Rect sadece x yönünde hareket etmektedir width.
paddlex = e.getX() - 50;
repaint();
}
public static void main(String[] args) {
GameBall b = new GameBall("title");
Timer t = new Timer(30, b);
t.start();
}
}
/////////////////////////// Rect Class////////////////////////////////
public class RectCollision extends Rectangle{
private int Ball_x;
private int Ball_y;
private int Ball_w;
private int Ball_h;
public RectCollision(int x, int y, int width, int height) {
super(x, y, width, height);
Ball_x = x;
Ball_y = y;
Ball_w = width;
Ball_h = height;
}
public void onDraw(Graphics g) {
g.setColor(new Color(255,255,30));
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
}
}
////////////////////////////////////////Ball Class/////////////////////////////////////
public class Ball extends Rectangle {
private int Ball_x;
private int Ball_y;
private int Ball_w;
private int Ball_h;
public Ball(int x, int y, int width, int height) {
super(x, y, width, height);
Ball_x = x;
Ball_y = y;
Ball_w = width;
Ball_h = height;
}
public void onDraw(Graphics g) {
g.setColor(new Color(255,255,30));
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
}
public static void main(String[] args) {
JFrame f=new JFrame("Title");
f.setSize(500,500);
Ball b=new Ball(5,5,30,30);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Hiç yorum yok:
Yorum Gönder
Lütfen yorumlarınızı iletiniz...