I need help to make a character jump in java. This is my main class
Board.java
package com.mime.mario;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.mime.mario.graphics.Background;
import com.mime.mario.graphics.BasicImages;
import com.mime.mario.graphics.Grass;
import com.mime.mario.graphics.Player;
public class Board extends JPanel implements MouseListener, ActionListener {
private static final long serialVersionUID = 1L;
BasicImages bi;
Background b;
Grass g1;
Player p;
Timer time;
int i = 0;
Font font = new Font("Arial", Font.BOLD, 20);
String score = "Score = " + i;
public Board() {
bi = new BasicImages();
g1 = new Grass();
b = new Background();
p = new Player();
time = new Timer(5, this);
time.start();
addMouseListener(this);
addKeyListener(new Al());
setFocusable(true);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void paint(Graphics g) {
g.drawImage(b.getBackground(), 0, 0, null);
// prints the walls
for (int x = 50; x < 896; x += 128) {
g.drawImage(bi.getWall(), x, 120, null);
}
for (int x = 480; x < 700; x += 64) {
g.drawImage(bi.getWall(), x, 350, null);
}
// prints the coins
for (int x = 100; x < 900; x += 128) {
g.drawImage(bi.getCoin(), x, 100, null);
}
// prints the grass
for (int x = -20; x < 960; x += 64) {
for (int y = 520; y < 590; y += 64) {
g.drawImage(g1.getGrass(), x, y, null);
}
}
g.drawImage(p.getPlayer(),p.getX(),p.getY(),null);
g.setFont(font);
g.drawString(score, 416, 64);
}
public void mouseClicked(MouseEvent e) {
i++;
score = "Score = " + i;
repaint();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public class Al extends KeyAdapter {
Rectangle nextlevel = new Rectangle(900, 430, 32, 700);
Rectangle player = new Rectangle(p.getX(), p.getY(), 44, 77);
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_LEFT) {
p.move(-32, 0);
}
if (keycode == KeyEvent.VK_RIGHT) {
if (!player.intersects(nextlevel)) {
p.move(32, 0);
}
}
if (keycode == KeyEvent.VK_UP) {
p.move(0, -32);
}
if (keycode == KeyEvent.VK_DOWN) {
p.move(0, 32);
}
if (keycode == KeyEvent.VK_SPACE) {
p.move(0, -32);
}
if (player.intersects(nextlevel)) {
System.out.println("gshjfkjasf");
}
repaint();
}
public void keyRelased(KeyEvent e) {
p.move(0, 0);
}
}
Other Class: Player.java
package com.mime.mario.graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Player {
public int x = 75;
public int y =460;
ImageIcon img = new ImageIcon("C://Games//Mario//Characters//mario temporary.png");
public Image mario = img.getImage();
public Image getPlayer() {
return mario;
}
public void move(int dx, int dy){
x+=dx;
y+=dy;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
I know this is really long but if you could take the time just to look over it and tell me how to make the character jump I would be really grateful. Thank you! Please help me!
You can replace the sleep
method with the sprite's wait
method. Eg
//Creating a simple sprite
Sprite mario;
//Replacing sleep
mario.wait(200);
Or you can use Thread.Sleep
if you are using a Runnable game Canvas.
I'm just guessing here, but:
Right now, when you press the up-arrow key, the character is drawn in a new location (30 pixels up from where it was before).
You want it to move up a few times, pause, move down a few times.
The very simplest way of doing this will do what you ask, but it makes the system unresponsive while it runs:
int n;
for (n = 0; n < 3; ++n) {
p.move(0, -32);
sleep(200);
}
for (n = 0; n < 3; ++n) {
p.move(0, 32);
sleep(200);
}
If you're actually trying to write a game, you have a long way to go.
Edited to add:
Probably you need Thread.sleep() instead of sleep--I'm not a java person, but this might help you .
You really should read through some java tutorials at this point.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.