Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gametest.java;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- @SuppressWarnings("serial")
- public class GameTest extends JPanel {
- static boolean stopp = true;
- static String key = "";
- static int x = 0;
- static int y = 0;
- static boolean directionX = false; // false = left true = right;
- static boolean directionY = false; // false = upp true = down;
- static char direction = ' ';
- static final int ballSize = 40; // CHANGABLE VARIABLE only *5 tho, so like 5, 10, 15, 20, 25, 30, 35 etc.
- static int frames = 0;
- static int touches = 0;
- static int pixelsMoved = 5;
- static int pixels = 0;
- static int xLast = 0;
- static int xCurrent = 0;
- static int yLast = 0;
- static int yCurrent = 0;
- static int[] pixel;
- static final int WIDTH = 1280;
- static final int HEIGHT = WIDTH / 16 * 9; // * (16/9) for higher HEIGHT || * (9/16) for longer WIDTH
- static int ballWIDTH = WIDTH - (16 + ballSize); // Constant and so the ball is hit on the upper left corner
- static int ballHEIGHT = HEIGHT - (38 + ballSize); // Constant and so the ball is hit on the upper left corner
- static final int[] xMonster = {400, 70, 510, 50}; // Only ending in a 0 or 5*
- static final int[] yMonster = {230, 120, 620, 170}; // Only ending in a 0 or 5*
- static final int monsterSize = ballSize;
- static final int monsters = 4;
- boolean touched = false;
- boolean xMAXED = false;
- boolean yMAXED = false;
- static boolean alive = true;
- static boolean monsterStatus = true; // FALSE = DEAD TRUE = ALIVE
- static boolean xCollisionN = false;
- static boolean xCollisionP = false;
- static boolean yCollisionN = false;
- static boolean yCollisionP = false;
- private void moveBall() {
- switch (key) {
- case "Upp":
- directionY = false;
- direction = 'Y';
- break;
- case "Nedpil":
- directionY = true;
- direction = 'Y';
- break;
- case "Vänster":
- directionX = false;
- direction = 'X';
- break;
- case "Höger":
- directionX = true;
- direction = 'X';
- break;
- }
- if(key.equals("Upp") || key.equals("Nedpil") || key.equals("Vänster") || key.equals("Höger")) {
- if(direction == 'X' ) {
- if(directionX == false) {
- if(xCollisionP == false) {
- x -= pixelsMoved;
- } else {
- xCollisionN = false;
- }
- } else if(directionX == true) {
- if(xCollisionN == false) {
- x += pixelsMoved;
- } else {
- xCollisionP = false;
- }
- }
- }
- if(direction == 'Y') {
- if(directionY == false) {
- if(yCollisionP == false) {
- y -= pixelsMoved;
- } else {
- yCollisionN = false;
- }
- } else if(directionY == true) {
- if(yCollisionN == false) {
- y += pixelsMoved;
- } else {
- yCollisionP = false;
- }
- }
- }
- }
- if(x + ballSize < xMonster[0]) {
- System.out.println(x);
- xCollisionN = false;
- } else if(x + ballSize > xMonster[0] - 5 && x + ballSize <= xMonster[0] - 5 + monsterSize){
- System.out.println(x);
- xCollisionN = true;
- }
- if(xMonster[0] + monsterSize < x - 5) {
- System.out.println(x);
- xCollisionP = false;
- } else if(xMonster[0] + monsterSize > x - 5 && xMonster[0] + monsterSize <= x - 5 + ballSize){
- System.out.println(x);
- xCollisionP = true;
- }
- if(y + ballSize < yMonster[0]) {
- System.out.println(y);
- yCollisionN = false;
- } else if(y + ballSize > yMonster[0] - 5 && y + ballSize <= yMonster[0] - 5 + monsterSize){
- System.out.println(y);
- yCollisionN = true;
- }
- if(yMonster[0] + monsterSize < y - 5) {
- System.out.println(y);
- yCollisionP = false;
- } else if(yMonster[0] + monsterSize > y - 5 && yMonster[0] + monsterSize <= y - 5 + ballSize){
- System.out.println(y);
- yCollisionP = true;
- }
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- // BACKGROUND
- g2d.setColor(java.awt.Color.LIGHT_GRAY);
- g2d.fillRect(0,0, WIDTH, HEIGHT);
- // PLAYER
- g2d.setColor(java.awt.Color.RED);
- g2d.fillRect(x, y, ballSize, ballSize);
- // MONSTER
- if(monsterStatus == true) {
- g2d.setColor(java.awt.Color.BLACK);
- g2d.fillRect(xMonster[0],yMonster[0], monsterSize, monsterSize);
- }
- }
- public GameTest() {
- KeyListener listener = new MyKeyListener();
- addKeyListener(listener);
- setFocusable(true);
- }
- public class MyKeyListener implements KeyListener {
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- key = KeyEvent.getKeyText(e.getKeyCode());
- stopp = false;
- }
- @Override
- public void keyReleased(KeyEvent e) {
- stopp = true;
- }
- }
- public static void start() throws InterruptedException {
- //Creating JFrame and the frame
- JFrame frame = new JFrame("Sample Frame");
- GameTest game = new GameTest();
- frame.add(game);
- frame.setSize(WIDTH, HEIGHT);
- frame.setVisible(true);
- frame.setResizable(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Update loop
- while (true) {
- if(stopp == false) {
- game.moveBall();
- }
- game.repaint();
- Thread.sleep(8);
- }
- }
- public static void main(String[] args) throws InterruptedException {
- start();
- } // END OF MAIN
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement