| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/10/2008 07:09:58
|
SomethingPurple
Newbie
Joined: 06/10/2008 06:51:33
Messages: 2
Offline
|
OK, so for my final programming assignment I have to make a game that can be played over a network. I basically only need it to work on my computer and my partner's computer. I have written most of the code for alien movements, shooting, dying etc but I am completely stuck on the networking bit. I have looked up tutorials and stuff, but I can't seem to figure out how to apply it to my game. Would anyone be able to give me an idea of what I should be doing? Here's my code, if you have any questions about it ask away (I have attached a pic of what the game basically looks like so you have some visual, (the game screen ends at the white line on the right) Thanks! import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.lang.Math; import java.applet.*; import java.util.*; public class Invaders extends Applet implements Runnable{ //MISC. IMAGES Image p1, p2, p1shot, p2shot, shield1, shield2, divider, nil, abloon; //XY POSITIONS (NOT SHOTS) int p1_x = 184, p2_x = (431+184); int ay[] = new int[4]; int ax[] = new int[5]; //SHOTS int p1shot_x, p1shot_y; boolean p1fired = false; Image ashot[][] = new Image[5][4]; int ashot_y[][] = new int[5][4]; int ashot_x[][] = new int[5][4]; boolean afired[][] = new boolean[5][4]; int randshot[][] = new int[5][4]; //HEIGHTS/WIDTHS int awidth = 46, aheight = 36; int bwidth = 46, bheight = 36; int mwidth = 13, mheight = 19; int swidth = 123; int pwidth = 51, pheight = 25; //ALIENS Image[][] a = new Image[5][4]; boolean[][] adestroy = new boolean[5][4]; boolean aright = true; //ALIEN COUNT int[] clmn = new int[5]; int acount = 20, aspeed = 0; //DYING VARIABLES int p1dieX = 20; int p2dieX = 20; boolean p1die=false, p2die=false; //SHIELD Image[] shield1X = new Image[12]; Image[] shield2X = new Image[12]; boolean sdestroy1 = false, sdestroy2=false; int SX1 = 0, SX2 = 0; //BLOON int bx = -bwidth, randb, bcount; int[] randbshot = new int[6]; Image[] missile = new Image[6]; int[] mx = new int[6]; int[] my = new int[6]; Image qmark; Image[] qmarkX = new Image[2]; boolean bdestroy = true, bright = false; boolean[] bfired = {false, false, false, false, false, false}; //BUFFERING/KEYS int keys[]; private Image dbImage; private Graphics dbg; //TESTING Font font = new Font("monospaced", Font.BOLD, 25); int number1 = 0, number2 = 0; public void init() { for(int x=0;x<5;x++) for(int y=0;y<4;y++) { ax[x] = (25 + 63*x); ay[y] = (73*y+50); adestroy[x][y] = false; afired[x][y] = false; clmn[x] = 4; } setBackground(Color.black); keys = new int[256]; Arrays.fill(keys,0); // 0 = key is up enableEvents(AWTEvent.KEY_EVENT_MASK); p1 = getImage(getDocumentBase(), "p1.PNG"); p2 = getImage(getDocumentBase(), "p2.PNG"); p1shot = getImage(getDocumentBase(), "p1shot.PNG"); p2shot = getImage(getDocumentBase(), "p2shot.PNG"); for(int i=0;i<5;i++) a[i][0] = getImage(getDocumentBase(), "a1.PNG"); for(int i=0;i<5;i++) a[i][1] = getImage(getDocumentBase(), "a2.PNG"); for(int i=0;i<5;i++) a[i][2] = getImage(getDocumentBase(), "a3.PNG"); for(int i=0;i<5;i++) a[i][3] = getImage(getDocumentBase(), "a4.PNG"); for(int i=0;i<5;i++) abloon = getImage(getDocumentBase(), "a5.PNG"); for(int i=0;i<5;i++) ashot[i][0] = getImage(getDocumentBase(), "ashot1.PNG"); for(int i=0;i<5;i++) ashot[i][1] = getImage(getDocumentBase(), "ashot2.PNG"); for(int i=0;i<5;i++) ashot[i][2] = getImage(getDocumentBase(), "ashot3.PNG"); for(int i=0;i<5;i++) ashot[i][3] = getImage(getDocumentBase(), "ashot4.PNG"); for(int i=0;i<6;i++) missile[i] = getImage(getDocumentBase(), "missile.PNG"); //Shield decreases in size when hit shield1X[0] = getImage(getDocumentBase(), "shield1a.PNG"); shield1X[1] = getImage(getDocumentBase(), "shield1b.PNG"); shield1X[2] = getImage(getDocumentBase(), "shield1c.PNG"); shield1X[3] = getImage(getDocumentBase(), "shield1d.PNG"); shield1X[4] = getImage(getDocumentBase(), "shield1e.PNG"); shield1X[5] = getImage(getDocumentBase(), "shield1f.PNG"); shield1X[6] = getImage(getDocumentBase(), "shield1g.PNG"); shield1X[7] = getImage(getDocumentBase(), "shield1h.PNG"); shield1X[8] = getImage(getDocumentBase(), "shield1i.PNG"); shield1X[9] = getImage(getDocumentBase(), "shield1j.PNG"); shield1X[10] = getImage(getDocumentBase(), "shield1k.PNG"); shield1X[11] = getImage(getDocumentBase(), "shield1l.PNG"); shield2X[0] = getImage(getDocumentBase(), "shield2a.PNG"); shield2X[1] = getImage(getDocumentBase(), "shield2b.PNG"); shield2X[2] = getImage(getDocumentBase(), "shield2c.PNG"); shield2X[3] = getImage(getDocumentBase(), "shield2d.PNG"); shield2X[4] = getImage(getDocumentBase(), "shield2e.PNG"); shield2X[5] = getImage(getDocumentBase(), "shield2f.PNG"); shield2X[6] = getImage(getDocumentBase(), "shield2g.PNG"); shield2X[7] = getImage(getDocumentBase(), "shield2h.PNG"); shield2X[8] = getImage(getDocumentBase(), "shield2i.PNG"); shield2X[9] = getImage(getDocumentBase(), "shield2j.PNG"); shield2X[10] = getImage(getDocumentBase(), "shield2k.PNG"); shield2X[11] = getImage(getDocumentBase(), "shield2l.PNG"); qmarkX[0] = getImage(getDocumentBase(), "qmark1.PNG"); qmarkX[1] = getImage(getDocumentBase(), "qmark2.PNG"); divider = getImage(getDocumentBase(), "divider.PNG"); nil = getImage(getDocumentBase(), "nil.PNG"); setForeground(Color.white); } public void processKeyEvent(KeyEvent e) { // the 0xff below binds the key code to below 256 int key = (e.getKeyCode()&0xff); if (e.getID() == KeyEvent.KEY_PRESSED) { keys[key] = 1; // 1 = key is down } else if (e.getID() == KeyEvent.KEY_RELEASED) { keys[key] = 0; // 0 = key is up } repaint(); } public void start() { // define a new thread Thread th = new Thread (this); // start this thread th.start (); } public void stop() {} public void destroy() {} public void run () { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true) { try { // Stop thread for x milliseconds Thread.sleep ( ; } catch (InterruptedException ex) {} if (p1die) p1 = getImage(getDocumentBase(), "p1die.PNG"); else p1 = getImage(getDocumentBase(), "p1.PNG"); if (!p1die) { if (keys[37] == 1) //left { if (p1_x>0) p1_x--; } if (keys[39] == 1) //left { if (p1_x < (430-pwidth)) p1_x++; } if ((keys[32] == 1) && (!p1fired)) //space { p1fired = true; p1shot_x = p1_x + 25; p1shot_y = 588; } } //p1's shot if (p1fired) { p1shot_y-=5; if (p1shot_y<0) p1fired = false; } //left/right boolean for(int i=0;i<5;i++) { if (aright) ax[i]++; else ax[i] --; } //dying counter if (p1dieX >0) p1dieX--; else if (p1dieX == 0) { if (SX1 < 3) SX1 = 0; else SX1-=3; sdestroy1 = false; p1_x = 184; p1die = false; p1dieX = -1; } //aliens collide with right wall if ( ((clmn[4]>0) && (ax[4]>=(430-awidth))) || ((clmn[4]==0) && (clmn[3]>0) && (ax[3]>=(430-awidth))) || ((clmn[4]==0) && (clmn[3]==0) && (clmn[2]>0) && (ax[2]>=(430-awidth))) || ((clmn[4]==0) && (clmn[3]==0) && (clmn[2]==0) && (clmn[1]>0) && (ax[1]>=(430-awidth))) || ((clmn[4]==0) && (clmn[3]==0) && (clmn[2]==0) && (clmn[1]==0) && (clmn[0]>0) && (ax[0]>=(430-awidth))) ) { aright = false; for(int y=0;y<4;y++) ay[y]+=15; } //aliens collide with left wall if ( ((clmn[0]>0) && (ax[0]<0)) || ((clmn[0]==0) && (clmn[1]>0) && (ax[1]<0)) || ((clmn[0]==0) && (clmn[1]==0) && (clmn[2]>0) && (ax[2]<0)) || ((clmn[0]==0) && (clmn[1]==0) && (clmn[2]==0) && (clmn[3]>0) && (ax[3]<0)) || ((clmn[0]==0) && (clmn[1]==0) && (clmn[2]==0) && (clmn[3]==0) && (clmn[4]>0) && (ax[4]<0)) ) { aright = true; for(int y=0;y<4;y++) ay[y]+=15; } //aliens are shot for (int x=0;x<5;x++) for (int y=0;y<4;y++) { //referring to shot if ( ( ((p1shot_x<=ax[x]) && (p1shot_x>=ax[x]+awidth)) || //left x intersects ((p1shot_x+4<=ax[x]+awidth) && (p1shot_x+4>=ax[x])) //right x intersects ) && ( ((p1shot_y >= ay[y]) && (p1shot_y <= ay[y] + aheight)) || //top intersects ((p1shot_y + 11 >= ay[y]) && (p1shot_y <= ay[y] + aheight)) //bottom intersects ) && (!adestroy[x][y]) //the alien isn't already destroyed && (p1fired) ) { p1fired = false; adestroy[x][y] = true; clmn[x]--; acount--; } //aliens shoot //random shot intervals if (acount > 15) randshot[x][y] = (int)(Math.random() * 700); else if (acount > 10) randshot[x][y] = (int)(Math.random() * 500); else if (acount > 5) randshot[x][y] = (int)(Math.random() * 300); else randshot[x][y] = (int)(Math.random() * 100); if ((randshot[x][y] == 1) && (!afired[x][y]) && (!adestroy[x][y])) { afired[x][y] = true; ashot_y[x][y] = ay[y] + aheight; ashot_x[x][y] = ax[x] + awidth/2; } if (afired[x][y]) { ashot_y[x][y]+=4; } if (ashot_y[x][y] > 623) afired[x][y] = false; //player is hit by ashot if ( ( ((ashot_x[x][y]<=p1_x) && (ashot_x[x][y]>=p1_x+pwidth)) || //left x intersects ((ashot_x[x][y]+4<=p1_x+pwidth) && (ashot_x[x][y]+4>=p1_x)) //right x intersects ) && (ashot_y[x][y] + 11 >= 58 && (afired[x][y]) && (!p1die) ) { afired[x][y] = false; p1die = true; p1dieX = 100; } //player is hit by missile for (int i=0;i<6;i++) { if ( ( ((mx[i]<=p1_x) && (mx[i]>=p1_x+pwidth)) || //left x intersects ((mx[i]+mwidth<=p1_x+pwidth) && (mx[i]+mwidth>=p1_x)) //right x intersects ) && (my[i] + mheight >= 58 && (bfired[i]) && (!p1die) ) { p1die = true; p1dieX = 100; bfired[i] = false; } //missile hits shield if ( ( ((mx[i]<=146) && (mx[i]>=146+swidth)) || //left x intersects ((mx[i]+mwidth<=146+swidth) && (mx[i]+mwidth>=146)) //right x intersects ) && (my[i] + mheight >= 537 + 2*SX1) && (bfired[i]) && (!sdestroy1) ) { if (SX1 < 10) SX1+=2; else sdestroy1 = true; bfired[i] = false; } } //alien shot hits shield if ( ( ((ashot_x[x][y]<=146) && (ashot_x[x][y]>=146+swidth)) || //left x intersects ((ashot_x[x][y]+4<=146+swidth) && (ashot_x[x][y]+4>=146)) //right x intersects ) && (ashot_y[x][y] + 11 >= 537 + 2*SX1) && (afired[x][y]) && (!sdestroy1) ) { if (SX1 < 11) SX1++; else sdestroy1 = true; afired[x][y] = false; } //player hits shield if ( ( ((p1shot_x<=146) && (p1shot_x>=146+swidth)) || //left x intersects ((p1shot_x+4<=146+swidth) && (p1shot_x+4>=146)) //right x intersects ) && (p1shot_y <= 537 + 24) && (p1shot_y > 537) && (p1fired) && (!sdestroy1) ) { if (SX1 < 11) SX1++; else sdestroy1 = true; p1fired=false; } shield1 = shield1X[SX1]; } //BLOON if (!bdestroy) { //left/right boolean if (bright) bx+=2; else bx-=2; //bounce off of wall if ((bx+bwidth>=863) && (bcount>0) && (bright)) { bcount--; bright = false; } if ((bx<=0) && (bcount>0) && (!bright)) { bcount--; bright = true; } //destroy if out of bounds if ((bx+bwidth<-3) || (bx>865)) bdestroy = true; //shooting for (int i = bcount*-2+4;i<bcount*-2+6;i++) if ((Math.abs(randbshot[i]-bx)<2) && (!bfired[i])) { bfired[i] = true; mx[i] = bx+18; my[i] = 43; } for (int i=0;i<6;i++) if (bfired[i]) my[i] +=5; } else { for (int i=0;i<6;i++) bfired[i] = false; randb = (int)(Math.random() * 22); // if the number is one, bloon alien will enter if (randb == 1) { bdestroy = false; if (bright) { bx = 863; bright = false; } else { bx = -bwidth; bright = true; } bcount = 2; randb = 0; for (int i=0;i<6;i++) randbshot[i] = (int)(Math.random() * (863-bwidth)); } } repaint(); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void update (Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } public void paint(Graphics g) { g.setFont(font); for (int y=0;y<4;y++) for (int x=0;x<5;x++) { if (!adestroy[x][y]) g.drawImage(a[x][y],(ax[x]), (ay[y]),this); if (afired[x][y]) g.drawImage(ashot[x][y],ashot_x[x][y],ashot_y[x][y],this); } g.drawImage (p1, p1_x, 588, this); g.drawImage (p2, p2_x, 588, this); // g.drawString("", 500, 30); g.drawImage (divider, 430, 0, this); if(!sdestroy1)g.drawImage(shield1, 146,537,this); if(!bdestroy)g.drawImage(abloon,bx,11,this); if (p1fired) g.drawImage (p1shot,p1shot_x,p1shot_y,this); for (int i=0;i<6;i++) if (bfired[i]) g.drawImage(missile[i], mx[i],my[i], this); } }
| Filename |
game.bmp |
Download
|
| Description |
|
| Filesize |
1729 Kbytes
|
| Downloaded: |
52 time(s) |
This message was edited 2 times. Last update was at 06/10/2008 08:33:41
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/10/2008 08:42:58
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Realistically you should have a server process and some number of client processes. The client process sends what the user is doing with, for example, the keyboard. Internally the server process is really what knows where things (like a character and a bomb) are and updates the clients with any changes. The client just renders whatever it is told to by the server.
But changing the code you have to do this will be, um, fun. Because you're using applets you can only communicate, in effect, one way. The applet can initiate communication back to the server where it came from but it cannot act as a server. So one applet on one machine cannot talk directly to another applet on another machine. They must go through a server. This will require a good deal more code to deal with the server part of the equation.
Do you have to run as an applet? If you were a standalone application you could at least have one application be the server and the other the client.
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 06/11/2008 06:32:34
|
SomethingPurple
Newbie
Joined: 06/10/2008 06:51:33
Messages: 2
Offline
|
I only made it an applet because that's the format the game that I had to make last year was in. So yeah, it doesn't have to be an applet, I just knew how to work with them better.
If I were to convert this to an application, what major changes would have to be made? (Specifically how would I draw the images?)
|
|
|
 |
|
|
|
|