Snake.java

Discussions about all aspects of technology, what you're doing with it and exchange of ideas and solutions to problems you might be having.

Snake.java

Postby Gh0st.IRE on Mon Apr 08, 2013 9:27 pm

I was follow a simple tutorial to learn the basics of a Java game and found this little tutorial/example
http://zetcode.com/tutorials/javagamestutorial/snake/

So after writing all out myself so I understood what was happening I found that my keyboard arrows (i.e. movement) were not working and I'm not sure why. Can any of you more advanced users help me?

Code: Select all
//The relevant variables
private boolean left = false;
    private boolean right = true;
    private boolean up = false;
    private boolean down = false;
    private boolean inGame = true;

public void move() {
         
          //Key game Algorithim
          //z = Dots i.e. Snake size
          //The user moves the snakes head and each dot then moves one place infront of where it used to be. [x(z-1)]
          for (int z = dots; z > 0; z--){
              x[z] = x[(z - 1)];
              y[z] = y[(z - 1)];
          }
         
          //Move left
          if (left) {
              x[0] -= DOT_SIZE;
          }
         
          //Move right
          if(right){
              x[0] += DOT_SIZE;
          }
         
          //Move up
          if(up){
              y[0] -= DOT_SIZE;
          }
         
          //Move down
          if(down){
              y[0] += DOT_SIZE;
          }
      }

 private class TAdapter extends KeyAdapter{
          //Currently not working
          public void keypressed(KeyEvent e) {
              int key = e.getKeyCode();
             
              if((key == KeyEvent.VK_LEFT) && (!right)){
                  left = true;
                  up = false;
                  down = false;
              }
             
              if((key == KeyEvent.VK_RIGHT) && (!left)){
                  right = true;
                  up = false;
                  down = false;
              }
             
              if((key == KeyEvent.VK_UP) && (!down)){
                  up = true;
                  right = false;
                  left = false;
             }
              if ((key == KeyEvent.VK_DOWN) && (!up)) {
                down = true;
                right = false;
                left = false;
              }
          }
      }
}



That's a short copy and paste from the code. The entire thing is available on the above link.
Ex-TF2 Admin. Forum Moderator. Add me on Steam or PM me here!
Image
Heresy MMO Guild Member.
User avatar
Gh0st.IRE
Moderator
 
Player: Rear Naked Bugs
STEAM_0:0:12137987
 
Posts: 4635
Joined: Tue Dec 16, 2008 3:09 pm
Location: Dublin, Ireland



Image

Re: Snake.java

Postby Unspecified on Mon Apr 08, 2013 10:16 pm

I haven't thought about java in about 7 years, so i cant really help you get to the answer.

The best i can do is suggest you start sticking some debug lines in there to see what the various variables contain at each point and how far through the program its getting.

The first step is to see if the keypresses are actually being registered so stick a
Code: Select all
System.out.println("Blah variable is" + blah + ".");
inside 'keypressed' to see if its even getting there.

Basically just keep narrowing it down until you get to just a few lines that aren't doing what you expect and its easier to see the problem usually.
ImageUKCS Head Moderator. Retired Senior TF2 Admin.
Feel free to send me a Image or message me through Image with any questions or problems.
Don't forget that UKCS has an image uploader that's fast and free for all members!


17:43 - Salva: i did find out im not pregnant

One of the UKCS iSeries Clanbox Co-Organisers:
Image
User avatar
Unspecified
Executive Admin
Head Moderator
UKCS iSeries Attendee
 
Player: unspecified
STEAM_0:0:2326270
 
Posts: 4922
Joined: Mon Sep 22, 2008 3:05 pm
Location: UK



Image

Re: Snake.java

Postby Kaelan002 on Mon Apr 08, 2013 10:44 pm

OH GOD ITS JAVA. You should totally learn C# instead. ;)
There are 10 types of people in the world: those that know binary, those that don't, and those that didn't expect this joke to be in base 3.

[video =373,150]http://a.pomf.se/ixefrf.webm[/video] Now shhhh Kaelan... OOOOOOH
pomf.se is ded. :'(
User avatar
Kaelan002
UKCS Sponsor
UKCS iSeries Attendee
 
Player: Kae
STEAM_0:1:29878923
 
Posts: 2167
Joined: Sun Nov 07, 2010 12:28 pm
Location: The restaurant at the end of the universe



Image

Re: Snake.java

Postby Gh0st.IRE on Mon Apr 08, 2013 11:20 pm

Kaelan002 wrote:OH GOD ITS JAVA. You should totally learn C# instead. ;)


Believe me, that's the plan for the Summertime when I'm off! Got my books ready (digitally) and all!

Good idea Unspec, I'll try that tomorrow.
Ex-TF2 Admin. Forum Moderator. Add me on Steam or PM me here!
Image
Heresy MMO Guild Member.
User avatar
Gh0st.IRE
Moderator
 
Player: Rear Naked Bugs
STEAM_0:0:12137987
 
Posts: 4635
Joined: Tue Dec 16, 2008 3:09 pm
Location: Dublin, Ireland



Image

Re: Snake.java

Postby Gh0st.IRE on Mon Apr 08, 2013 11:26 pm

SOLVED!
Ex-TF2 Admin. Forum Moderator. Add me on Steam or PM me here!
Image
Heresy MMO Guild Member.
User avatar
Gh0st.IRE
Moderator
 
Player: Rear Naked Bugs
STEAM_0:0:12137987
 
Posts: 4635
Joined: Tue Dec 16, 2008 3:09 pm
Location: Dublin, Ireland



Image

Re: Snake.java

Postby Deathtaker27 on Tue Apr 09, 2013 7:49 am

post the good code so I can play it at work?
Image
Thanks Podge, speccy and Tao
User avatar
Deathtaker27
Old Timer
 
Player: DT - RS
STEAM_0:1:22687060
 
Posts: 3408
Joined: Sat Mar 28, 2009 8:40 pm



Image

Re: Snake.java

Postby adz on Tue Apr 09, 2013 8:59 pm

My God the code in that tutorial is horrible.


adz
Home Member
 
Player: Adz
STEAM_0:1:7111525
 
Posts: 297
Joined: Wed Jun 03, 2009 10:36 pm


Return to PC World, Technology and Gadgets

Who is online

Users browsing this forum: CommonCrawl [Bot] and 1 guest