This site requires JavaScript, please enable it in your browser!
Greenfoot back
Lifeslayer
Lifeslayer wrote ...

2014/11/8

Turn Based Game?

Lifeslayer Lifeslayer

2014/11/8

#
Hello! So I am fairly new here. I just started the other day but I have little experience using Java. I am trying to learn more for school competition because I am good with other programming as well. Here is my problem. I have a game with little set up yet, but I want to make it turn based and the snakes controlled by an A.I. So what I want is one move for one piece a turn. I want to be able to have it controlled by mouse in that case, correct? Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Spider here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Spider extends Actor
{
    /**
     * Act - do whatever the Spider wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public void act()
    if (Greenfoot.isKeyDown("w"))
    {
        turn(-3);
    
    if (Greenfoot.isKeyDown("s"))
    {
        turn(3);
    }
    if (Greenfoot.isKeyDown("d"))
    {
        move(1);
    }
    Actor snake;
    snake = getOneObjectAtOffset(0, 0, Snake.class);
    if (snake != null)
    {
        World world;
        world = getWorld();
        world.removeObject(snake);
    }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Snake here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Snake extends Actor
{
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Ant here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ant extends Actor
{
    /**
     * Act - do whatever the Ant wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         
    }   
}
The snakes are A.I. The spiders are Player Controlled with mouse The ants are backround (I want them to move around randomly. Don't know how)
Lifeslayer Lifeslayer

2014/11/8

#
*UPDATE* I fixed the ants as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    public void act()
{
    {
       movement();
    }   
}   
     
public void movement()
{
     
        move(7);
        if (Greenfoot.getRandomNumber(100)<20){
            turn(15);
        }
}  
}
danpost danpost

2014/11/8

#
You will have to change the name of your act methods so they are not called every act cycle (you will be calling them during their turns). Also, you will have to remove the keystroke detection and replace it with mouse action detection. Finally, your world class act method will have to control when each actor takes its turn. Does that help for a start?
Lifeslayer Lifeslayer

2014/11/8

#
danpost wrote...
You will have to change the name of your act methods so they are not called every act cycle (you will be calling them during their turns). Also, you will have to remove the keystroke detection and replace it with mouse action detection. Finally, your world class act method will have to control when each actor takes its turn. Does that help for a start?
That somewhat helps. I would understand it a lot more if you left an edit in the source code
Super_Hippo Super_Hippo

2014/11/8

#
You said the spiders are controlled with the mouse. It looks like you can control them with 'dws' (idk why in this order though and not the usual 'wasd' moving). Well, is it a game for just one player? Place the controlling in the world act method. If you only have one player, just have something like 'if any key was pressed (or mouse control was used), then move the spider and move the snakes randomly one step'. If you have more than one player, then you set a variable to save who's turn it is. After everyone moved, let the snakes move once.
Lifeslayer Lifeslayer

2014/11/8

#
Super_Hippo wrote...
You said the spiders are controlled with the mouse. It looks like you can control them with 'dws' (idk why in this order though and not the usual 'wasd' moving). Well, is it a game for just one player? Place the controlling in the world act method. If you only have one player, just have something like 'if any key was pressed (or mouse control was used), then move the spider and move the snakes randomly one step'. If you have more than one player, then you set a variable to save who's turn it is. After everyone moved, let the snakes move once.
I have it for one player. I have no idea what the coding is for all of that is though:( and I want the player one to control Spiders while A.I. controls Snakes
Super_Hippo Super_Hippo

2014/11/8

#
What exactly don't you understand? When adding the objects to the world, you save reference to them in the world, so you can call methods on them later (when they have to be moved).
danpost danpost

2014/11/8

#
Have you any experience with object-oriented languages? if not (and, even if so), start here for everything java related.
Lollygag Lollygag

2014/11/11

#
Best option to learn programming is with this.
danpost danpost

2014/11/11

#
Lollygag wrote...
Best option to learn programming is with this.
This would be for everything Greenfoot related (which basically teaches one how to use the Greenfoot IDE).
Lifeslayer Lifeslayer

2014/11/25

#
danpost wrote...
Have you any experience with object-oriented languages? if not (and, even if so), start here for everything java related.
I have knowledge in HTML, VBScript, .NET
You need to login to post a reply.