Random rn = new Random();
int answer = rn.nextInt(3251) + 3000;
Random rn = new Random();
int answer = rn.nextInt(3251) + 3000;
Random rn = new Random();
int answer = rn.nextInt(3251) + 3000;
import javax.swing.JFrame; import java.awt.*; public class Main { public static void main(String[] args) { Spel spela = new Spel(); JFrame A = new JFrame(); A.setBounds(10, 10, 700, 600); A.setTitle("BreakOut Game"); A.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); A.setVisible(true); A.setResizable(false); A.add(spela); } }
import javax.swing.JPanel; import java.awt.Rectangle; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.*; import javax.swing.Timer; import javax.swing.*; public class Spel extends JPanel implements KeyListener, ActionListener { private boolean play = false; private int score = 0; private int TotalBrics =21; private Timer A; public int map[][]; public int brickheight; public int brickwidth; private int delay = 8; private int delay2 = 15; private int liv = 3; private int SliderX = 310; private int BallX = 120; private int BallY = 350; private int VelX = -1; private int VelY = 2; JLabel AntalLiv = new JLabel(); private Spelplan skapabrickor;//Deklarerar en variabel som används för anropa klassen Spelplan public Spel(){ skapabrickor = new Spelplan(); //Anropar klassen spelplan så funktioner från denna klass kan köras. addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(true); Timer A = new Timer(delay, this); A.start(); ActionListener UppdateraSuccesivt = new ActionListener() { public void actionPerformed(ActionEvent evt) { Uppdatera(); // uppdaterar x och y-koordinater repaint(); // Refresh the JFrame } }; new Timer(delay2, UppdateraSuccesivt).start(); } public void paint(Graphics g) { //Bakgrund g.setColor(Color.BLACK); g.fillRect(1, 1, 692, 592); //Spelbrickor skapabrickor.draw((Graphics) g); //Måste användas så att Spelbrickor skrivs ut i JFrame! //SidoLinjer g.setColor(Color.YELLOW); g.fillRect(0, 0, 3, 592); g.fillRect(0, 0, 692, 3); g.fillRect(691, 0, 3, 592); //Slagträ g.setColor(Color.GREEN); g.fillRect(SliderX, 550, 100, 8); //Boll g.setColor(Color.RED); g.fillOval(BallX, BallY, 25, 25); g.dispose(); } public void Uppdatera() { if(new Rectangle(BallX, BallY, 25, 25).intersects(new Rectangle(SliderX, 550, 100, 8))) { VelY = -VelY; } BallX += VelX; BallY += VelY; if(BallX > 565 || BallX <0) { VelX = -VelX; } if(BallY < 0) { VelY = -VelY; } if(BallY > 665) { liv = liv-1; BallX = 250; BallY = 250; if(liv==0) { System.exit(0); } } } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_RIGHT) { if(SliderX >= 600) { SliderX = 600; } else { MoveRight(); } } else if(e.getKeyCode() == KeyEvent.VK_LEFT) { if(SliderX < 10) { SliderX = 10; } else { MoveLeft(); } } } public void MoveRight() { play = true; SliderX +=20; repaint(); } public void MoveLeft() { play = true; SliderX -=20; repaint(); } @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) { }
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class Spelplan { int u = 0; int startX = 100; int startY = 50; // Rectangle [][] spelbrickor = new Rectangle[5][2]; int poang = 5; int width = 50; int height = 25; int spacing = 2; //Genererar flera stycken spelbrickor, totalt row*col public void draw(Graphics g) { for (int row = 0; row < 2; row++) { int y = startY + (row * (height + spacing)); for (int col = 0; col < 5; col++) { int x = startX + (col * (width + spacing)); g.setColor(Color.PINK); g.fillRect(x, y, width, height); g.setColor(Color.WHITE); // g.setFont(font); g.drawString(String.valueOf(poang), x+25, y+20); // spelbrickor[col][row] = g; } } } }
import javax.swing.JFrame; import java.awt.*; public class Main { public static void main(String[] args) { Spel spela = new Spel(); JFrame A = new JFrame(); A.setBounds(10, 10, 700, 600); A.setTitle("BreakOut Game"); A.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); A.setVisible(true); A.setResizable(false); A.add(spela); } }
import javax.swing.JPanel; import java.awt.Rectangle; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.*; import javax.swing.Timer; import javax.swing.*; public class Spel extends JPanel implements KeyListener, ActionListener { private boolean play = false; private int score = 0; private int TotalBrics =21; private Timer A; public int map[][]; public int brickheight; public int brickwidth; private int delay = 8; private int delay2 = 15; private int liv = 3; private int SliderX = 310; private int BallX = 120; private int BallY = 350; private int VelX = -1; private int VelY = 2; JLabel AntalLiv = new JLabel(); private Spelplan skapabrickor;//Deklarerar en variabel som används för anropa klassen Spelplan public Spel(){ skapabrickor = new Spelplan(); //Anropar klassen spelplan så funktioner från denna klass kan köras. addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(true); Timer A = new Timer(delay, this); A.start(); ActionListener UppdateraSuccesivt = new ActionListener() { public void actionPerformed(ActionEvent evt) { Uppdatera(); // uppdaterar x och y-koordinater repaint(); // Refresh the JFrame } }; new Timer(delay2, UppdateraSuccesivt).start(); } public void paint(Graphics g) { //Bakgrund g.setColor(Color.BLACK); g.fillRect(1, 1, 692, 592); //Spelbrickor skapabrickor.draw((Graphics) g); //Måste användas så att Spelbrickor skrivs ut i JFrame! //SidoLinjer g.setColor(Color.YELLOW); g.fillRect(0, 0, 3, 592); g.fillRect(0, 0, 692, 3); g.fillRect(691, 0, 3, 592); //Slagträ g.setColor(Color.GREEN); g.fillRect(SliderX, 550, 100, 8); //Boll g.setColor(Color.RED); g.fillOval(BallX, BallY, 25, 25); g.dispose(); } public void Uppdatera() { if(new Rectangle(BallX, BallY, 25, 25).intersects(new Rectangle(SliderX, 550, 100, 8))) { VelY = -VelY; } BallX += VelX; BallY += VelY; if(BallX > 565 || BallX <0) { VelX = -VelX; } if(BallY < 0) { VelY = -VelY; } if(BallY > 665) { liv = liv-1; BallX = 250; BallY = 250; if(liv==0) { System.exit(0); } } } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_RIGHT) { if(SliderX >= 600) { SliderX = 600; } else { MoveRight(); } } else if(e.getKeyCode() == KeyEvent.VK_LEFT) { if(SliderX < 10) { SliderX = 10; } else { MoveLeft(); } } } public void MoveRight() { play = true; SliderX +=20; repaint(); } public void MoveLeft() { play = true; SliderX -=20; repaint(); } @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) { }
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class Spelplan { int u = 0; int startX = 100; int startY = 50; // Rectangle [][] spelbrickor = new Rectangle[5][2]; int poang = 5; int width = 50; int height = 25; int spacing = 2; //Genererar flera stycken spelbrickor, totalt row*col public void draw(Graphics g) { for (int row = 0; row < 2; row++) { int y = startY + (row * (height + spacing)); for (int col = 0; col < 5; col++) { int x = startX + (col * (width + spacing)); g.setColor(Color.PINK); g.fillRect(x, y, width, height); g.setColor(Color.WHITE); // g.setFont(font); g.drawString(String.valueOf(poang), x+25, y+20); // spelbrickor[col][row] = g; } } } }
Random rn = new Random();
int answer = rn.nextInt(3251) + 3000;
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106