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.
[quote]What I’m wondering how to do is move the enemies down on the screen in those fancy patterns.
[/quote]
What fancy patterns do you mean?
[quote]Also how would I make enemies shoot in the general direction of the player.
[/quote]
The angle the enemy needs to be to shoot at the player is:
Math.atan2(targetY - myY, targetX - myX)
@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.
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
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);
}
}
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
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
gaaahhh idk how to go about this delay thing
here is some pseudocode:
...
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;
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.
Use multiple bounding boxes.
No matter how slow, java.awt.geom.Area is also good
[quote]I need it to require you to let go in order to shoot again.
[/quote]
Set spacePressed to false when you fire.
I’m not exactly sure on how to use that I looked something up & its like…a shape inside a shape? lol idk
Set spacePressed to false when you fire.
[/quote]
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
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:
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;
}
}
already have that although I’m not using switch statements. using if. could that be the problem?
—Edit----nope…not the problem.
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.
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.