Skip to content
Snippets Groups Projects
Commit 6e3a5a24 authored by c7h-alb-u1's avatar c7h-alb-u1
Browse files

Update src/GameFrame.java, src/GameObject.java, src/GamePanel.java,...

Update src/GameFrame.java, src/GameObject.java, src/GamePanel.java, src/Main.java, src/Walls.java, out/MazeGame.jar files
parent d44b67dd
Branches
No related tags found
No related merge requests found
File added
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
GamePanel game;
JPanel score;
JPanel start;
JButton resetButton;
JButton startGame;
public GameFrame(int width, int height, String title) {
this.setTitle(title);
this.setSize(width, height);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
resetButton = new JButton();
resetButton.setText("Reset");
resetButton.setSize(100,30);
resetButton.addActionListener(this);
resetButton.setFocusable(false);
startGame = new JButton();
startGame.setText("Start");
startGame.setSize(200,200);
startGame.setLocation(250,250);
startGame.addActionListener(this);
startGame.setFocusable(false);
score = new JPanel();
score.setBackground(Color.black);
score.add(resetButton);
score.setFocusable(true);
start = new JPanel();
start.setBackground(Color.black);
start.setSize(getWidth(),getHeight());
start.setLocation(0,0);
start.add(startGame);
this.add(start);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == resetButton){
this.remove(game);
game = new GamePanel();
this.add(game);
SwingUtilities.updateComponentTreeUI(this);
game.requestFocus();
}
if(e.getSource() == startGame){
this.remove(start);
game = new GamePanel();
this.add(score,BorderLayout.NORTH);
this.add(game);
SwingUtilities.updateComponentTreeUI(this);
game.requestFocus();
}
}
}
import java.awt.*;
import java.awt.event.*;
public class GameObject extends Rectangle{
Color color;
GameObject(int x, int y, int width, int height, Color color){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.color=color;
}
public void draw(Graphics g) {
g.setColor(this.color);
g.fillRect(this.x, this.y, this.width, this.height);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.TimeUnit;
public class GamePanel extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
Timer timer = new Timer(5, this);
int x = 0, y = 0, velX = 0, velY = 0, veldX = 0, veldY = 0;
int playerSize = 22;
int movmentSpeed = 4;
int faildRounds;
ImageIcon ggwp;
private Walls finishWall;
private Walls walls;
private boolean play = true;
public GamePanel() {
timer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
walls = new Walls();
ggwp = new ImageIcon("res\\ggwp.png");
this.setSize(817,840);
this.setLocation(100,0);
this.requestFocus(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0,0,getWidth(),getWidth());
//Walls
walls.draw(this,g);
//Player
if(play) {
g.setColor(Color.red);
g.fillOval(x, y, playerSize, playerSize);
}
if(walls.wallsCollision(x,y,playerSize,playerSize)){
x = 0;
y=0;
}
if(walls.finishWallsCollision(x,y,playerSize,playerSize)){
play=false;
}
if(!play) {
gameOver(g);
}
}
public void gameOver(Graphics g) {
if(!play){
g.setColor(Color.black);
g.fillRect(0,0,getWidth(),getHeight());
ggwp.paintIcon(this,g,280,250);
faildRounds++;
}
}
public int getFailedRounds(){
return faildRounds;
}
@Override
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if(play) {
if (c == KeyEvent.VK_LEFT || c == KeyEvent.VK_A)
veldX = -movmentSpeed;
if (c == KeyEvent.VK_RIGHT || c == KeyEvent.VK_D)
velX = movmentSpeed;
if (c == KeyEvent.VK_UP || c == KeyEvent.VK_W)
veldY = -movmentSpeed;
if (c == KeyEvent.VK_DOWN || c == KeyEvent.VK_S)
velY = movmentSpeed;
}
}
@Override
public void keyReleased(KeyEvent e) {
int c = e.getKeyCode();
if(play) {
if (c == KeyEvent.VK_LEFT || c == KeyEvent.VK_A)
veldX = 0;
if (c == KeyEvent.VK_RIGHT || c == KeyEvent.VK_D)
velX = 0;
if (c == KeyEvent.VK_UP || c == KeyEvent.VK_W)
veldY = 0;
if (c == KeyEvent.VK_DOWN || c == KeyEvent.VK_S)
velY = 0;
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (x < 0) {
velX = 0;
x = 0;
}
if (x > getWidth() - playerSize) {
velX = 0;
x = getWidth() - playerSize;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > getHeight() - playerSize) {
velY = 0;
y = getHeight() - playerSize;
}
x += velX + veldX;
y += velY + veldY;
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
}
public class Main {
public static void main(String[] args) {
new GameFrame(820, 875, "Maze Game");
}
}
import java.awt.*;
public class Walls {
public int unit_size = 50;
int[] wallXPos = {
//upper wall
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
//left wall
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
//down wall
1,2,3,4,5,6,7,8,9,10,11,12,13,
//right wall
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
//rest
1,1,1,1,
2,2,2,2,
3,3,3,3,3,
4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,
6,6,6,6,
7,7,7,7,7,7,7,
8,8,8,8,8,8,
9,9,9,9,9,9,9,9,
10,10,10,10,10,
11,11,11,11,11,11,11,
12,12,12,12,
13,13,13,
14,14,14
};
int[] wallYPos = {
//upper wall
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
//left wall
2,3,4,5,6,7,8,9,10,11,12,13,14,15,
//down wall
15,15,15,15,15,15,15,15,15,15,15,15,15,
//right wall
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
//rest
//1
3,8,9,12,
//2
1,3,6,12,
//3
3,6,7,10,14,
//4
2,3,7,8,10,12,
//5
2,3,4,8,9,10,11,12,13,
//6
4,8,9,13,
//7
1,2,4,6,7,8,11,
//8
4,8,11,12,13,14,
//9
1,2,4,5,6,8,10,11,
//10
1,4,8,11,13,
//11
4,6,7,8,9,11,13,
//12
8,11,13,4,
//13
8,11,13,
//14
3,8,13
};
public Walls() {
}
public void draw(Component c, Graphics g) {
for(int i=0; i< wallXPos.length;i++)
{
g.setColor(Color.GRAY);
g.fillRect(wallXPos[i]*unit_size,wallYPos[i]*unit_size,unit_size,unit_size);
}
g.setColor(Color.yellow);
g.fillRect(14*unit_size,15*unit_size,2*unit_size,unit_size);
}
public boolean wallsCollision(int x, int y,int width,int height)
{
boolean collided = false;
for(int i=0; i< wallXPos.length;i++)
{
if(new Rectangle(x, y, width, height).intersects(new Rectangle(wallXPos[i]*unit_size, wallYPos[i]*unit_size, 50, 50)))
{
collided = true;
break;
}
}
return collided;
}
public boolean finishWallsCollision(int x, int y,int width,int height)
{
boolean finishCollided = false;
if(new Rectangle(x, y, width, height).intersects(new Rectangle(14*unit_size, 15*unit_size, 50, 50)))
{
finishCollided = true;
}
return finishCollided;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment