Hi,
currently I am working on a school project (grade 12) in which we are supposed to create a small game. My idea was something like the old DOS-Game "Sopwith" which some of you may know; you are flying a plane and have to shoot / bomb some ground targets.
Until now, i managed to get a flying plane which shoots some bullets, crashes and explodes when you touch the world's borders, the ground or enemy buildings. Also, there is a complete world with hills, factorys, moving tanks and some other stuff. So far so good.
When a bullet hits an enemy, it gets removed from the world. But as one bullet normally does not destroy a whole tank, i added a private int which should count the bullets which already hit it.
Problems are: 1st the variable does not count down, ergo the enemy can not be destroyed. 2nd if I write "getWorld().removeObject(this);" to remove the bullet from the world after hitting a target the game stops, so i have to respawn it in a place where it cant do any damage (just an anoying problem)...
Full bullet.class code:
If you want to see the whole game just ask and I'll upload it.
Greetings from Berlin :)
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class bullet here. * * @author (your name) * @version (a version number or a date) */ public class bullet extends plane { private int speed = - 4 ; private int factory_life = 20 ; private int tank_life = 10 ; private int flak_life = 5 ; private int quarter_life = 20 ; /** * Act - do whatever the bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { /** * spawne die Kugel beim Flugzeug und beschleunige sie in die aktuelle Richtung */ Actor plane = getOneIntersectingObject(plane. class ); if (plane != null ) { setLocation(getX(), getY()); setRotation(rotation); move(- 4 ); } else { move(- 4 ); destroyEnemies(); if (atWorldEdge()) { setImage( "blank.png" ); setLocation( 100 , 600 ); move( 0 ); turn( 180 ); } if (atGround()) { setImage( "blank.png" ); setLocation( 100 , 600 ); move( 0 ); turn( 180 ); } } } /** * Wenn Gegner getroffen, Leben abziehen. Objekt verschwinden lassen, falls Leben = 0. */ public void destroyEnemies() { Actor enemy = getOneIntersectingObject(factory. class ); if (enemy != null ) { factory_life = factory_life - 1 ; if (factory_life== 0 ) { getWorld().removeObject(enemy); } setImage( "blank.png" ); setLocation( 100 , 600 ); move( 0 ); turn( 180 ); } Actor enemy2 = getOneIntersectingObject(tank. class ); if (enemy2 != null ) { tank_life = tank_life - 1 ; if (tank_life== 0 ) { getWorld().removeObject(enemy2); } setImage( "blank.png" ); setLocation( 100 , 600 ); move( 0 ); turn( 180 ); } Actor enemy3 = getOneIntersectingObject(flak. class ); if (enemy3 != null ) { flak_life = flak_life - 1 ; if (flak_life== 0 ) { getWorld().removeObject(enemy3); } setImage( "blank.png" ); setLocation( 100 , 600 ); move( 0 ); turn( 180 ); } Actor enemy4 = getOneIntersectingObject(quarter. class ); if (enemy4 != null ) { quarter_life = quarter_life - 1 ; if (quarter_life== 0 ) { getWorld().removeObject(enemy4); } setImage( "blank.png" ); setLocation( 100 , 600 ); move( 0 ); turn( 180 ); } } } |
