Created
June 10, 2023 10:24
-
-
Save R3DHULK/7ff9a79c6b4e2e1205fb52244c85d0af to your computer and use it in GitHub Desktop.
Fall Guys Game Basic Interface Created Using Java Swing
This file contains hidden or 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 javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
public class FallGuysGame extends JFrame implements KeyListener { | |
private int playerX; | |
private int playerY; | |
private int playerWidth; | |
private int playerHeight; | |
private int jumpHeight; | |
private boolean isJumping; | |
public FallGuysGame() { | |
setTitle("Fall Guys Game"); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setPreferredSize(new Dimension(800, 600)); | |
addKeyListener(this); | |
playerX = 350; | |
playerY = 400; | |
playerWidth = 100; | |
playerHeight = 100; | |
jumpHeight = 200; | |
isJumping = false; | |
pack(); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} | |
@Override | |
public void paint(Graphics g) { | |
super.paint(g); | |
// Draw player character | |
g.setColor(Color.BLUE); | |
g.fillRect(playerX, playerY, playerWidth, playerHeight); | |
// Draw ground | |
g.setColor(Color.GREEN); | |
g.fillRect(0, 500, getWidth(), 100); | |
} | |
private void jump() { | |
if (!isJumping) { | |
isJumping = true; | |
Thread jumpThread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
int startY = playerY; | |
int targetY = playerY - jumpHeight; | |
int stepSize = 10; | |
int delay = 20; | |
for (int y = startY; y >= targetY; y -= stepSize) { | |
playerY = y; | |
repaint(); | |
try { | |
Thread.sleep(delay); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
for (int y = targetY; y <= startY; y += stepSize) { | |
playerY = y; | |
repaint(); | |
try { | |
Thread.sleep(delay); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
playerY = startY; | |
isJumping = false; | |
repaint(); | |
} | |
}); | |
jumpThread.start(); | |
} | |
} | |
@Override | |
public void keyTyped(KeyEvent e) { | |
// Not used | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
int keyCode = e.getKeyCode(); | |
if (keyCode == KeyEvent.VK_SPACE) { | |
jump(); | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
// Not used | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
FallGuysGame game = new FallGuysGame(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment