我需要帮助才能在java中跳转角色。 这是我的主要课程
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);
}
}
其他课程: 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;
}
}
我知道这真的很长但是如果你能花点时间去看它并告诉我如何让角色跳跃,我会非常感激。 谢谢!请帮帮我!
答案 0 :(得分:1)
您可以使用精灵的sleep
方法替换wait
方法。例如。
//Creating a simple sprite
Sprite mario;
//Replacing sleep
mario.wait(200);
如果您使用的是Runnable游戏画布,则可以使用Thread.Sleep
。
答案 1 :(得分:0)
我只是猜测,但是:
现在,当您按向上箭头键时,角色将在新位置绘制(距离之前的位置为30像素)。
你希望它向上移动几次,暂停,向下移动几次。
非常这样做的最简单方法就是按照你的要求行事,但它会使系统在运行时无响应:
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);
}
如果你真的想写一个游戏,那么你还有很长的路要走。
已编辑添加:
你可能需要Thread.sleep()而不是sleep - 我不是一个java人,而是this might help you。
此时你真的应该阅读一些java教程。