Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Enemy for top down shooter  (Read 4223 times)
0 Members and 1 Guest are viewing this topic.
Offline Painguy

Senior Newbie





« Posted 2011-05-23 06:50:01 »

so im creating a top down shooter as my first game, and i have everything working except for a boss & enemies. What I'm wondering how to do is move the enemies down on the screen in those fancy patterns. Also how would I make enemies shoot in the general direction of the player. I have several ideas on how to do this, but I want the most sure fire way to go about this.
Offline zoto

Senior Devvie


Medals: 4



« Reply #1 - Posted 2011-05-23 09:18:12 »

Quote
What I'm wondering how to do is move the enemies down on the screen in those fancy patterns.
What fancy patterns do you mean?

Quote
Also how would I make enemies shoot in the general direction of the player.
The angle the enemy needs to be to shoot at the player is:
1  
Math.atan2(targetY - myY, targetX - myX)
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2011-05-23 10:13:05 »

so im creating a top down shooter as my first game, and i have everything working except for a boss & enemies. What I'm wondering how to do is move the enemies down on the screen in those fancy patterns. Also how would I make enemies shoot in the general direction of the player. I have several ideas on how to do this, but I want the most sure fire way to go about this.

Have you seen BulletML?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline OverKill

Junior Devvie




Java games rock!


« Reply #3 - Posted 2011-05-24 07:46:42 »

@Zoto:
I think me means like the top-down scrollers where all the enemies fly in patterns past you.
IIRC Commando was also like this.

@OP:
You basically already have your answer.
Each wave will not only have the entities that move around, it would also have a 'waypoint' system upon which the entities move.
The waypoins would basically be a chain of vectors. i.e. Start @x/y and move to x1/y1, then x2/y2. etc pp.

For the shooting you could just put in a radius around the entity and when the player is in that radius, let the entities shoot with a random chance.
For 'shooting in the general direction' just add a random offset to the xp/yp.

I hope this is what you wanted to know.
Offline Painguy

Senior Newbie





« Reply #4 - Posted 2011-05-24 08:03:30 »

@Zoto:
I think me means like the top-down scrollers where all the enemies fly in patterns past you.
IIRC Commando was also like this.

@OP:
You basically already have your answer.
Each wave will not only have the entities that move around, it would also have a 'waypoint' system upon which the entities move.
The waypoins would basically be a chain of vectors. i.e. Start @x/y and move to x1/y1, then x2/y2. etc pp.

For the shooting you could just put in a radius around the entity and when the player is in that radius, let the entities shoot with a random chance.
For 'shooting in the general direction' just add a random offset to the xp/yp.

I hope this is what you wanted to know.
alright so it was basically what I had in mind. before i get started on that tho i need to fix another problem i have. so i got started on shooting the laser, but I ran into some issues. every time i press space nothing happens. I've already created a counter of the number of times it goes thru & it seems to be working. idk whats wrong. if u dont mind I will post some of my code so u guys can review it. I feel like I'm doing all this inefficiently.

Game loop in Main. Check the level1 if statement portion.
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  
public void run(DisplayMode dm2) throws InvalidMidiDataException, IOException, MidiUnavailableException, FontFormatException{
      s = new Renderer();
      dm = s.find1stcompatmode(modes1);
      s.setFullScreen(dm);
      s.getFullScreenWindow().addKeyListener(new KeyInputHandeler());
      s.getFullScreenWindow().requestFocus();
      m.loadmenu();
      menuactive = true;
      long startTime = System.currentTimeMillis();
      long cumTime = startTime;
      while(islooping){
         long timePassed = System.currentTimeMillis() - cumTime;
         cumTime = System.currentTimeMillis();
         Graphics2D g = s.getGraphics();
         if(menuactive){
            m.selection();
            m.a.update(timePassed);
            m.h.update(timePassed);
               if(playon){
                  m.p.update(timePassed);
               }
               if(exiton){
                  m.e.update(timePassed);
               }
               m.draw(g);
         }
         if(levelselactive){
            ls.selection();
            ls.hand.update(timePassed);
            ls.p.update(timePassed);
            ls.c.update(timePassed);
            ls.m.update(timePassed);
            ls.draw(g);
         }
         if(level1){
            if(spacePressed){
               Laser shot = new Laser();
               shot.loadlasers();
               laser.add(shot);
            }
            if(!laser.isEmpty()){
               for(int i = 0; i<laser.size(); i++){
                  laser.get(i).las1.update(timePassed);
                  laser.get(i).drawlaser(g);
                  laser.get(i).move();
               }
            }
            l1.player.f.update(timePassed);
            l1.drawscroll(g);
            l1.player.drawship(g);
            l1.scroll();
            l1.player.move();
         }
         g.dispose();
         s.update();
         try{
            Thread.sleep(10);
         }catch(Exception ex){}
         }
         s.restoreScreen();
   }
}


Also here is my laser class
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  
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;

class Laser {
   private Image blaser,blaser1,blaser2;
   public Ani las1;
   private int y;
   public void move(){
      y+=1;
   }
   public void loadlasers(){
      blaser = new ImageIcon("C:\\Users\\Nima\\workspace\\Computer Science Game\\Sprites\\Playership\\Laser\\blas1.png").getImage();
      blaser1 = new ImageIcon("C:\\Users\\Nima\\workspace\\Computer Science Game\\Sprites\\Playership\\Laser\\blas2.png").getImage();
      blaser2 = new ImageIcon("C:\\Users\\Nima\\workspace\\Computer Science Game\\Sprites\\Playership\\Laser\\blas3.png").getImage();
      las1 = new Ani();
      las1.addScene(blaser, 20);
      las1.addScene(blaser1, 20);
      las1.addScene(blaser2, 20);
      y = PlayerShip.y+20;
   }
   public void drawlaser(Graphics2D g){
      g.drawImage(las1.getImage(), PlayerShip.x, y, null);
   }
}
Offline zoto

Senior Devvie


Medals: 4



« Reply #5 - Posted 2011-05-24 09:27:26 »

Just to be clear your expected behavior is for the laser to go from the top of the screen down but keep the same x position as the player as the player moves? If it is then it looks like your code should work like that as long as its being called, maybe add a few System.out.println calls to check.

It seems odd that you are drawing the lasers and ship and them immediately moving them to a different position.

There is some nice game loop code in the tutorials section. Using BufferedImages is a lot better than using ImageIcon. Load your images once and reuse them for all the lasers, there is no reason to store multiple identical images in memory.
Offline Painguy

Senior Newbie





« Reply #6 - Posted 2011-05-24 21:00:53 »

Just to be clear your expected behavior is for the laser to go from the top of the screen down but keep the same x position as the player as the player moves? If it is then it looks like your code should work like that as long as its being called, maybe add a few System.out.println calls to check.

It seems odd that you are drawing the lasers and ship and them immediately moving them to a different position.

There is some nice game loop code in the tutorials section. Using BufferedImages is a lot better than using ImageIcon. Load your images once and reuse them for all the lasers, there is no reason to store multiple identical images in memory.
well i need to turn in this game on friday & since this is my first game I just want to get the very basics down. After I turn it in ill probably refine it. I'll make sure to checl those tutorials out Smiley. ok one other questio Tongue I got my bullets to work finnaly(i shoot them & once they go offscreen i remove them). What I want to do is force the player to press the shoot button to only get 1 shot. As of now if you press it 1 time it will shoot as long as you have the ur hand on space. thx in advance
Offline zoto

Senior Devvie


Medals: 4



« Reply #7 - Posted 2011-05-24 22:52:06 »

Currently you are allowing a new shot to be fired ever update as long as the spacePressed is true, you will need to add an addition check to see if they can fire.
I like to figure out the cool-down time and then set a timer to this cool-down after a shot is fired, then each update you decrement the time elapsed. Doing it this way your 'can fire' check is just seeing if the timer is less than 1.

This is just a simple timer, you probably use something similar in your Class Ani.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-05-25 04:32:52 »

Currently you are allowing a new shot to be fired ever update as long as the spacePressed is true, you will need to add an addition check to see if they can fire.
I like to figure out the cool-down time and then set a timer to this cool-down after a shot is fired, then each update you decrement the time elapsed. Doing it this way your 'can fire' check is just seeing if the timer is less than 1.

This is just a simple timer, you probably use something similar in your Class Ani.
Ah no the Ani class operates where you have to call update with a deltaTime and that's how it updates the images Wink

Offline Painguy

Senior Newbie





« Reply #9 - Posted 2011-05-25 21:17:05 »

gaaahhh idk how to go about this delay thing Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-05-26 01:53:02 »

here is some pseudocode:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
...
private int delay = 0;
..
//in game loop
if(spacePressed && delay <= 0) {
    //add bullet
    Laser shot = new Laser();
    shot.loadlasers();
    laser.add(shot);
   
    delay = 1000; //1 second delay
}

if(delay > 0)
    delay -= timePassed;

Offline Painguy

Senior Newbie





« Reply #11 - Posted 2011-05-28 02:10:44 »

well it kinda worked. That got it to be a continuous stream, but if u hold the space button it still keeps shooting. I need it to require you to let go in order to shoot again. Also I've recently created a basic boss. I've kinda got that working, but I need to do some collision detection stuff which I've been having a hard time comprehending. I dont want to use the bounding box method for the boss sprite since its so large & has random gaps. How would I get a mask of the sprite? I have a feeling I have to ignore the transparency some how, but Im still new to all of this so I dont know how. I read somethign about getting RBG's of the sprite then storing them in an array or something...idk lol.
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #12 - Posted 2011-05-28 04:37:31 »

Use multiple bounding boxes.

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-05-28 08:08:32 »

No matter how slow, java.awt.geom.Area is also good Smiley

Offline zoto

Senior Devvie


Medals: 4



« Reply #14 - Posted 2011-05-28 10:06:30 »

Quote
I need it to require you to let go in order to shoot again.
Set spacePressed to false when you fire.
Offline Painguy

Senior Newbie





« Reply #15 - Posted 2011-05-29 02:40:41 »

No matter how slow, java.awt.geom.Area is also good Smiley

I'm not exactly sure on how to use that Sad I looked something up & its like...a shape inside a shape? lol idk

Quote
I need it to require you to let go in order to shoot again.
Set spacePressed to false when you fire.
I tried that & it works for like 5 secs then just stops working afterwards. Also I just recently noticed that I can't move diagonaly to my left & shoot at the same time. I have to do one or the other. Sorry to bombard you guys with questions. Its my first game so i gotz me lotz n lotz of questions
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-05-29 03:26:57 »

The move and shoot problem is easily solved by using booleans for each button. in keyPressed, you set the appropriate boolean to true if that key is down. In keyReleased, you set that boolean to false. Each call to update() you update your logic based on what is pressed and what is not.

Example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private boolean left, right, up, down, space;
....
public void keyPressed(KeyEvent key) {
    switch(key.getKeyCode()) {
        case KeyEvent.VK_LEFT: left = true; break;
        case KeyEvent.VK_RIGHT: right = true; break;
        ....
    }
}

public void keyReleased(KeyEvent key) {
    switch(key.getKeyCode()) {
        case KeyEvent.VK_LEFT: left = false; break;
        case KeyEvent.VK_RIGHT: right = false; break;
    }
}

Offline Painguy

Senior Newbie





« Reply #17 - Posted 2011-05-29 16:55:41 »

already have that Sad although I'm not using switch statements. using if. could that be the problem?
---Edit----nope...not the problem.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-05-29 20:37:10 »

Yeah I think the problem is that even if you set "spacePressed" to false, holding it down will still set it back to true in the keyPresed. You should have another boolean, "hasShot" that is set to true when you add the Laser to the ArrayList.
Then you set it back to false in your keyReleased method.
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  
private boolean spacePressed, hasShot;
...
public void keyPressed(KeyEvent key) {
    switch(key.getKeyCode()) {
        ...
        case KeyEvent.VK_SPACE: spacePressed = true; break;
        ...
    }
}

public void keyReleased(KeyEvent key) {
    switch(key.getKeyCode()) {
        ...
        case KeyEvent.VK_SPACE: spacePressed = hasShot = false; break;
        ...
    }
}
..
//in game loop
if(spacePressed && !hasShot && delay <= 0) {
    //add bullet
    Laser shot = new Laser();
    shot.loadlasers();
    laser.add(shot);
    hasShot = true;
    delay = 1000; //1 second delay
}
...
if(delay > 0)
    delay -= timePassed;


EDIT: Switch statements are a convenient way to write if statements where it all depends on 1 value. No difference between writing a switch statement and an if-else ladder.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (86 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (81 views)
2014-12-05 12:39:02

SHC (91 views)
2014-12-03 16:27:13

CopyableCougar4 (98 views)
2014-11-29 21:32:03

toopeicgaming1999 (157 views)
2014-11-26 15:22:04

toopeicgaming1999 (154 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!