Jump to content

how to program ai

prolemur

I want to write ai for the aliens in space invader and I don't know how to go about writing something that all the individual aliens will follow.

Also I want to know how to apply the ai to the entity, or else it would be useless.

 

It would be something like:

move right;if moving right and hit right wall or moving left and hit left wall{  change direction;  move down;}
Link to comment
Share on other sites

Link to post
Share on other sites

You could have a square representing the whole mass for collision boundaries, which would need to be adjusted as they died off. When that square hits the edge of the screen then you change direction and drop. Then to apply it to all entities you can have each position be a translation of some origin point in the group.

 

That's what I would do anyway. If I need to elaborate just ask.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

You could have a square representing the whole mass for collision boundaries, which would need to be adjusted as they died off. When that square hits the edge of the screen then you change direction and drop. Then to apply it to all entities you can have each position be a translation of some origin point in the group.

 

That's what I would do anyway. If I need to elaborate just ask.

I guess since this is pretty ,uch all one dimensional as in it doesn't really have to be a box going around all the aliens but it could just be a line that goes on the lowest row from lowest to highest column and I can control the movement of that line and change the aliens relative to that line, correct?

Link to comment
Share on other sites

Link to post
Share on other sites

I guess since this is pretty ,uch all one dimensional as in it doesn't really have to be a box going around all the aliens but it could just be a line that goes on the lowest row from lowest to highest column and I can control the movement of that line and change the aliens relative to that line, correct?

What would work really easy, and sound like what you're saying, is you can just check the left most and right most ships.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

What would work really easy, and sound like what you're saying, is you can just check the left most and right most ships.

Here's what I did so far, I wasn't really feeling starting from scratch so I grabbed the half complete code for moving and firing and barriers (which I will insert later) from a previous attempt : http://linustechtips.com/main/topic/64333-turing-alien-invaders-problem/ and I added the aliensline that moves following very simple instructions.

var padding : int := 10var ship : boolean := truevar x : int := maxx div 2var y : int := paddingvar speed : int := 1var xsize : int := 20var ysize : int := 10var fire : boolean := falsevar xfire : int := maxx div 2var yfire : int := padding + ysizevar firespeed : int := 2var xfiresize : int := 2var yfiresize : int := 6var alien : boolean := truevar xaliensize : int := 30var yaliensize : int := 14var alienswide : int := 10var alienstall : int := 8var alienspeed : int := 1var xalienssize : int := xaliensize * alienswide + ((alienswide - 1) * padding)var xalien : int := (maxx - xalienssize) div 2var yalien : int := maxy - (yaliensize * (alienstall + 1)) - (alienstall + 1) * paddingvar control : array char of booleanprocedure input    Input.KeyDown (control)    if control (KEY_LEFT_ARROW) and x > padding then        x -= speed    elsif control (KEY_RIGHT_ARROW) and x + xsize < maxx - padding then        x += speed    end ifend inputprocedure alienai    xalien += alienspeed    if xalien + xalienssize >= maxx - padding or xalien <= padding then        alienspeed := alienspeed * -1        yalien -= yaliensize    end ifend alienaiprocedure firing    if fire = true then        yfire += firespeed        Draw.FillBox (xfire, yfire, xfire + xfiresize, yfire + yfiresize, white)        if yfire >= maxy then            fire := false            yfire := padding + ysize        end if    elsif fire = false then        if control (chr (32)) then            fire := true            xfire := x + xsize div 2 - xfiresize div 2        end if    end ifend firingprocedure graphics    if ship = true then        Draw.Box (x, y, x + xsize, y + ysize, white)    end if    if fire = true then        Draw.Box (xfire, yfire, xfire + xfiresize, yfire + yfiresize, white)    end if    if alien = true then        Draw.Line (xalien, yalien, xalien + xalienssize, yalien, white)    end if    colorback (black)    delay (10)    clsend graphicsloop    input    alienai    firing    graphicsend loop% if (1,1..6) dead then%     aleinx1Dline += aleinxsize%%     alienxsize * 6 + alienelbowroom * 5 := alein1Dline

I'm probably going to need to add a 2D boolean array later to hold what aliens are alive to check a lot of things like xalienssize so it can move all the way to the left and right borders and other things. Have any feedback so far?

Link to comment
Share on other sites

Link to post
Share on other sites

Have any feedback so far?

 

Right now it looks like you are running your "alienai" routine on each alien individually. If I understand correctly then this will turn into each individual alien running until it hits a wall and then changing direction. Is that the behavior you want or are you after them moving in formation like the original?

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

Right now it looks like you are running your "alienai" routine on each alien individually. If I understand correctly then this will turn into each individual alien running until it hits a wall and then changing direction. Is that the behavior you want or are you after them moving in formation like the original?

I'm going after the original, but there will also be something in there making sure the aliens can't touch each other and move as a unit, I would upload the code I have for just the movement so far but it's kind of sketch..

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so what you would do is find the current coordinates of the spaceships, if their coordinates is lesser or equal to 0 then keep on decrementing their coordinates until it is previous coordinates - height of spaceships. Same principles apply for right but instead what you do is if their coordinates is greater or equal to screen width and if so move down (decrementing coordinates until it is previous - height of spaceships)

Link to comment
Share on other sites

Link to post
Share on other sites

I put the aliens into an array that I can control or let the user decide the amount of aliens wide and tall. The problem is the array of aliens won't move when I try increasing the alienx value in the loop

nvm this problem was resolved because when I originally had the array in the loop it spayed black all over the screen so I moved it outside, but it's because I had not set a delay or a clear screen in the loop, now it works fine. Next step will be getting the hit detection of the aliens with the bullet and I'm only semi sure of how I will be decreasing the alienx stuff.. :)

var xpadding : int := 10var ypadding : int := 10var alienspeed : int := 1var alienswide : int := 8var alienstall : int := 4var xaliensize : int := 40var yaliensize : int := 20var alienx : int := (maxx - (alienswide * xaliensize) - ((alienswide - 1) * xpadding)) div 2var alieny : int := maxy - (alienstall + 1) * yaliensize - (alienstall + 1) * ypaddingvar aliens : array 1 .. alienswide, 1 .. alienstall of booleanloop    for x : 1 .. alienswide        for y : 1 .. alienstall            aliens (x, y) := true            if aliens (x, y) = true then                Draw.Box (alienx + (x - 1) * xaliensize + (x - 1) * xpadding, alieny + (y - 1) * yaliensize + (y - 1) * ypadding, alienx + x * xaliensize + (x - 1) * xpadding, alieny + y * yaliensize                    + (y - 1) * ypadding, black)            end if        end for    end for    alienx += alienspeed    if alienx + alienswide * xaliensize + (alienswide - 1) * xpadding >= maxx - xpadding or alienx <= xpadding then        alienspeed := alienspeed * -1        alieny -= yaliensize - ypadding    end if    delay (10)    clsend loop
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×