所以我正在创建一个非常简单的平台游戏,我想让平台随机生成,以便每个游戏都是独一无二的。但是,我不确定如何完美地做到这一点。我设法得到一些实际生成的代码,实际上在屏幕上生成随机矩形,但我不知道如何制作它以便“角色”可以在它们上移动而不会掉进去和什么不是。到目前为止,这是我的所有代码。现在它并不漂亮,但是当我知道一切正常时,我会把它搞定。在此先感谢您的帮助:)
P.S。我还评论了与随机矩形相关的东西,因为它们完全填满了屏幕,我看不到发生了什么:P
Game.java
public class Game extends JPanel implements Runnable {
public Rectangle character, floor;
public int characterWidth = 24;
public int characterHeight = 36;
public int fps = 1000; //setting framerate
public int fallingSpeed = 1;
public int fallingFrame = 0;
public int floorHeight = 80;
public int movementSpeed = 1;
public int movementFrame = -1;
public int movementFallingSpeed = 10;
public int movementResetSpeed = 1;
public int jumpingLength = 150; //# of pixels
public int jumpingFrame = 1;
public int jumpingCountFrame = 0;
public int jumpingCountSpeed = fallingSpeed;
public int xs = 0;
public int ys = 0;
public int keyJump = KeyEvent.VK_SPACE;
public int keyLeft = KeyEvent.VK_A;
public int keyRight = KeyEvent.VK_D;
public boolean objectsDefined = false;
public boolean falling = false;
public boolean running = true;
public boolean right = false;
public boolean left = false;
public boolean jumping = true;
public Thread game;
RandomRects ad[];
Random rand = new Random();
public Game(Frame f){
setBackground(Color.black);
defineObjects();
game = new Thread(this);
game.start();
f.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == keyLeft ){
left = true;
}
if (e.getKeyCode() == keyRight){
right = true;
}
if (e.getKeyCode() == keyJump){
if (!falling){
jumping = true;
}
}
}
public void keyReleased(KeyEvent e){
if (e.getKeyCode() == keyLeft ){
left = false;
}
if (e.getKeyCode() == keyRight){
right = false;
}
}
});
}
public void defineObjects(){
character = new Rectangle((Main.width/2) - (characterWidth/2), (Main.height/2) - (characterHeight/2), characterWidth, characterHeight);
floor = new Rectangle(-10, Main.height - floorHeight, Main.width + 10, floorHeight);
ad = new RandomRects[rand.nextInt(200)];
/*for(int count=0; count< ad.length; count++){
int posX1=rand.nextInt(400);
int posY1=rand.nextInt(400);
int posX2=rand.nextInt(400);
int posY2=rand.nextInt(400);
Color rectColor= new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
ad[count] = new ad(posX1, posX2, posY1, posY2, rectColor);
}*/
objectsDefined = true;
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (objectsDefined){
g.setColor(Color.WHITE);
g.fill3DRect(character.x - xs, character.y - ys, character.width, character.height, true);
g.fill3DRect(floor.x -xs , floor.y - ys, floor.width, floor.height, true);
}
/*for(int count=0; count<ad.length; count++){
ad[count].draw (g);
}*/
}
public void run() {
while (running){
//Character feet
Point pt1 = new Point(character.x, character.y + character.height);
Point pt2 = new Point(character.x + character.width, character.y + character.height);
//Falling
if (!jumping){
if (fallingFrame >= fallingSpeed){
if (floor.contains(pt1) || floor.contains(pt2)){
falling = false;
}else{
character.y += 1;
falling = true;
}
if (falling){
character.y += 1;
//ys+=1;
}
fallingFrame = 0;
}else{
fallingFrame += 1;
}
}
//Jumping
if (jumpingCountFrame >= jumpingCountSpeed){
if (jumping){
if (jumpingFrame <= jumpingLength){
character.y -= 1;
//ys -=1;
jumpingFrame += 1;
}else{
jumpingFrame = 0;
jumping = false;
}
}
jumpingCountFrame = 0;
}else{
jumpingCountFrame += 1;
}
//Movement Speed Check
if (falling){
movementSpeed = movementFallingSpeed;
}else{
movementFrame += 1;
}
//Movement
if(movementFrame >= movementSpeed){
if (right){
character.x += 1;
xs +=1;
}
if (left){
character.x -= 1;
xs-=1;
}
movementFrame = -1;
}else{
movementFrame += 1;
}
fpsSetter();
repaint();
}
}
@SuppressWarnings("static-access")
public void fpsSetter(){
try{
game.sleep(fps/1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
RandomRects.java
public class RandomRects {
private int posX1;
private int posY1;
private int posX2;
private int posY2;
private Color rectColor;
public RandomRects(int posX1, int posX2, int posY1, int posY2, Color color){
this.posX1 = posX1;
this.posX2 = posX2;
this.posY1 = posY1;
this.posY2 = posY2;
this.rectColor = color;
}
public void draw (Graphics g){
g.setColor(rectColor);
g.fillRect(posX1,posY1,posX2,posY2);
}
}
答案 0 :(得分:0)
基本上,你需要的是“想象”移动角色;如果移动的字符与任何矩形重叠(“碰撞检测”),则阻止移动。如果没有,则确认角色的下一个位置是移动的角色。您可以使用Rectangle
方法intersects()
。