Sign in to follow this  
danpc

New to the world of game programming

Recommended Posts

danpc    122
hello all i've recently taking on the learning of java game programming and was learning quite alot and getting into the swing of things quite quickly (or quick enough for me). i've taken on the basics of programming some simple "old school" games in java and was wondering if anyone could look at my coding for two games (pong and space invaders both currently incomplete but with the skeleton or template for near completion) and make any suggestions, recommendations or alterations to the coding to improve them in any possible manor (or tell me that i've gone about them in completely the wrong way and help me out a little).... too be completely honest im not 100% on objects and am pretty sure ive used them wrong. thanks in advance. ---------------------------------------------------------------------------------- import java.applet.*; import java.awt.*; public class SpaceInvaders extends Applet implements Runnable { Image offscreenImage; Graphics offscr; Thread t; int width, height, startAngle, arcAngle; int counter = 0; private Font aFont; public void init() { width = size().width; height = size().height; aFont = new Font("Helvetica", Font.BOLD, 25); offscreenImage = createImage(width, height); } public void run() { while(true) { repaint(); alienMove(); try { t = new Thread(this); t.start(); t.sleep(10); } catch (InterruptedException e) {;} } } public void paint(Graphics g) { offscr.setColor(Color.black); offscr.fillRect(0, 0, width, height); offscr.setFont(aFont); offscr.setColor(Color.white); offscr.fillArc(0,0,width,height,startAngle,arcAngle); g.drawImage(offscreenImage, 0, 0, this); } public void update(Graphics g) { paint(g); } public boolean mouseMove(Event e, int x, int y) { return true; } public void alienMove(){ cityACollision(); wallACollision(); wallPCollision(); wallBCollision(); bulletCCollision(); bulletACollision(); allDead(); isAlive(); playerHit(); } public void cityACollision() { // if aliens coords match city coords kill blocks return; } public void isAlive() { // loop through all objects // if alive draw return; } public void wallACollision() { // if alien hits wall and is not dead reverse all alien // direction and step down } public void wallPCollision() { // if ship hits wall place on edge } public void wallBCollision() { // if bullet hits either X Boundary // kill } public void bulletCCollision(){ //if bullet hits city kill building block return; } public void bulletACollision(){ // if player bullet hits alien - kill // return; } public void playerHit(){ //if abullet or alien coords match players // then game over return; } public void allDead(){ //if all aliens are dead // then win return; } public boolean mouseClicked(Event e, int x, int y) { // if there is no current bullet fired by player // create bullet moving upwards and make fired true // pFire return true; } class AlienShip { int alienX, alienY; boolean aAlive = true; boolean front; public void AlienShip(){ AlienShip aliens[][] = new AlienShip[10][5]; // 50 aliens in rectangle 10 by 5 for (int x =0; 0<=9; x++) { aliens[x][4].front = true; // front aliens are tagged for firing } } }; class BuildingBlock { int blockX, blockY, sizeX, sizeY; boolean bAlive; class City { int cityX, cityY; BuildingBlock blocks[][] = new BuildingBlock[5][3]; //city is made of 15 blocks public void City(){ City cities[] = new City[4]; //4 cities } }; class aFire { int bulletX, bulletY, sizeX, sizeY; int diry = 2; boolean aAlive = true; public void aFire(){ // alien fire aFire aBullet = new aFire(); } }; class pFire { int bulletX, bulletY, sizeX, sizeY; int diry = -2; boolean aAlive = true; public void pFire(){ if (counter == 0) { pFire pBullet = new pFire(); //check to see if there is currently a player bullet in action //if not, create one counter++; } } }; } } -------------------------------------------------------------------------------- /******************************************************** * * * * * will making the puck, paddles, goals and walls * * objects make the game easier to extend? * * if i start 2 threads the speed is increased, * * helpfull? * * how do i make puck movement and collisions events, * * that are initialised by the corresponding action? * * would objects make this easier? * * * ********************************************************/ import java.applet.*; import java.awt.*; public class AirHockey extends Applet implements Runnable { Image offscreenImage; Graphics offscr; Graphics start; Thread t; int begun = 0; int pucky = 243; int puckx = 443; int p1=0; int p2=0; int right = 3; int up = -3; int down = 3; int left = -3; int diry = up; int dirx = right; boolean hittopgr = false; //hit top? going right. boolean hitbottomgr = true; boolean hittopgl = true; boolean hitbottomgl = true; int paddle1y, paddle2y, width, height, startAngle, arcAngle; private Font aFont; public void init() { width = size().width; height = size().height; paddle1y = size().height/2 -35; paddle2y = size().height/2 -35; aFont = new Font("Helvetica", Font.BOLD, 25); offscreenImage = createImage(width, height); } public void run() { while(true) { repaint(); puckMove(); try { t.sleep(10); } catch (InterruptedException e) {;} } } public void paint(Graphics g) { if (paddle1y < 0){paddle1y=0;} if (paddle1y >size().height-70){paddle1y=size().height-70;} if (paddle2y < 0){paddle2y=0;} if (paddle2y >size().height-70){paddle2y=size().height-70;} offscr.setColor(Color.white); offscr.fillRect(0, 0, width, height); offscr.setFont(aFont); offscr.setColor(Color.black); offscr.fillArc(0,0,width,height,startAngle,arcAngle); offscr.drawLine(450,0,450,500); offscr.drawLine(0,0,0,500); offscr.drawLine(0,0,900,0); offscr.drawLine(0,499,899,499); offscr.drawLine(899,0,899,499); offscr.drawString("Score "+p1 +" : " +p2,10,20); offscr.drawOval(puckx, pucky,15,15); offscr.fillOval(puckx, pucky,15,15); offscr.drawRect(20,paddle1y,10,70); offscr.fillRect(20,paddle1y,10,70); offscr.drawRect(size().width-40,paddle2y,10,70); offscr.fillRect(size().width-40,paddle2y,10,70); g.drawImage(offscreenImage, 0, 0, this); } public void update(Graphics g) { paint(g); } public boolean mouseMove(Event e, int x, int y) { paddle2y = y; return true; } public boolean keyDown(Event e, int key) { if (key == 1004){ paddle1y = paddle1y-35; } if (key == 1005){ paddle1y = paddle1y+35; } return true; } public boolean keyUp(Event e, int key) { return true; } public void puckMove(){ puckx = puckx + dirx; pucky = pucky + diry; puckScore(); wallCollision(); paddleCollision(); } public void puckScore() { if (puckx > 884) { p1++; try { t.sleep(1000); reset(); } catch (InterruptedException e) {;} } if (puckx < 1) { p2++; try { t.sleep(1000); reset(); } catch (InterruptedException e) {;} } return; } public void reset(){ puckx = 443; pucky = 243; hittopgr = false; hitbottomgr = true; hittopgl = true; hitbottomgl = true; } public void wallCollision() { if (pucky < 1){ diry = down; } if (pucky > 484){ diry = up; } return; } public void paddleCollision(){ if(pucky <= paddle1y+70 && pucky >= paddle1y-5 && puckx <=30 && puckx >=29){ dirx = right; } if(pucky <= paddle2y+70 && pucky >= paddle2y-5 && puckx <=size().width-55 && puckx >=size().width-56){ dirx = left; } return; } public boolean mouseDown(Event e, int x, int y) { if (x > 0 && begun == 0) { begun++; offscr = offscreenImage.getGraphics(); t = new Thread(this); t.start(); } return true; } } --------------------------------------------------------------------------------

Share this post


Link to post
Share on other sites
Angex    884
Well I had a quick scan through your code and noticed a couple of problems with the way you're handling threads and double buffering.

I think some of the sections from this tutorial will help you out.

But to breifly describe some issues; you shouldn't hold onto a reference for the back buffers Graphics object, just get it when needed at the start of paint().


public void paint(final Graphics g) {

if (offscreenImage == null) {
// Error.
return;
}

Graphics2D offscr = (Graphics2D) offscreenImage.getGraphics();

offscr.setColor(Color.black);
offscr.fillRect(0, 0, width, height);
offscr.setFont(aFont);

offscr.setColor(Color.white);
offscr.fillArc(0,0,width,height,startAngle,arcAngle);

g.drawImage(offscreenImage, 0, 0, this);

offscr.dispose();
}








Also you don't need to be continually creating and starting threads, just do this once.



public void init() {
// Other initialisation...

t = new Thread(this);
t.start();
}

public void run() {
while (true) {

repaint();

alienMove();

try {
Thread.sleep(10);

} catch (InterruptedException e) {;}
}
}






Also in the future you may want to think about updating your technique for collisions, imagine if you had 100's of walls and bullets to check. You don't really want to write a method for every single one :)

Maybe you can make use of an entity class, then iterate through all your entities and check whether their boundaries are overlapping.

Don't be too down-hearted about these comments, you've made a good start.

[EDIT: That sounds really bad, reading it back :) ]

Share this post


Link to post
Share on other sites
danpc    122
hey, thanks for the quick reply



i'll have a look through the turtorial you suggested thanks, the second problem you pointed out completely slipped passed me (though as soon as you mentioned it i knew i must have put the thread bits in the wrong place).

thanks again

Share this post


Link to post
Share on other sites
OrangyTang    1298
You shouldn't be doing double buffering manually, instead use the BufferStrategy class. The nice thing about this is that it'll do a proper pointer flip if possible which avoid tearing and is faster, but if not possible it'll fall back to doing a copy (like you're already doing). Either way your code will look identical.

Share this post


Link to post
Share on other sites
danpc    122
hey guys, first of all thanks to everyone that replied, i've taken them in to consideration and will more than likelybegin implementing them as i begin to learn them.

secondly i beleive ive almost finished the space invaders type game and was wondering if someone could take a look at it and point out any major flaws or problems, there's one biggie i noticed and that is it isnt drawing anything, if starred for hours at it changing code here and there but can't nail the problem..


cheers




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

import java.applet.*;
import java.awt.*;
import java.util.*;

public class SpaceInvaders extends Applet implements Runnable
{

Image offscreenImage;
Graphics offscr;
Thread t;
int width, height, startAngle, arcAngle, dir;
int counter, pFCounter, aMovC, aFCounter = 0;
int mul = 1;
boolean fired = false;
int playerY, playerX, randAX, randAY;
Random generator = new Random();
AlienShip aliens[][] = new AlienShip[10][5];
// 50 aliens in rectangle 10 by 5
City cities[] = new City[4];
//4 cities
pFire pBullet = new pFire();
aFire aBullet = new aFire();
private Font aFont;

public void init() {

width = size().width;
height = size().height;
playerX = width / 2;
playerY = height - 20;
dir = width /90;
aFont = new Font("Helvetica", Font.BOLD, 25);

offscreenImage = createImage(width, height);
offscr = offscreenImage.getGraphics();

t = new Thread(this);
t.start();
}

public void run() {
while(true)
{
alienMove();
repaint();
try {
t.sleep(10);

}
catch (InterruptedException e) {;}
}
}

public void paint(Graphics g) {
offscr.setColor(Color.white);
offscr.fillRect(0, 0, width, height);
offscr.setFont(aFont);


offscr.setColor(Color.black);
g.drawImage(offscreenImage, 0, 0, this);
}

public void update(Graphics g)
{
paint(g);
}

public void alienMove(){
isAlive();
cityACollision();
wallACollision();
wallPCollision();
wallBCollision();
bulletCCollision();
bulletACollision();
allDead();
playerHit();

if (pFCounter != 0)
{
pBullet.bulletY =+ pBullet.diry;
}

if (aFCounter != 0)
{
aBullet.bulletY =+ aBullet.diry;
}

if (aMovC ==20){
if (aFCounter < 5)
{
//pick random front row to fire.
// for now just use any alive alien
while (fired == false){
randAX = generator.nextInt(10);
randAY = generator.nextInt(5);
if (aliens[randAX][randAY].aAlive == true){
aFire aBullet = new aFire();
fired =true;
}

}
fired = false;
}
aMovC =0;
}
for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{

aliens[x][y].alienX =+ mul;
aMovC ++;
}
}





}

public void cityACollision() {

for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{
for (int z=0; z<4; z++){
for (int b=0; b<5; b++){
for (int c=0; c<3; c++){
if (aliens[x][y].aAlive == true && (aliens[x][y].alienX == cities[z].blocks[b][c].blockX && aliens[x][y].alienY == cities[z].blocks[b][c].blockY))
{
cities[z].blocks[b][c].bAlive = false;
}
}
}
}
}
}
// if aliens coords match city coords kill blocks
return;
}

public void isAlive() {
offscr.fillRect(playerX,playerY,30,15);
counter = 0;

for (int z=0; z<4; z++){
for (int b=0; b<5; b++){
for (int c=0; c<3; c++){
if (cities[z].blocks[b][c].bAlive ==true){
offscr.fillRect(cities[z].blocks[b][c].blockX, cities[z].blocks[b][c].blockY, cities[z].blocks[b][c].sizeX, cities[z].blocks[b][c].sizeY);
}}}}



if (pFCounter != 0)
{
offscr.fillRect(pBullet.bulletX, pBullet.bulletY, pBullet.sizeX, pBullet.sizeY);
}

if (aFCounter!= 0)
{
offscr.fillRect(aBullet.bulletX, aBullet.bulletY, aBullet.sizeX, aBullet.sizeY);
}

for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{
if (aliens[x][y].aAlive == true)
{

offscr.fillRect(aliens[x][y].alienX,aliens[x][y].alienY,aliens[x][y].aSizeX,aliens[x][y].aSizeY);


}
if (aliens[x][y].aAlive == false)
{
//speeds of aliens
counter++;
if (counter > 10 && counter < 25){
mul =2 * dir;
}
else if (counter > 25 && counter < 40){
mul =4 * dir;
}
else if (counter > 40 && counter < 48){
mul =7 * dir;
}
else if (counter > 48 && counter < 50){
mul =10 * dir;
}
else {
mul =1 * dir;
}
}
}
}


// loop through all objects
// if alive draw
return;
}


public void wallACollision() {
for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{
if ((aliens[x][y].alienX > width || aliens[x][y].alienX < 0 ) && aliens[x][y].aAlive == true)
{
for (int z= 0; z<5; z++){
for (int c= 0; c<10; c++){
dir =dir * -1;
aliens[z][c].alienY =+ (height /50);}
}

}

// if alien hits wall and is not dead reverse all alien on line
// direction and step down
}
}
}

public void wallPCollision() {
if(playerX >= width -29)
{
playerX = width -30;
}
else if(playerX <= 0)
{
playerX = 0;
}
}

public void wallBCollision() {
if (pBullet.bulletY <= 0)
{
pBullet.pfAlive = false;
pFCounter--;
}
if (aBullet.bulletY >= height)
{
aBullet.afAlive = false;
aFCounter--;
}

// if bullet hits either X Boundary
// kill
}


public void bulletCCollision(){
for (int z=0; z<4; z++){
for (int b=0; b<5; b++){
for (int c=0; c<3; c++){
if (pBullet.bulletX == cities[z].blocks[b][c].blockX && pBullet.bulletY == cities[z].blocks[b][c].blockY)
{
cities[z].blocks[b][c].bAlive = false;
pBullet.pfAlive=false;
pFCounter--;
}
}
}
}
for (int z=0; z<4; z++){
for (int b=0; b<5; b++){
for (int c=0; c<3; c++){
if (aBullet.bulletX == cities[z].blocks[b][c].blockX && aBullet.bulletY == cities[z].blocks[b][c].blockY)
{
cities[z].blocks[b][c].bAlive = false;
aBullet.afAlive=false;
aFCounter--;
}
}
}
}

//if bullet hits city kill building block
return;
}

public void bulletACollision(){
for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{
if (pBullet.bulletX == aliens[x][y].alienX && pBullet.bulletY == aliens[x][y].alienY)
{
pBullet.pfAlive=false;
pFCounter--;
aliens[x][y].aAlive =false;
}
}
}

// if player bullet hits alien - kill
//
return;
}

public void playerHit(){
for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{
if (aliens[x][y].aAlive == true && (aliens[x][y].alienX == playerX && aliens[x][y].alienY == playerY))
{
//end game.
}
}
}

if (aBullet.afAlive ==true && ((aBullet.bulletX >= playerX && aBullet.bulletX <= playerX+10) && (aBullet.bulletY >= playerY )))
//if abullet or alien coords match players
// then game over
return;
}

public void allDead(){
counter = 0;
for (int x =0; x<10; x++)
{
for (int y=0; y<5; y++)
{
if (aliens[x][y].aAlive == false)
{
//speeds of aliens
counter++;
}
}
}
if (counter == 50)
{
//end
}
//if all aliens are dead
// then win
return;
}

public boolean mouseClicked(Event e, int x, int y)
{
if (pFCounter == 0)
{
pFire pBullet = new pFire();
pBullet.bulletX = x;
pBullet.bulletY = playerY;
//check to see if there is currently a player bullet in action
//if not, create one
pFCounter++;
}

// if there is no current bullet fired by player
// create bullet moving upwards and make fired true
// pFire
return true;
}

public boolean mouseMove(Event e, int x, int y)
{
playerY = y;

return true;
}

class AlienShip
{
int alienX, alienY;
int aSizeX, aSizeY = 7;

boolean aAlive = true;
boolean front;

public void AlienShip(){
int extX = 0;
int y=0;


for (int x=0; x<=9; x++)
{
aliens[x][4].front = true;
// front aliens are tagged for firing
}

for (int x=0; x<10; x++)
{
int extY=0;
aliens[x][y].alienX = extX;

for (y=0; y<5; y++)
{
aliens[x][y].alienY = 20 + extY;
extY =+20;
if (y==4)
{
extX =+10;
}
}
}
}
};

class City
{
int cityX, cityY;
BuildingBlock blocks[][] = new BuildingBlock[5][3];
//city is made of 15 blocks

class BuildingBlock
{
int blockX, blockY, sizeX, sizeY;
boolean bAlive;

public void City(){
int by=0;
int extX =0;
int extBX=0;
int extBY=0;
for (int x=0; x<4; x++){

cities[x].cityX =50 +extX;
for (int bx=0; bx<5; bx++){
extBY=0;
cities[x].blocks[bx][by].blockX = 50+extBX;
for ( by=0; by<3; by++){
cities[x].blocks[bx][by].blockY = 50+extBY;
extBY+=sizeY;
}
extBX+=sizeX;
}
extX +=100;
}
}
}};

class aFire
{
int bulletX, bulletY, sizeX, sizeY;
int diry = 2;
boolean afAlive = true;
public void aFire(){

// alien fire
}
};

class pFire
{
int bulletX, bulletY, sizeX, sizeY;
int diry = -2;
boolean pfAlive = true;
public void pFire(){

}
};
}


================================================================================

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this