Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Hello all  (Read 17145 times)
0 Members and 1 Guest are viewing this topic.
Offline cooler

Junior Newbie

« Posted 2014-09-17 17:22:43 »

Hi there I'm hoping I can get some help here. I have been reading the book Begging Android games second edition. In which a game called Mr Nom (a snake game)is used as an example. I have been working through it and would like to adapt it a bit by taking out the snake and replacing it with a single character.

Now for the problem I cant seem to figure out how to adapt the code to just move a sprite without the tail.

Here is the original code:

public class Snake {
    public static final int UP = 0;
    public static final int LEFT = 1;
    public static final int DOWN = 2;
    public static final int RIGHT = 3;
    public List<SnakePart> parts = new ArrayList<SnakePart>();
    public int direction;   
    public Snake() {       
        direction = UP;
        parts.add(new SnakePart(5, 6));
        parts.add(new SnakePart(5, 7));
        parts.add(new SnakePart(5, 8));
    public void turnLeft() {
        direction += 1;
        if(direction > RIGHT)
            direction = UP;
    public void turnRight() {
        direction -= 1;
        if(direction < UP)
            direction = RIGHT;
    public void eat() {
        SnakePart end = parts.get(parts.size()-1);
        parts.add(new SnakePart(end.x, end.y));
    public void advance() {
        SnakePart head = parts.get(0);               
        int len = parts.size() - 1;
        for(int i = len; i > 0; i--) {
            SnakePart before = parts.get(i-1);
            SnakePart part = parts.get(i);
            part.x = before.x;
            part.y = before.y;
        if(direction == UP)
            head.y -= 1;
        if(direction == LEFT)
            head.x -= 1;
        if(direction == DOWN)
            head.y += 1;
        if(direction == RIGHT)
            head.x += 1;
        if(head.x < 0)
            head.x = 9;
        if(head.x > 9)
            head.x = 0;
        if(head.y < 0)
            head.y = 12;
        if(head.y > 12)
            head.y = 0;
    public boolean checkBitten() {
        int len = parts.size();
        SnakePart head = parts.get(0);
        for(int i = 1; i < len; i++) {
            SnakePart part = parts.get(i);
            if(part.x == head.x && part.y == head.y)
                return true;
        return false;

I understand what all the code means I just cant seem to untangle tail parts.

there is also a snakepart class:

public class SnakePart {
    public int x, y;
    public SnakePart(int x, int y) {
        this.x = x;
        this.y = y;

I have been trying for a few days now to solve this I am very new to this any help would be much appreciated even if its just pointing me in the right direction.

Thanks for taking the time to look at this.
Offline KevinWorkman

« JGO Plugged Duke »

Medals: 287
Projects: 12
Exp: 12 years - Coding Tutorials!

« Reply #1 - Posted 2014-09-17 17:30:39 »

I'm not really sure what you're asking. Are you asking how to stop using a snake and to use something more like Pac-Man? - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline cooler

Junior Newbie

« Reply #2 - Posted 2014-09-17 17:36:59 »

It would be just the head of the snake moving round the screen. No tail involved no growing when it eats things. In essence yes like pacman.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline saucymeatman
« Reply #3 - Posted 2014-09-17 18:02:39 »

You cannot program a game without knowing how to program.
Copying and pasting code will get you no-where.
Learn to program, then in a month, start programming your first game Smiley
Offline cooler

Junior Newbie

« Reply #4 - Posted 2014-09-17 18:22:18 »

I appreciate what your saying there Saucymeatman and I'm the first to say I have got along way to go but at this moment I am trying to learn. I do not expect to program a game for a long time yet what I am doing is playing about with code.

As said in the first post I am reading a book all about that, I have taken game code from one part of the book and thought what would happen if I tried this and how would I go about it. To my mind that is a good way to learn by breaking things and trying to put them back together.

As I have tried for a few days to work it out by adapting the code used in the book and failed as my knowledge is not good enough to see what I'm missing. I am not here asking for someone to rewrite the code for me but to maybe help me see what it is I am missing. Every day I learn something new and I hope to keep on learning.
Offline Drenius
« Reply #5 - Posted 2014-09-17 18:35:51 »

Rather try to write your own code while learning. The way you are doing it now you will only learn how to hijack other's code, but not to actually set up an even simple application from scratch.
Make your own so that you really understand what you are doing.
Offline KevinWorkman

« JGO Plugged Duke »

Medals: 287
Projects: 12
Exp: 12 years - Coding Tutorials!

« Reply #6 - Posted 2014-09-17 18:54:33 »

Instead of starting with a solution and trying to work backwards, maybe try it the other way around: can you create an MCVE that demonstrates exactly what you're confused about?

Start from scratch, and if you really want to use code you found from the internet, then take it one small step at a time. Only add as much code as it takes to show us exactly what you're confused about. That way we can copy and paste your code into our own editors to see what's going on. - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline cooler

Junior Newbie

« Reply #7 - Posted 2014-09-17 19:01:12 »

yep see what you mean KevinWorkman will work on that tomorrow and get back to you thanks all for the replies so far Smiley
Pages: [1]
  ignore  |  Print  

EgonOlsen (1869 views)
2018-06-10 19:43:48

EgonOlsen (1901 views)
2018-06-10 19:43:44

EgonOlsen (1258 views)
2018-06-10 19:43:20

DesertCoockie (1703 views)
2018-05-13 18:23:11

nelsongames (1379 views)
2018-04-24 18:15:36

nelsongames (2003 views)
2018-04-24 18:14:32

ivj94 (2753 views)
2018-03-24 14:47:39

ivj94 (1955 views)
2018-03-24 14:46:31

ivj94 (3045 views)
2018-03-24 14:43:53

Solater (1090 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!