Last active
August 29, 2015 14:20
-
-
Save danleyb2/592945a0fb931bee43e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.KeyEvent; | |
import java.util.ArrayList; | |
import javax.swing.ImageIcon; | |
public class Dude { | |
int x, dx, y,nx,nx2,left, dy; | |
Image still,jump,reverse; | |
int ammo = 10; | |
ImageIcon s = new ImageIcon("motorbike.png"); | |
ImageIcon j= new ImageIcon("motorbike.png"); | |
ImageIcon l = new ImageIcon("motorbikeleft.png"); | |
static ArrayList bullets; | |
public Dude() { | |
x = 75; | |
left = 150; | |
nx = 0; | |
nx2= 685; | |
y = 172; | |
still = s.getImage(); | |
bullets = new ArrayList(); | |
} | |
public Rectangle getBounds() | |
{ | |
return new Rectangle(left,y, 63, 154); | |
} | |
public static ArrayList getBullets() | |
{ | |
return bullets; | |
} | |
public void fire() | |
{ | |
if (ammo > 0) | |
{ | |
ammo--; | |
//The v is from the board class, which corresponds to the character's | |
//position when it is jumping, resulting in the bullet being formed | |
//at a higher position when the character is at the peak of its jump | |
Bullet z = new Bullet((left + 60), (Board.v + 154/2)); | |
bullets.add(z); | |
}} | |
public void move() { | |
if (dx != -1){ | |
if (left + dx <= 150) | |
left+=dx; | |
else{ | |
x = x + dx; | |
nx2= nx2+dx; | |
nx = nx + dx; | |
}} | |
else | |
{ | |
if (left+dx >0) | |
left = left + dx; | |
} | |
} | |
public int getX() { | |
return x; | |
} | |
public int getLeft(){ | |
return left; | |
} | |
public int getnX() { | |
return nx; | |
} | |
public int getnX2() { | |
return nx2; | |
} | |
public int getdx() { | |
return dx; | |
} | |
public Image getImage() { | |
return still; | |
} | |
public void keyPressed(KeyEvent e) { | |
int key = e.getKeyCode(); | |
if (key == KeyEvent.VK_LEFT) | |
{ dx = -1; | |
still = l.getImage(); } | |
if (key == KeyEvent.VK_RIGHT) | |
{dx = 1; | |
still = s.getImage(); | |
} | |
if (key == KeyEvent.VK_SPACE) | |
{ | |
fire(); | |
} | |
if (key == KeyEvent.VK_UP) | |
{dy = 1; | |
still = j.getImage(); | |
} } | |
public void keyReleased(KeyEvent e) { | |
int key = e.getKeyCode(); | |
if (key == KeyEvent.VK_LEFT) | |
dx = 0; | |
if (key == KeyEvent.VK_RIGHT) | |
dx = 0; | |
if (key == KeyEvent.VK_UP) | |
{dy = 0; | |
still = s.getImage();} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment