Mitt första spel i Java
Hej här är mitt första spel kodat i Java. Det är en liten simpel Pong-klon som jag mest kodat för att ge en grunderfarenhet i hur spel funkar. Denna ska byggas på för att bli ett 2-spelar spel.
Jag tar gärna emot konstruktiv feedback.
Screen:
Kod:
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
@SuppressWarnings("serial")
public class Flong extends Applet implements Runnable,MouseMotionListener{
final int FPS = 60,HALFHEIGHT = 35;
int goalcheck=0;
Graphics buffer;
Image offscreen;
Dimension dim;
Thread th;
Pad player,opponent;
Ball ball;
public void init(){
player = new Pad();
opponent = new Pad();
ball = new Ball();
opponent.x=589;
setSize(600,360);
dim = getSize();
offscreen = createImage(dim.width,dim.height);
buffer = offscreen.getGraphics();
addMouseMotionListener(this);
th = new Thread(this);
th.start();
}
public void paint(Graphics g){
buffer.clearRect(0, 0, dim.width, dim.height);
buffer.drawString(""+player.points, 280, 30);
buffer.drawString(""+opponent.points, 300, 30);
buffer.drawString("-", 292, 30);
buffer.drawRoundRect(player.x, player.y-HALFHEIGHT, player.width, player.height, 6, 6);
buffer.drawRoundRect(opponent.x, opponent.y-HALFHEIGHT, opponent.width, opponent.height, 6, 6);
buffer.drawOval((int)ball.x-ball.radius, (int)ball.y-ball.radius, ball.radius*2, ball.radius*2);
buffer.drawLine(player.x+5, 0, player.x+5, 360);
buffer.drawLine(opponent.x+5, 0, opponent.x+5, 360);
buffer.drawLine(0, 0, 600, 0);
buffer.drawLine(0, 359, 600, 359);
g.drawImage(offscreen,0,0,this);
}
public void update(Graphics g){
paint(g);
}
public void run() {
while (true){
ball.wallbounce();
goalcheck=ball.padbounce(player.y, opponent.y);
if (goalcheck==2)
player.points++;
if (goalcheck==1)
opponent.points++;
ball.move();
opponent.ai(ball.y);
repaint();
try {Thread.sleep(1000/FPS);}
catch (InterruptedException ie){};
}
}
public void mouseDragged(MouseEvent me) {
player.y=me.getY();
}
public void mouseMoved(MouseEvent me) {
player.y=me.getY();
}
}
public class Pad {
int y=0,x=0,height=70,width=10;
int points=0;
double vy=0;
public Pad(){
}
public void ai(double bally){
if (bally>y){
vy=Math.sqrt(bally-y);
if (vy>3)
vy=3;
}
else {
vy=-Math.sqrt(Math.abs(y-bally));
if (vy<-3)
vy=-3;
}
y+=vy;
}
}
public class Ball {
double x=300,y=150,vx=-4,vy=0;
final int HALFHEIGHT=35,WIDTH=10;
int radius=15;
public Ball(){
}
public void resetposition(){
vy=0;
x=300;
y=150;
}
public void move(){
x+=vx;
y+=vy;
}
public void wallbounce(){
if (y<0+radius){
if (vy<0)
vy=-vy;
}
if (y>360-radius){
if (vy>0)
vy=-vy;
}
}
public int padbounce(int playery,int opponenty){
if (x-radius-WIDTH<0&&y>(playery-HALFHEIGHT)&&y<(playery+HALFHEIGHT)){
vx=-vx*1.01;
if (y>playery){
vy=Math.sqrt(Math.abs(y-playery));
vy/=1.5;
}
if (y<playery){
vy=-Math.sqrt(Math.abs(y-playery));
vy/=1.5;
}
}
if (x+radius+WIDTH>595&&y>(opponenty-HALFHEIGHT)&&y<(opponenty+HALFHEIGHT)){
vx=-vx*1.01;
if (y>opponenty){
vy=Math.sqrt(Math.abs(y-opponenty));
vy/=1.5;
}
if (y<opponenty){
vy=-Math.sqrt(Math.abs(y-opponenty));
vy/=1.5;
}
}
if (x-radius<5||x+radius>595){
if (x-radius<5){
vx=4;
resetposition();
return 1;
}
else {
vx=-4;
resetposition();
return 2;
}
}
return 0;
}
}