01-24-2013, 08:43 PM
I would like to create a basic game that is kind of like the mario flash games. I'm still testing on some of the concepts, so I created this testing class. I'm trying to get a gravity affect by making the y coodinate lower until it hits the floor using an ".intersects" for the collision. But the box(the box is my test charater) thing just floats in the air and the repaint() command just makes it flash ut it does not move down. I also tried to make the box move using a key listener, but I don't think it even makes it up to that line of code. Here's the code so far:
[code2=java]import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class rectPlayer
extends Applet
implements KeyListener{
private static final long serialVersionUID = 1L;
int x = 10;
int y = 10;
Rectangle rect;
Rectangle floor;
public void init()
{
this.addKeyListener(this);
rect = new Rectangle (x,y,50,50);
floor = new Rectangle(0,200,500,10);
}
public void paint(Graphics g)
{
setSize(new Dimension(500,300));
Graphics2D g2 = (Graphics2D)g;
g2.draw(rect);
g2.draw(floor);
if (rect.intersects(floor))
{
y--;
repaint();
}else{
y++;
repaint();
}
}
@SuppressWarnings("unused")
@Override
public void keyPressed(KeyEvent e) {
if (KeyEvent.KEY_PRESSED == KeyEvent.VK_RIGHT)
{
x++;
x++;
}
else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_LEFT){
x--;
x--;
}
else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_UP){
y--;
y--;
}
else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_DOWN){
y++;
y++;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}[/code2]
[code2=java]import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class rectPlayer
extends Applet
implements KeyListener{
private static final long serialVersionUID = 1L;
int x = 10;
int y = 10;
Rectangle rect;
Rectangle floor;
public void init()
{
this.addKeyListener(this);
rect = new Rectangle (x,y,50,50);
floor = new Rectangle(0,200,500,10);
}
public void paint(Graphics g)
{
setSize(new Dimension(500,300));
Graphics2D g2 = (Graphics2D)g;
g2.draw(rect);
g2.draw(floor);
if (rect.intersects(floor))
{
y--;
repaint();
}else{
y++;
repaint();
}
}
@SuppressWarnings("unused")
@Override
public void keyPressed(KeyEvent e) {
if (KeyEvent.KEY_PRESSED == KeyEvent.VK_RIGHT)
{
x++;
x++;
}
else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_LEFT){
x--;
x--;
}
else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_UP){
y--;
y--;
}
else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_DOWN){
y++;
y++;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}[/code2]