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

2013/5/20

please help!

manish123 manish123

2013/5/20

#
Hello, i am new to greenfoot and need help with programming a computer tank that can track your movements, i am stuck and need help, Thank you!
danpost danpost

2013/5/20

#
Be more specific. Show some code (whatever you have tried).
manish123 manish123

2013/5/20

#
ok well this is what i have tried: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ListIterator; /** * Write a description of class greenTank here. * * @author (your name) * @version (a version number or a date) */ public class greenTank extends platformMover { /** * Act - do whatever the greenTank wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnTowardTank(); int distance = (int)(Math.random()*5)+1; move(distance); } public void turnTowardTank() { World world = getWorld(); List<Tank> tank = world.getObjects(Tank.class); ListIterator<Tank> iter = tank.listIterator(); if(iter.hasNext()) { } Tank enemy = iter.next(); int eX = enemy.getX(); int y = getY(); int x = getX(); int angle = getRotation(); if(x < eX) { if(angle == 180) { turn(); } else if(x > eX) { if(angle == 0) { turn(); } } } } public void turn() { GreenfootImage img = getImage(); img.mirrorVertically(); setImage(img); turn(180); } }
manish123 manish123

2013/5/20

#
but the computer tank just wont move
You need to login to post a reply.