-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
381 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package game.pong; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.*; | ||
import javax.swing.*; | ||
|
||
public class Ball extends Rectangle { | ||
|
||
Random random; | ||
int xVelocity; | ||
int yVelocity; | ||
int initialSpeed = 3; | ||
|
||
Ball(int x, int y, int width, int height) { | ||
super(x, y, width, height); | ||
random = new Random(); | ||
int randomXDirection = random.nextInt(2); //0 -> left ; 1 -> right | ||
if (randomXDirection == 0) | ||
randomXDirection--; | ||
setXDirection(randomXDirection * initialSpeed); | ||
|
||
int randomYDirection = random.nextInt(2); //0 -> left ; 1 -> right | ||
if (randomYDirection == 0) | ||
randomYDirection--; | ||
setYDirection(randomYDirection * initialSpeed); | ||
} | ||
|
||
public void setXDirection(int randomXDirection) { | ||
xVelocity = randomXDirection; | ||
} | ||
|
||
public void setYDirection(int randomYDirection) { | ||
yVelocity = randomYDirection; | ||
} | ||
|
||
public void move() { | ||
x += xVelocity; | ||
y += yVelocity; | ||
} | ||
|
||
public void draw(Graphics g) { | ||
g.setColor(Color.white); | ||
g.fillOval(x, y, width, height); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package game.pong; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.*; | ||
import javax.swing.*; | ||
|
||
public class GameFrame extends JFrame { | ||
|
||
GamePanel panel; | ||
|
||
GameFrame() { | ||
panel = new GamePanel(); | ||
this.add(panel); | ||
this.setTitle("Pong Game"); | ||
this.setResizable(false); //pencerenin büyüklüklerininin sonradan yanlışlıkla değiştirilememesi için | ||
this.setBackground(Color.BLACK); //arka plan | ||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
this.pack(); //GamePanel penceresinin, GameFrame penceresi ile birebir olması için | ||
this.setVisible(true); | ||
this.setLocationRelativeTo(null); //programın ekranın ortasında açılması için | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package game.pong; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.*; | ||
import javax.swing.*; | ||
import javax.xml.crypto.dsig.spec.DigestMethodParameterSpec; | ||
|
||
public class GamePanel extends JPanel implements Runnable { | ||
|
||
static final int GAME_WIDTH = 1000; | ||
static final int GAME_HEIGHT = (int)(GAME_WIDTH * (0.5555)); | ||
static final Dimension SCREEN_SIZE = new Dimension(GAME_WIDTH, GAME_HEIGHT); | ||
static final int BALL_DIAMETER = 20; | ||
static final int PADDLE_WIDTH = 25; | ||
static final int PADDLE_HEIGHT = 100; | ||
|
||
//GamePanel her şeye erişiyor görüldüğü gibi. | ||
|
||
Thread gameThread; | ||
Image image; | ||
Graphics graphics; | ||
Random random; | ||
Paddle paddle1; | ||
Paddle paddle2; | ||
Ball ball; | ||
Score score; | ||
|
||
|
||
GamePanel() { | ||
newPaddles(); | ||
newBall(); | ||
score = new Score(GAME_WIDTH, GAME_HEIGHT); | ||
this.setFocusable(true); | ||
this.addKeyListener(new AL()); | ||
this.setPreferredSize(SCREEN_SIZE); | ||
|
||
gameThread = new Thread(this); | ||
gameThread.start(); | ||
} | ||
|
||
public void newBall() { | ||
random = new Random(); | ||
ball = new Ball((GAME_WIDTH / 2) - (BALL_DIAMETER / 2), random.nextInt(GAME_HEIGHT - BALL_DIAMETER), BALL_DIAMETER, BALL_DIAMETER); | ||
} | ||
|
||
public void newPaddles() { | ||
paddle1 = new Paddle(0, (GAME_HEIGHT / 2) - (PADDLE_HEIGHT / 2), PADDLE_WIDTH, PADDLE_HEIGHT, 1); | ||
paddle2 = new Paddle(GAME_WIDTH - PADDLE_WIDTH, (GAME_HEIGHT / 2) - (PADDLE_HEIGHT / 2), PADDLE_WIDTH, PADDLE_HEIGHT, 2); | ||
} | ||
|
||
public void paint(Graphics g) { | ||
image = createImage(getWidth(), getHeight()); | ||
graphics = image.getGraphics(); | ||
draw(graphics); | ||
g.drawImage(image,0 ,0,this); | ||
} | ||
|
||
public void draw(Graphics g) { | ||
paddle1.draw(g); | ||
paddle2.draw(g); | ||
ball.draw(g); | ||
score.draw(g); | ||
} | ||
|
||
public void move() { | ||
paddle1.move(); | ||
paddle2.move(); | ||
ball.move(); | ||
} | ||
|
||
public void checkCollision() { | ||
|
||
//bounce ball off top & bottom window edges | ||
if (ball.y <= 0) { | ||
ball.setYDirection(-ball.yVelocity); | ||
} | ||
|
||
if (ball.y >= GAME_HEIGHT - BALL_DIAMETER) { | ||
ball.setYDirection(-ball.yVelocity); | ||
} | ||
|
||
//bounces ball off paddles | ||
if (ball.intersects(paddle1)) { | ||
ball.xVelocity = Math.abs(ball.xVelocity); // *= -1; | ||
ball.xVelocity++; //optional for more diffuculty | ||
|
||
if (ball.yVelocity > 0) | ||
ball.yVelocity++; //optional for more diffuculty | ||
else | ||
ball.yVelocity--; //optional for more diffuculty | ||
ball.setXDirection(ball.xVelocity); | ||
ball.setYDirection(ball.yVelocity); | ||
} | ||
if (ball.intersects(paddle2)) { | ||
ball.xVelocity = Math.abs(ball.xVelocity); // *= -1; | ||
ball.xVelocity++; //optional for more diffuculty | ||
|
||
if (ball.yVelocity > 0) | ||
ball.yVelocity++; //optional for more diffuculty | ||
else | ||
ball.yVelocity--; //optional for more diffuculty | ||
ball.setXDirection(-ball.xVelocity); | ||
ball.setYDirection(ball.yVelocity); | ||
} | ||
|
||
//stops paddles at window edges | ||
if (paddle1.y <= 0) | ||
paddle1.y = 0; | ||
if (paddle1.y >= (GAME_HEIGHT - PADDLE_HEIGHT)) | ||
paddle1.y = GAME_HEIGHT - PADDLE_HEIGHT; | ||
|
||
if (paddle2.y <= 0) | ||
paddle2.y = 0; | ||
if (paddle2.y >= (GAME_HEIGHT - PADDLE_HEIGHT)) | ||
paddle2.y = GAME_HEIGHT - PADDLE_HEIGHT; | ||
|
||
//give a player 1 point and creates new paddles & ball | ||
if(ball.x <= 0) { | ||
score.player2++; | ||
newPaddles(); | ||
newBall(); | ||
System.out.println(score.player2); | ||
} | ||
|
||
if(ball.x >= GAME_WIDTH - BALL_DIAMETER) { | ||
score.player1++; | ||
newPaddles(); | ||
newBall(); | ||
System.out.println(score.player1); | ||
} | ||
} | ||
|
||
public void run() { | ||
//game loop | ||
long lastTime = System.nanoTime(); | ||
double amountOfTicks = 60.0; //fps = Frame Per Second | ||
double ns = 1000000000 / amountOfTicks; | ||
double delta = 0; | ||
|
||
while (true) { | ||
long now = System.nanoTime(); | ||
delta += (now - lastTime) / ns; | ||
lastTime = now; | ||
|
||
if (delta >= 1) { | ||
move(); | ||
checkCollision(); | ||
repaint(); | ||
delta--; | ||
} | ||
} | ||
} | ||
|
||
public class AL extends KeyAdapter{ | ||
public void keyPressed(KeyEvent e) { | ||
paddle1.keyPressed(e); | ||
paddle2.keyPressed(e); | ||
} | ||
|
||
public void keyReleased(KeyEvent e) { | ||
paddle1.keyReleased(e); | ||
paddle2.keyReleased(e); | ||
} | ||
public void setYDirection(int yDirection){ | ||
|
||
} | ||
public void move(){ | ||
|
||
} | ||
public void draw(Graphics g) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package game.pong; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.*; | ||
import javax.swing.*; | ||
|
||
public class Paddle extends Rectangle{ | ||
|
||
int id; | ||
int yVelocity; | ||
int speed = 10; | ||
|
||
Paddle(int x, int y, int PADDLE_WIDTH, int PADDLE_HEIGHT, int id) { | ||
super(x, y, PADDLE_WIDTH, PADDLE_HEIGHT); | ||
this.id = id; | ||
} | ||
|
||
public void keyPressed(KeyEvent e) { | ||
switch (id) { | ||
case 1: | ||
if (e.getKeyCode() == KeyEvent.VK_W) { //paddle1 W tuşuna bastığında | ||
setYDirection(-speed); | ||
move(); | ||
} | ||
if (e.getKeyCode() == KeyEvent.VK_S) { //paddle1 W tuşuna bastığında | ||
setYDirection(speed); | ||
move(); | ||
} | ||
break; | ||
case 2: | ||
if (e.getKeyCode() == KeyEvent.VK_UP) { //paddle1 W tuşuna bastığında | ||
setYDirection(-speed); | ||
move(); | ||
} | ||
if (e.getKeyCode() == KeyEvent.VK_DOWN) { //paddle1 W tuşuna bastığında | ||
setYDirection(speed); | ||
move(); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
public void keyReleased(KeyEvent e) { | ||
switch (id) { | ||
case 1: | ||
if (e.getKeyCode() == KeyEvent.VK_W) { //paddle1 W tuşuna bastığında | ||
setYDirection(0); | ||
move(); | ||
} | ||
if (e.getKeyCode() == KeyEvent.VK_S) { //paddle1 W tuşuna bastığında | ||
setYDirection(0); | ||
move(); | ||
} | ||
break; | ||
case 2: | ||
if (e.getKeyCode() == KeyEvent.VK_UP) { //paddle1 W tuşuna bastığında | ||
setYDirection(0); | ||
move(); | ||
} | ||
if (e.getKeyCode() == KeyEvent.VK_DOWN) { //paddle1 W tuşuna bastığında | ||
setYDirection(0); | ||
move(); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
public void setYDirection(int yDirection) { | ||
yVelocity = yDirection; | ||
|
||
} | ||
|
||
public void move() { | ||
y = y + yVelocity; | ||
} | ||
|
||
public void draw(Graphics g) { | ||
if (id == 1) | ||
g.setColor(Color.blue); | ||
else | ||
g.setColor(Color.red); | ||
|
||
g.fillRect(x, y, width, height); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package game.pong; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.*; | ||
import javax.swing.*; | ||
|
||
public class PongGame { | ||
public static void main(String[] args) { | ||
|
||
GameFrame frame = new GameFrame(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package game.pong; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.*; | ||
import javax.swing.*; | ||
|
||
public class Score extends Rectangle{ | ||
|
||
static int GAME_WIDTH; | ||
static int GAME_HEIGHT; | ||
int player1; | ||
int player2; | ||
|
||
Score(int GAME_WIDTH, int GAME_HEIGHT) { | ||
Score.GAME_WIDTH = GAME_WIDTH; | ||
Score.GAME_HEIGHT = GAME_HEIGHT; | ||
} | ||
|
||
public void draw(Graphics g){ | ||
g.setColor(Color.white); | ||
g.setFont(new Font("Consolas", Font.PLAIN, 60)); | ||
|
||
g.drawLine(GAME_WIDTH / 2, 0 , GAME_WIDTH / 2, GAME_HEIGHT); | ||
|
||
g.drawString(String.valueOf(player1 / 10) + String.valueOf(player1 % 10), (GAME_WIDTH / 2) - 85, 50); | ||
g.drawString(String.valueOf(player2 / 10) + String.valueOf(player2 % 10), (GAME_WIDTH / 2) + 25, 50); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# Java-Projects | ||
# Java-Projects | ||
|
||
## Game Projects | ||
|
||
1. Snake | ||
2. Pong | ||
3. |