Created
April 13, 2024 04:32
-
-
Save jamesmurdza/d2baf163116875ceb9706db5f77fff9f to your computer and use it in GitHub Desktop.
Java game
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.*; | |
import javax.swing.*; | |
public class Main extends JPanel implements ActionListener, KeyListener { | |
private Timer timer; | |
private int playerX, playerY, playerVelocityY; | |
private boolean isJumping, isMovingLeft, isMovingRight; | |
private static final int PLAYER_WIDTH = 50; | |
private static final int PLAYER_HEIGHT = 50; | |
private static final int GROUND_Y = 300; | |
private static final int GRAVITY = 1; | |
private static final int PLAYER_JUMP_VELOCITY = -15; | |
private static final int PLAYER_MOVE_SPEED = 5; | |
public Main() { | |
setPreferredSize(new Dimension(500, 400)); | |
setBackground(Color.WHITE); | |
setFocusable(true); | |
addKeyListener(this); | |
playerX = 50; | |
playerY = GROUND_Y - PLAYER_HEIGHT; | |
playerVelocityY = 0; | |
timer = new Timer(15, this); | |
timer.start(); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
// Draw player | |
g.setColor(Color.BLUE); | |
g.fillRect(playerX, playerY, PLAYER_WIDTH, PLAYER_HEIGHT); | |
// Draw ground | |
g.setColor(Color.GREEN); | |
g.fillRect(0, GROUND_Y, getWidth(), getHeight() - GROUND_Y); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
// Update player position | |
if (isMovingLeft && playerX > 0) { | |
playerX -= PLAYER_MOVE_SPEED; | |
} | |
if (isMovingRight && playerX < getWidth() - PLAYER_WIDTH) { | |
playerX += PLAYER_MOVE_SPEED; | |
} | |
if (isJumping) { | |
playerVelocityY += GRAVITY; | |
playerY += playerVelocityY; | |
if (playerY >= GROUND_Y - PLAYER_HEIGHT) { | |
playerY = GROUND_Y - PLAYER_HEIGHT; | |
playerVelocityY = 0; | |
isJumping = false; | |
} | |
} | |
repaint(); | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
int key = e.getKeyCode(); | |
if (key == KeyEvent.VK_LEFT) { | |
isMovingLeft = true; | |
} | |
if (key == KeyEvent.VK_RIGHT) { | |
isMovingRight = true; | |
} | |
if (key == KeyEvent.VK_SPACE && !isJumping) { | |
isJumping = true; | |
playerVelocityY = PLAYER_JUMP_VELOCITY; | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
int key = e.getKeyCode(); | |
if (key == KeyEvent.VK_LEFT) { | |
isMovingLeft = false; | |
} | |
if (key == KeyEvent.VK_RIGHT) { | |
isMovingRight = false; | |
} | |
} | |
@Override | |
public void keyTyped(KeyEvent e) {} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(() -> { | |
JFrame frame = new JFrame("Simple Platformer"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.getContentPane().add(new Main()); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment