Академический Документы
Профессиональный Документы
Культура Документы
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
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;
}
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JCheckBox;
javax.swing.JButton;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
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;