codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Making a simple game  herman91 at 18:26 on Tuesday, April 25, 2006
 

I need to make a game for a school project. Only problem is my Java experience is very limited and I have no idea how to start and what to do.

Does anyone have advice on how to make a simple side-scroll shooting game? As in how to use graphics and how to make the background and what kind of code is needed?

<Added>


I was looking to make it like a jump-and-run game, much like Metal Slug

  Re: Making a simple game  crwood at 08:27 on Wednesday, April 26, 2006
 


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Game extends JPanel
{
BufferedImage bgImage;
BufferedImage token;
int dx;
int dy;
Rectangle r;
Dimension size;

public Game(BufferedImage[] images)
{
bgImage = images[0];
token = images[1];
dx = 3;
dy = 2;
r = new Rectangle(200, 200, token.getWidth(), token.getHeight());
size = new Dimension(bgImage.getWidth(), bgImage.getHeight());
setRequestFocusEnabled(true);
}

public void moveToken(int x, int y)
{
r.x += x * dx;
r.y += y * dy;
repaint();
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int x = (w - bgImage.getWidth())/2;
int y = (h - bgImage.getHeight())/2;
g2.drawImage(bgImage, x, y, this);
g2.drawImage(token, r.x, r.y, this);
}

public Dimension getPreferredSize()
{
return size;
}

public static void main(String[] args) throws IOException
{
String[] paths = { "/forum/images/owls.jpg", "/forum/images/Bird.gif" };
BufferedImage[] images = new BufferedImage[paths.length];
for(int j = 0; j < images.length; j++)
images[j] = ImageIO.read(new File(paths[j]));
Game game = new Game(images);
game.addKeyListener(new TokenMover(game));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(game));
//f.setSize(400,400);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
game.requestFocus();
}
}

class TokenMover extends KeyAdapter
{
Game game;

public TokenMover(Game game)
{
this.game = game;
}

public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
int x = 0;
int y = 0;
switch(keyCode)
{
case KeyEvent.VK_UP:
y = -1;
break;
case KeyEvent.VK_LEFT:
x = -1;
break;
case KeyEvent.VK_DOWN:
y = 1;
break;
case KeyEvent.VK_RIGHT:
x = 1;
break;
default:
}
game.moveToken(x, y);
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: What does it entail making a Java program run without the editor?
•  Re: How can I avoid accepting a null value from user input?
•  Textarea help
•  Re: Rolling the dice, sort of...
•  Re: help please for newbie to java and i/o
•  Re: Another 2-D Array question
•  Re: path delimiters
•  Re: 2 Dimensional Array
•  Converting Java Program to C


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006