Sie sind auf Seite 1von 11

import java.awt.

Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class AIPaddle {
int
int
int
int

x;//Horizontal
y;//Vertical
width = 15;
heigth = 40;

int speed = 4;
boolean isTwoPlayer = false;
Rectangle boundingBox;
boolean goingUp = false;
boolean goingDown = false;
public AIPaddle(int x, int y){
this.x = x;
this.y = y;
boundingBox = new Rectangle( x, y, width, heigth);
boundingBox.setBounds( x, y, width, heigth);
}
public void tick(Game game){
boundingBox.setBounds( x, y, width, heigth);
if (!isTwoPlayer){
if(Game.ball.y < y && y >= 0){
y -= speed;
}else if(Game.ball.y > y&& y + heigth <= game.getHeight(
) ){
y += speed;
}
}else{
if(goingUp && y >= 0){
y -= speed;
}else if (goingDown && y+heigth <game.getHeight()){
y += speed;
}
}

/*

//To follow us
if(game.player.y < y){
y -= speed;
}
if(game.player.y > y){
y += speed;
}*/

/*

if (!isTwoPlayer){
if(game.ball.y < y && y >= 0){
y -= speed;
}else if(game.ball.y > y&& y + heigth <= game.getHeight(

) ){
y += speed;
}
}
}
* else {
if(goingUp = true){
y -= speed;
} else if (goingDown = true){
y += speed;
}
*/
}
public void render(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(x, y, width, heigth);
}
}
------------------------------------------------------------------------------------------------------------import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Ball {
int x,y;
int size = 16;
int speed = 3;
int vx, vy;
Rectangle boundingBox;
public Ball(int x, int y){
this.x = x;
this.y = y;
//poz--->right neg<---left
vx = speed;
vy = speed;
boundingBox = new Rectangle(x,y, size, size);
boundingBox.setBounds(this.x, this.y, this.size, this.size);
}
public void tick(Game game){
boundingBox.setBounds(x, y, size, size);
if(x <= 0){

game.p2Score++;
vx = speed;
}else if (x+ size >= game.getWidth()){
game.p1Score++;
vx = -speed;
}
if(y <= 0){
vy = speed;
} else if(y + size >= game.getHeight()){
vy = -speed;
}
x += vx;
y += vy;
paddleCollide(game);
}
private void paddleCollide(Game game){
if(boundingBox.intersects(Game.player.boundingBox)){
vx = speed;
}else if (boundingBox.intersects(Game.AI.boundingBox)){
vx = -speed;
}
}
public void render(Graphics g){
g.setColor(Color.RED);
g.fillOval(x, y, size, size);
}
}
------------------------------------------------------------------------------------------------------------import
import
import
import

java.awt.Canvas;
java.awt.Color;
java.awt.Dimension;
java.awt.Graphics;

import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L; //This constructs the c
lass

public static PlayerPaddle player;


InputHandler IH;
public static AIPaddle AI;
public static Ball ball;

JFrame frame;// Window of the game


public final int WIDTH = 400; //Width of window
public final int HEIGHT = WIDTH / 16 * 9; // Height of window
public final Dimension gameSize = new Dimension(WIDTH, HEIGHT); // Conde
nse WIDTH & HEIGTH into 1 variable
public final String TITLE = "Pong InDev";
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYP
E_INT_RGB);
static //This creates a new image, and loads a frame -- alfa(seethrough)
argb
boolean gameRunning = false; // Whether the game is running
int p1Score, p2Score;
Thread thread;
public void run(){
while(gameRunning){ // If gameRunning = true
tick();
render();
try{
Thread.sleep(7);
}catch(Exception e){
e.printStackTrace();
}
}
}
//This is for applets
public synchronized void start(){
gameRunning = true;
thread = new Thread(this);
thread.start();
//End start method
}
public static synchronized void stop(){
gameRunning = false;
System.exit(0);
//End start method
}
public Game(){ // Constructor
frame = new JFrame();
setMinimumSize(gameSize);

setPreferredSize(gameSize);
setMaximumSize(gameSize);
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle(TITLE);
frame.setLocationRelativeTo(null);
// Relciban semmivel(pl sarokkal, tetve...stb)ezrt kzpre rakja
IH = new InputHandler(this);
player = new PlayerPaddle(10, 60);
AI = new AIPaddle(getWidth() - 25, 60);
ball = new Ball(getWidth() / 2, getHeight() /2);

}
public void tick(){
player.tick(this);
AI.tick(this);
ball.tick(this);
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);// Ennl kisebb szm Screentearing-e
t okozhat
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString("Player 1: " + p1Score, 5, 10);
g.drawString("Player 2: " + p2Score, getWidth() - 65, 10);

//Itt a sorend szmt, nem kerlhet a httr el a player, mert akkor nem lt
zana
player.render(g);
AI.render(g);
ball.render(g);
g.dispose();
bs.show();
/*

paddle.Render(g);
ai.render(g);
b.render(g);

*/
}
public static void main(String[] args){
new MainMenu();
//game.start();
}
}
------------------------------------------------------------------------------------------------------------import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class InputHandler implements KeyListener {


public InputHandler(Game game){
game.addKeyListener(this);
}

public void keyPressed(KeyEvent e) {


int keyCode = e.getKeyCode();
//Player 1
if(keyCode == KeyEvent.VK_W){ // VK_W =Virtual keyboard W gomb
Game.player.goingUp = true;
}
if(keyCode == KeyEvent.VK_S){
Game.player.goingDown = true;
}
//PLAYER 2
if(keyCode == KeyEvent.VK_UP){
Game.AI.goingUp = true;
}
if(keyCode == KeyEvent.VK_DOWN){
Game.AI.goingDown = true;
}
// Egyb rnyt billentyk
if(keyCode == KeyEvent.VK_ESCAPE){
System.exit(0);
}

public void keyReleased(KeyEvent e) {


int keyCode = e.getKeyCode();
//player1 controls
if(keyCode == KeyEvent.VK_W){ // VK_W =Virtual keyboard W gomb
Game.player.goingUp = false;
}
if(keyCode == KeyEvent.VK_S){
Game.player.goingDown = false;
}
//player 2 controls
if(keyCode == KeyEvent.VK_UP){
Game.AI.goingUp = false;
}
if(keyCode == KeyEvent.VK_DOWN){
Game.AI.goingDown = false;
}
}

public void keyTyped(KeyEvent e) {


}

}
-------------------------------------------------------------------------------------------------------------

public class Launcher {


public static void main(String[] args){
new MainMenu();
}
}

-------------------------------------------------------------------------------------------------------------

import
import
import
import
import

javax.swing.JFrame;
javax.swing.JCheckBox;
javax.swing.JButton;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;

public class MainMenu extends JFrame{


private static final long serialVersionUID = 1L;
int screenWidth = 400;
int screenHeight = 160;
int buttonWidth = 100;
int buttonHeight = 40;
JButton Play, Quit;
JCheckBox twoPlayer;
public MainMenu(){
getContentPane().setLayout(null);
//Tpus meghvs
addButtons();
addActions();
Play.setBounds((screenWidth - buttonWidth) / 2, 5,buttonWidth ,b
uttonHeight );//Positions the button
Quit.setBounds((screenWidth - buttonWidth) / 2, 50, buttonWidth,
buttonHeight);
twoPlayer.setBounds((screenWidth - buttonWidth) / 2, 95, buttonW
idth * 2,buttonHeight);
/*twoPlayers.setBounds(0, 95, buttonWidth , buttonHeight);
limitFrameRate.setBounds(0, 140, buttonWidth *3, buttonHeight);
*/
// Gomb kszts
getContentPane().add(Play); // Gombot ad a JFrame-hez
getContentPane().add(Quit);
getContentPane().add(twoPlayer);
/*getContentPane().add(twoPlayers);
getContentPane().add(limitFrameRate);*/
//JFrame cuccok
pack();
setVisible(true);
setLocationRelativeTo(null);
setSize(screenWidth, screenHeight);
setTitle("Pong Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
private void addButtons(){
Play = new JButton("Indt");
Quit = new JButton("Kilp");
twoPlayer = new JCheckBox("Kt Jtkos?");
}
private void addActions(){// Takes Play button creates new ActionListene
r
Play.addActionListener(new ActionListener(){// Turns the actionl
istener into a variable for usage
public void actionPerformed(ActionEvent e){

dispose();
Game game = new Game();
if(twoPlayer.isSelected()){
Game.AI.isTwoPlayer = true;
}else{
Game.AI.isTwoPlayer = false;
}
game.start();
}
});// Play button
Quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);// Shut down the program
}
});//Quit button
}
}

-------------------------------------------------------------------------------------------------------------

import
import
import
import
import

javax.swing.JFrame;
javax.swing.JCheckBox;
javax.swing.JButton;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;

public class MainMenu extends JFrame{


private static final long serialVersionUID = 1L;
int screenWidth = 400;
int screenHeight = 160;
int buttonWidth = 100;
int buttonHeight = 40;
JButton Play, Quit;
JCheckBox twoPlayer;
public MainMenu(){
getContentPane().setLayout(null);
//Tpus meghvs
addButtons();
addActions();
Play.setBounds((screenWidth - buttonWidth) / 2, 5,buttonWidth ,b

uttonHeight );//Positions the button


Quit.setBounds((screenWidth - buttonWidth) / 2, 50, buttonWidth,
buttonHeight);
twoPlayer.setBounds((screenWidth - buttonWidth) / 2, 95, buttonW
idth * 2,buttonHeight);
/*twoPlayers.setBounds(0, 95, buttonWidth , buttonHeight);
limitFrameRate.setBounds(0, 140, buttonWidth *3, buttonHeight);
*/
// Gomb kszts
getContentPane().add(Play); // Gombot ad a JFrame-hez
getContentPane().add(Quit);
getContentPane().add(twoPlayer);
/*getContentPane().add(twoPlayers);
getContentPane().add(limitFrameRate);*/
//JFrame cuccok
pack();
setVisible(true);
setLocationRelativeTo(null);
setSize(screenWidth, screenHeight);
setTitle("Pong Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
private void addButtons(){
Play = new JButton("Indt");
Quit = new JButton("Kilp");
twoPlayer = new JCheckBox("Kt Jtkos?");
}
private void addActions(){// Takes Play button creates new ActionListene
r
Play.addActionListener(new ActionListener(){// Turns the actionl
istener into a variable for usage
public void actionPerformed(ActionEvent e){
dispose();
Game game = new Game();
if(twoPlayer.isSelected()){
Game.AI.isTwoPlayer = true;
}else{
Game.AI.isTwoPlayer = false;
}
game.start();
}
});// Play button
Quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);// Shut down the program
}
});//Quit button
}

Das könnte Ihnen auch gefallen