• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Getov

Java Scroller Game

5 posts in this topic

Hello everyone !
So far I managed to make a simple vertical scrolling game but it is not working very good. I managed to make my space ship move and shoot , made the background move, and managed to handle collision detection but for ONE enemy only. If I add multiple enemies the collision doesn't work. I am not sure why, and I am not sure if I am doing it right.
(all the graphics I use are images)

I added all the enemies in ArrayList, and since then the collision stopped working. This is part of my code:

[source lang="java"]public class Game extends JPanel implements ActionListener {
private Player player;
private SpaceCraft enemy;
...........................
public ArrayList<SpaceCraft> enemies = new ArrayList<SpaceCraft>();
Random randGen = new Random();

public Game(){
player = new Player();
enemy = new SpaceCraft(200, -600);

// add enemies to the array
for (int i = 0; i < 1000 ; ++i){
int x_positon = 50 + randGen.nextInt(651);
int y_position = -randGen.nextInt(100000);

enemies.add(new SpaceCraft(x_positon, y_position));
}
...........................
}

...................................................................

public void detectCollisions(){
// FOR ONE ENEMY ONLY
Rectangle enemyBounds = enemy.getBounds();

ArrayList blasterShots = player.getBlasterShots();
for (int i = 0; i < blasterShots.size(); ++i){
Blaster temp = (Blaster)blasterShots.get(i);
Rectangle blasterBounds = temp.getBounds();

/* check if blaster collides with enemy */
if (enemyBounds.intersects(blasterBounds) && enemy.isAlive()){
enemy.setAlive(false);
temp.setVisible(false);
}
}

Rectangle playerBounds = player.getBounds();
if (playerBounds.intersects(enemyBounds)){
lostGame = true;
}

// HANDLE MANY ENEMIES - FAIL
ArrayList<Rectangle> enemiesBounds = new ArrayList<Rectangle>();
for (int i = 0; i < enemies.size(); ++i){
enemiesBounds.add(enemies.get(i).getBounds());
}
for (int i = 0; i > blasterShots.size(); ++i){
Blaster temp = (Blaster)blasterShots.get(i);
Rectangle blasterBounds = temp.getBounds();

for (int j = 0; j < enemies.size(); ++j){
if (enemiesBounds.get(j).intersects(blasterBounds) && enemies.get(j).isAlive()){
enemies.get(j).setAlive(false);
temp.setVisible(false);
}
}
}

}


// THIS IS THE CLASS FOR ENEMY BATTLE SHIPS

public class SpaceCraft implements Enemy {

private Image enemyImg;
private int x_pos;
private int y_pos;
private boolean alive;


public SpaceCraft(int x, int y){
this.x_pos = x;
this.y_pos = y;
this.alive = true;
ImageIcon img = new ImageIcon("images/enemy.png");
enemyImg = img.getImage();
}

public void moveRight(){

}

public void moveLeft(){

}

public void moveForward(int moveSpeed){
y_pos += moveSpeed;
}

public void setAlive(boolean is){
this.alive = is;
}

public boolean isAlive() {
return alive;
}

public Image getImage(){
return enemyImg;
}

public int getX(){
return this.x_pos;
}

public int getY(){
return this.y_pos;
}

// create rectangle around the enemy so we can check for collisions
public Rectangle getBounds(){
return new Rectangle(x_pos, y_pos, enemyImg.getWidth(null), enemyImg.getHeight(null));
}
}[/source]

If you need more code or more explanation tell me.
0

Share this post


Link to post
Share on other sites
// HANDLE MANY ENEMIES - FAIL
ArrayList<Rectangle> enemiesBounds = new ArrayList<Rectangle>();
for (int i = 0; i < enemies.size(); ++i){
enemiesBounds.add(enemies.get(i).getBounds());
}
[u][b] for (int i = 0; i [color=#ff0000]>[/color] blasterShots.size(); ++i){[/b][/u]

You wrote > when I think it should be <

Not sure if this cause your problem, but that would be a good bug to fix first [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
1

Share this post


Link to post
Share on other sites
OMG , jesus , I can't believe...... so stupid error.... THANKS man, I didn't even notice it.
Shit happens :D .

Thanks again, in the next few days I might have questions about making the game smoother , using threads and pressing more than 1 key at once... but for now I will try to do it myself :).
0

Share this post


Link to post
Share on other sites
Before going into threading (Which is a very delicate process) you better see how you can reduce processing time and memory allocation.

If you do the calculation of the same object more than once you might want to cache it somewhere.
Review container, if you don't need random access use forward iterator
What is called each loop. Do you call the only necessary stuff? Are you calling everything but should only call few things based on some conditions?
There are other optimization you can apply depending on game type etc. If there are no enemies in the screen, don't even check the bullets collision, stuff like that. Edited by deathwearer
0

Share this post


Link to post
Share on other sites
Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"]
public void keyPressed(KeyEvent e){

switch (e.getKeyCode()){

case KeyEvent.VK_LEFT:
keyLeft = true;
break;

case KeyEvent.VK_RIGHT:
keyRight = true;
break;

case KeyEvent.VK_SPACE:
space = true;
break;
}
}


public void keyReleased(KeyEvent e){

switch (e.getKeyCode()){

case KeyEvent.VK_LEFT:
keyLeft = false;
break;

case KeyEvent.VK_RIGHT:
keyRight = false;
break;
case KeyEvent.VK_SPACE:
space = false;
break;
}
}
}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter {

public void keyPressed(KeyEvent e){
player.keyPressed(e);
}

public void keyReleased(KeyEvent e){
player.keyReleased(e);
}
}

......................................................................................

public void actionPerformed(ActionEvent e){
/* TODO: anti-ghosting */
if (player.keyLeft){
player.moveLeft();
} else if (player.keyRight){
player.moveRight();
} else if (player.space){
player.generateBlaster();
}
............................................
}
[/source] Edited by Getov
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0