943,809 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4311
  • Java RSS
Aug 18th, 2009
0

Game in java source code

Expand Post »
Greetings,

Hmm, Im not sure Is this the right place to ask this question, Well If im wrong please correct me.

Im working in a company as Java programmer that also provide professional training for Java and two of my trainees have created "Stick Snake" and "Snake and ladders" game in java (these 2 games for now, Many more to come).

And, I want to share that code with newbies so they can read and understand the code and build up their logic tech. I found Daniweb the best place for it.

So what i want to know is, How can i share those codes ? You cant say those codes are fully optimized or best codes as codes are written by trainees and even i haven't checked them, I have just seen games in working condition.

So please let know, Should i post them in "Code Snippets" area or just provide links of files here ?

Regards
Similar Threads
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Aug 18th, 2009
0

Re: Game in java source code

it's better to provide links than publishing in code sniplets,but it's an IT discussion community may everyone have the knowledge of these games can't you provide much higher
Reputation Points: 10
Solved Threads: 2
Newbie Poster
prisonpassioner is offline Offline
20 posts
since Aug 2009
Aug 18th, 2009
0

Re: Game in java source code

yep, i'd say the code snippets would be the place to put them. It's probably a good idea to give some a good description too. If you are feeling particularly motivated you could make it into a tutorial. I see the Java Tutorial section is currently empty.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Aug 21st, 2009
0

Re: Game in java source code

Hello again,

Sorry for late reply, I was out of town.

Ok, I will be posting 3 programs in few time.
1 - Snake
2 - Snake and ladders
3 - Web browser


Also, I have another question to ask.

I have just released my first open source project. Its an Object Relational Mapping Framework for java application.

I just made a start of it. I have future plans for it but before proceeding i want to make sure, im on the right way or not. Also, Im new to sourceforge.net too, So i need little help on it.

It would be the best if anyone can try my framework and check it.

Waiting for your suggestions.

Regards,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 18th, 2009
-1

Hi Mr. Puneetkay

Hello.. I am Mariz=)

i had read your posts .. and i'm interested with your code..

here is my email SNIP

hope we can talk about it.. i just need a simple java game for my defence if you don't mind...

i am a 2ndyr college from Phil.

Thanks in advance...=)
Last edited by happygeek; Sep 18th, 2009 at 5:35 am. Reason: keep it onsite
Reputation Points: 3
Solved Threads: 0
Newbie Poster
aizteru05 is offline Offline
1 posts
since Sep 2009
Sep 18th, 2009
0

Re: Game in java source code

I am also interested in the code =).

Please do share it =)!

Thank you in advance.
Reputation Points: 12
Solved Threads: 4
Light Poster
roswell67 is offline Offline
32 posts
since Sep 2009
Jul 12th, 2010
0
Re: Game in java source code
Java Syntax (Toggle Plain Text)
  1. import javax.swing.JFrame;
  2.  
  3. public class snake extends JFrame {
  4.  
  5. public snake() {
  6.  
  7. add(new Board());
  8.  
  9. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. setSize(320, 340);
  11. setLocationRelativeTo(null);
  12. setTitle("Snake");
  13.  
  14. setResizable(false);
  15. setVisible(true);
  16. }
  17.  
  18. public static void main(String[] args) {
  19. new Snake();
  20. }
  21. }
  22. This is Board.java file:
  23.  
  24. import java.awt.Color;
  25. import java.awt.Font;
  26. import java.awt.FontMetrics;
  27. import java.awt.Graphics;
  28. import java.awt.Image;
  29. import java.awt.Toolkit;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.awt.event.KeyAdapter;
  33. import java.awt.event.KeyEvent;
  34.  
  35. import javax.swing.ImageIcon;
  36. import javax.swing.JPanel;
  37. import javax.swing.Timer;
  38.  
  39.  
  40. public class Board extends JPanel implements ActionListener {
  41.  
  42. private final int WIDTH = 300;
  43. private final int HEIGHT = 300;
  44. private final int DOT_SIZE = 10;
  45. private final int ALL_DOTS = 900;
  46. private final int RAND_POS = 29;
  47. private final int DELAY = 140;
  48.  
  49. private int x[] = new int[ALL_DOTS];
  50. private int y[] = new int[ALL_DOTS];
  51.  
  52. private int dots;
  53. private int apple_x;
  54. private int apple_y;
  55.  
  56. private boolean left = false;
  57. private boolean right = true;
  58. private boolean up = false;
  59. private boolean down = false;
  60. private boolean inGame = true;
  61.  
  62. private Timer timer;
  63. private Image ball;
  64. private Image apple;
  65. private Image head;
  66.  
  67.  
  68. public Board() {
  69. addKeyListener(new TAdapter());
  70.  
  71. setBackground(Color.black);
  72.  
  73. ImageIcon iid = new ImageIcon(this.getClass().getResource("dot.png"));
  74. ball = iid.getImage();
  75.  
  76. ImageIcon iia = new ImageIcon(this.getClass().getResource("apple.png"));
  77. apple = iia.getImage();
  78.  
  79. ImageIcon iih = new ImageIcon(this.getClass().getResource("head.png"));
  80. head = iih.getImage();
  81.  
  82. setFocusable(true);
  83. initGame();
  84. }
  85.  
  86.  
  87. public void initGame() {
  88.  
  89. dots = 3;
  90.  
  91. for (int z = 0; z < dots; z++) {
  92. x[z] = 50 - z*10;
  93. y[z] = 50;
  94. }
  95.  
  96. locateApple();
  97.  
  98. timer = new Timer(DELAY, this);
  99. timer.start();
  100. }
  101.  
  102.  
  103. public void paint(Graphics g) {
  104. super.paint(g);
  105.  
  106. if (inGame) {
  107.  
  108. g.drawImage(apple, apple_x, apple_y, this);
  109.  
  110. for (int z = 0; z < dots; z++) {
  111. if (z == 0)
  112. g.drawImage(head, x[z], y[z], this);
  113. else g.drawImage(ball, x[z], y[z], this);
  114. }
  115.  
  116. Toolkit.getDefaultToolkit().sync();
  117. g.dispose();
  118.  
  119. } else {
  120. gameOver(g);
  121. }
  122. }
  123.  
  124.  
  125. public void gameOver(Graphics g) {
  126. String msg = "Game Over";
  127. Font small = new Font("Helvetica", Font.BOLD, 14);
  128. FontMetrics metr = this.getFontMetrics(small);
  129.  
  130. g.setColor(Color.white);
  131. g.setFont(small);
  132. g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2,
  133. HEIGHT / 2);
  134. }
  135. public void checkApple() {
  136.  
  137. if ((x[0] == apple_x) && (y[0] == apple_y)) {
  138. dots++;
  139. locateApple();
  140. }
  141. }
  142.  
  143.  
  144. public void move() {
  145.  
  146. for (int z = dots; z > 0; z--) {
  147. x[z] = x[(z - 1)];
  148. y[z] = y[(z - 1)];
  149. }
  150.  
  151. if (left) {
  152. x[0] -= DOT_SIZE;
  153. }
  154.  
  155. if (right) {
  156. x[0] += DOT_SIZE;
  157. }
  158.  
  159. if (up) {
  160. y[0] -= DOT_SIZE;
  161. }
  162.  
  163. if (down) {
  164. y[0] += DOT_SIZE;
  165. }
  166. }
  167.  
  168.  
  169. public void checkCollision() {
  170.  
  171. for (int z = dots; z > 0; z--) {
  172.  
  173. if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  174. inGame = false;
  175. }
  176. }
  177.  
  178. if (y[0] > HEIGHT) {
  179. inGame = false;
  180. }
  181.  
  182. if (y[0] < 0) {
  183. inGame = false;
  184. }
  185.  
  186. if (x[0] > WIDTH) {
  187. inGame = false;
  188. }
  189.  
  190. if (x[0] < 0) {
  191. inGame = false;
  192. }
  193. }
  194.  
  195. public void locateApple() {
  196. int r = (int) (Math.random() * RAND_POS);
  197. apple_x = ((r * DOT_SIZE));
  198. r = (int) (Math.random() * RAND_POS);
  199. apple_y = ((r * DOT_SIZE));
  200. }
  201.  
  202. public void actionPerformed(ActionEvent e) {
  203.  
  204. if (inGame) {
  205. checkApple();
  206. checkCollision();
  207. move();
  208. }
  209.  
  210. repaint();
  211. }
  212.  
  213.  
  214. private class TAdapter extends KeyAdapter {
  215.  
  216. public void keyPressed(KeyEvent e) {
  217.  
  218. int key = e.getKeyCode();
  219.  
  220. if ((key == KeyEvent.VK_LEFT) && (!right)) {
  221. left = true;
  222. up = false;
  223. down = false;
  224. }
  225.  
  226. if ((key == KeyEvent.VK_RIGHT) && (!left)) {
  227. right = true;
  228. up = false;
  229. down = false;
  230. }
  231.  
  232. if ((key == KeyEvent.VK_UP) && (!down)) {
  233. up = true;
  234. right = false;
  235. left = false;
  236. }
  237.  
  238. if ((key == KeyEvent.VK_DOWN) && (!up)) {
  239. down = true;
  240. right = false;
  241. left = false;
  242. }
  243. }
  244. }
  245. }
  246.  
  247. }
Last edited by peter_budo; Oct 6th, 2011 at 2:43 am. Reason: Adding code tags to old post
Reputation Points: 10
Solved Threads: 0
Newbie Poster
raviteja.k@vimu is offline Offline
1 posts
since Jul 2010
Jul 12th, 2010
0
Re: Game in java source code
that is pretty small code for a Java game, but it is simple . You might want to include the images, because it is completely useless unless people have the images, unless someone can make it themselves . Either ways, I am not here to use the code, but I am making a custom gaming engine (2d turn based) in Java and I was interested in your game, just out of curiosity > Good luck on your next project ! Oh, btw if you need any help on Java game programming, just add me on MSN at :

[email removed]
Last edited by Ezzaral; Feb 18th, 2011 at 2:04 pm. Reason: Snipped email. Please keep it on site.
Reputation Points: 21
Solved Threads: 9
Junior Poster
WargRider is offline Offline
102 posts
since Feb 2010
Feb 17th, 2011
0

I Need the code

Can you please forward the code for snake and ladder to me. However, is it playable by two players? and does it allow network gaming using TCP Socket.

Just send it to me please. THE SNAKE AND LADDER
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dawenlomo is offline Offline
1 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC