Mercurial > hg > pub > prymula > com
view immposiblerush/src/pietraszczyk/immposiblerush/ImmposibleRush.java @ 20:b637ac9c6605
lamertetris - PKGBUILD corrections
author | Przemyslaw <prymula76@outlook.com> |
---|---|
date | Sun, 31 Mar 2024 16:21:08 +0200 |
parents | 2787f5e749ae |
children |
line wrap: on
line source
/* Immposible Rush - gra w dobieranie koloru * Przemyslaw R. Pietraszczyk * Data: 13-09-2023 * Licencja: GPL * Edytor: Geany */ package pietraszczyk.immposiblerush; import java.awt.*; // Toolkit import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.ImageIcon; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.*; //Timer import java.util.ArrayList; import java.util.List; import java.awt.geom.AffineTransform; import java.lang.Thread; import java.beans.*; //property change stuff import java.io.BufferedReader; import java.io.InputStream; import java.io.OutputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.*; import java.awt.image.BufferedImage; import java.awt.image.*; import javax.imageio.ImageIO; import javax.imageio.*; public class ImmposibleRush extends JPanel { static JFrame frame; static ImmposibleRush immposiblerush; static Timer timer = new Timer(); static Timer timer3 = new Timer(); private Graphics bg; private Image bufor; static BufferedImage startImage = null; static BufferedImage gameOver = null; static BufferedImage colorsBalls = null; static BufferedImage board = null; static BufferedImage imgSquare = null; static BufferedImage [] numbers; static Actions actions; static private Rectangle rect; static Random genRand; //static BufferedImage [] colors static Image icon; static Ball [] ball = new Ball[5]; static Square [] square = new Square[8]; static int N = 0; static int FRAME = 0; static Color [] colors = new Color[4]; // skladowe RGB static int direction = 1; static boolean go = false; static int counter =0; static List<Integer> countscore; static int score = 0; static int hiscore; static int lives = 3; static HiScore HiScoreFile; static WAVResource wav; static boolean gameover = false; static Timer timer2 = new Timer(); //should not create timer again static long periord = 60; public ImmposibleRush (){ startImage = LoadIMG("/startscreen.png"); gameOver = LoadIMG("/gameover.png"); colorsBalls = LoadIMG("/balls.png"); imgSquare = LoadIMG("/square.png"); numbers = new BufferedImage[10]; countscore = new ArrayList<Integer>(); colors[0] = new Color(0x1c, 0x00, 0xf9); // niebieski colors[1] = new Color(0x04, 0x79, 0x01); // zielony colors[2] = new Color(0xc9, 0xdb, 0x54); // zolty colors[3] = new Color(0xf5, 0x1a, 0x1a); // czerwony icon = Toolkit.getDefaultToolkit().getImage("immposiblerush.png"); for (int i=0; i<10; i++) { String numbname = "/"+i+".png"; System.out.println("NUMER: "+numbname); numbers[i]=LoadIMG(numbname); } for (int i=0; i < 8; i++) { square[i] = new Square(imgSquare.getSubimage(i*100, 0, 100, 100), i); } for (int i=0; i < 4; i++) { ball[i] = new Ball(colorsBalls.getSubimage(i*50, 0, 50, 50), 0, 0); ball[i].setActive(false); } genRand = new Random(); try{ HiScoreFile = new HiScore(); } catch (IOException e) {} hiscore = Integer.valueOf(HiScoreFile.load()); wav = new WAVResource(); BoardCollisions(); } public enum InfoVar { SCORE, HISCORE, LIVES, OVER; } private void drawInfo(Graphics g, int x, int y, InfoVar scr){ String s; if (scr == InfoVar.SCORE) s = String.valueOf(score); else if (scr == InfoVar.HISCORE ) s = String.valueOf(hiscore); else if (scr == InfoVar.LIVES)s = String.valueOf(lives); else s = String.valueOf(score); char sign; int ii; if ( scr == InfoVar.OVER) x = 310/2 - 24*s.length()/2; for (int i=0; i<s.length(); i++){ sign = s.charAt(i); ii = Integer.valueOf(sign); // zwraca kod ASCII znaku ii -= 48; // odejmujemy do indeksu g.drawImage(numbers[ii], x,y, numbers[ii].getWidth(), numbers[ii].getHeight(), null); x+= numbers[ii].getWidth(); } } private static void createAndShowGUI() { frame=new JFrame("Immposible Rush"); //icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource( "/immposiblerush.png") ); frame.setIconImage(icon); final int frameWidth = 310; final int frameHeight = 590; final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds((int) screenSize.getWidth() - frameWidth, (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); actions = new Actions(); frame.addKeyListener(actions); immposiblerush = new ImmposibleRush(); frame.add(immposiblerush); immposiblerush.setBounds(5,5, 300,540); frame.setLayout(null); // potrzebne - wyposrodkowywuje obrazek frame.setVisible(true); frame.setFocusable(true); timer.schedule(new RoundTask(), 0, 5000); //timer.schedule(new Move(), 0, 50); timer3.schedule(new KeyInput(), 0, 200); runTaskMove(); } protected void paintComponent(Graphics g){ super.paintComponent(g); if (gameover){ g.drawImage(gameOver, 0, 0, gameOver.getWidth(), gameOver.getHeight(), null); drawInfo(g, 150, 320, InfoVar.OVER); } else if (!go && !gameover) { g.drawImage(startImage, 0, 0, startImage.getWidth(), startImage.getHeight(), null); } else { BoardCollisions(); g.drawImage(board, 0, 0, 310, 590, null); if (ball[N].getActive()) g.drawImage(ball[N].image, ball[N].getX(), ball[N].getY(), ball[N].image.getWidth(), ball[N].image.getHeight(), null); drawInfo(g, 15, 5, InfoVar.SCORE); // pisz punkty drawInfo(g, 200, 5, InfoVar.HISCORE); // pisz najlepszy drawInfo(g, 150, 5, InfoVar.LIVES); // pisz zycia } } static private void BoardCollisions () { board = new BufferedImage(310, 590, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = board.createGraphics(); graphics.setPaint( new Color (183,93,93)); graphics.fillRect(0, 0,board.getWidth(), board.getHeight()); graphics.drawImage(square[FRAME].image, square[FRAME].getX(), square[FRAME].getY(), square[FRAME].image.getWidth(), square[FRAME].image.getHeight(), null); graphics.dispose(); //board.flush(); } private BufferedImage LoadIMG(String file) { try { return ImageIO.read(getClass().getResourceAsStream(file)); } catch (IOException ioe) { ioe.printStackTrace(); } return null; } public static void main(final String[] args) { javax.swing.SwingUtilities.invokeLater(new RunnableImplementation()); } private static final class RunnableImplementation implements Runnable { public void run(){ createAndShowGUI(); } } static class RoundTask extends TimerTask { public void run () { if (ball[N].getActive() == false && go) { N = genRand.nextInt(4); ball[N].setActive(true); ball[N].setY(0); } } } static class KeyInput extends TimerTask { public void run () { char key; key = actions.getKey(); // Akcja if (go) { if (key == 'a' || key == 'A') { if (FRAME > 0) { --FRAME; direction = -1; } else if ( FRAME == 0) { FRAME = 7; direction = -1; } } if (key == 'd' || key == 'D') { if (FRAME < 7) { ++FRAME; direction = 1; } else if (FRAME == 7) { FRAME = 0; direction = 1; } } } // Start if (!go && key == ' '){ go = true; score = 0; lives = 3; gameover = false; countscore.clear(); for (int i=300; i<=900; i+=140) { countscore.add(i); } wav.play("/swoosh.wav"); } if (gameover && key == ' '){ gameover = false; } } } static void runTaskMove() { TimerTask task = new TimerTask() { @Override public void run() { //System.out.println("PERIOD: "+periord); if(score == 10) { this.cancel(); // cancel this task to run new task periord = 50; runTaskMove(); } else if (score == 20) { this.cancel(); // cancel this task to run new task periord = 45; runTaskMove(); } else if (score == 40) { this.cancel(); // cancel this task to run new task periord = 40; runTaskMove(); } else if (score == 60) { this.cancel(); // cancel this task to run new task periord = 35; runTaskMove(); } else if (score == 80) { this.cancel(); // cancel this task to run new task periord = 30; runTaskMove(); } if (ball[N].getActive()) { ball[N].addY(10); if (checkCollisions()) { wav.play("/point.wav"); } else{ if (ball[N].getY() > 491) { System.out.println("dno !"); wav.play("/hit.wav"); try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } ball[N].setActive(false); ball[N].setY(0); --lives; // odejmij zycie } } } if (lives < 0) { go = false; if (score > hiscore) { hiscore = score; HiScoreFile.save(String.valueOf(score)); //go = false; } gameover = true; this.cancel(); // cancel this task to run new task periord = 60;; runTaskMove(); } BoardCollisions(); immposiblerush.repaint(); } }; timer2.schedule(task, periord, periord); int countDeletedTasks = timer2.purge(); // } static private boolean checkCollisions(){ int startX = Math.max(ball[N].getX() ,0); int endX = Math.min(ball[N].getX() + ball[N].image.getWidth(), board.getWidth()); int startY = Math.max(ball[N].getY(), 0); //because Y goes from top to bottom int endY = Math.min(ball[N].getY() + ball[N].image.getHeight(), board.getHeight()); for (int y = startY; y < endY; y++) { for (int x = startX; x < endX; x++) { int c = board.getRGB(x, y); if (colors[N].equals(new Color(c))) { if (y >= 475 && y <= 477 && (FRAME % 2 == 1)) { if (colors[0].equals(colors[N])) { System.out.println("Niebieski"); ball[N].setActive(false); ++score; return true; } else if (colors[1].equals(colors[N])) { System.out.println("Zielony"); ball[N].setActive(false); ++score; return true; } else if (colors[2].equals(colors[N])) { System.out.println("zolty"); ball[N].setActive(false); ++score; return true; } else if (colors[3].equals(colors[N])) { System.out.println("czerwony"); ball[N].setActive(false); ++score; return true; } } } } } return false; } } //}