Painguy
Senior Newbie 
|
 |
«
Posted
2011-05-23 08: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.
|
|
|
|
|
zoto
|
 |
«
Reply #1 - Posted
2011-05-23 11:18:12 » |
|
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? 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) |
|
|
|
|
|
Orangy Tang
|
 |
«
Reply #2 - Posted
2011-05-23 12: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?
|
|
|
|
Games published by our own members! Check 'em out!
|
|
OverKill
Junior Member  
Java games rock!
|
 |
«
Reply #3 - Posted
2011-05-24 09: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.
|
|
|
|
|
Painguy
Senior Newbie 
|
 |
«
Reply #4 - Posted
2011-05-24 10: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); } } |
|
|
|
|
|
zoto
|
 |
«
Reply #5 - Posted
2011-05-24 11: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.
|
|
|
|
|
Painguy
Senior Newbie 
|
 |
«
Reply #6 - Posted
2011-05-24 23: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  . ok one other questio  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
|
|
|
|
|
zoto
|
 |
«
Reply #7 - Posted
2011-05-25 00: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.
|
|
|
|
|
ra4king
|
 |
«
Reply #8 - Posted
2011-05-25 06: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 
|
|
|
|
Painguy
Senior Newbie 
|
 |
«
Reply #9 - Posted
2011-05-25 23:17:05 » |
|
gaaahhh idk how to go about this delay thing 
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ra4king
|
 |
«
Reply #10 - Posted
2011-05-26 03: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; .. if(spacePressed && delay <= 0) { Laser shot = new Laser(); shot.loadlasers(); laser.add(shot); delay = 1000; }
if(delay > 0) delay -= timePassed; |
|
|
|
|
Painguy
Senior Newbie 
|
 |
«
Reply #11 - Posted
2011-05-28 04: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.
|
|
|
|
|
Nate
|
 |
«
Reply #12 - Posted
2011-05-28 06:37:31 » |
|
Use multiple bounding boxes.
|
|
|
|
ra4king
|
 |
«
Reply #13 - Posted
2011-05-28 10:08:32 » |
|
No matter how slow, java.awt.geom.Area is also good 
|
|
|
|
zoto
|
 |
«
Reply #14 - Posted
2011-05-28 12:06:30 » |
|
I need it to require you to let go in order to shoot again. Set spacePressed to false when you fire.
|
|
|
|
|
Painguy
Senior Newbie 
|
 |
«
Reply #15 - Posted
2011-05-29 04:40:41 » |
|
No matter how slow, java.awt.geom.Area is also good  I'm not exactly sure on how to use that  I looked something up & its like...a shape inside a shape? lol idk 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
|
|
|
|
|
ra4king
|
 |
«
Reply #16 - Posted
2011-05-29 05: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; } } |
|
|
|
|
Painguy
Senior Newbie 
|
 |
«
Reply #17 - Posted
2011-05-29 18:55:41 » |
|
already have that  although I'm not using switch statements. using if. could that be the problem? ---Edit----nope...not the problem.
|
|
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2011-05-29 22: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; ... } } .. if(spacePressed && !hasShot && delay <= 0) { Laser shot = new Laser(); shot.loadlasers(); laser.add(shot); hasShot = true; delay = 1000; } ... 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.
|
|
|
|
|