浏览代码

Added info screen

jancoow 10 年之前
父节点
当前提交
ceb23e8a84

+ 2 - 1
src/main/Window.java

@@ -7,10 +7,11 @@ import model.GameStateManager;
 import view.GameView;
 import control.GameControl;
 
+@SuppressWarnings("serial")
 public class Window extends JFrame {
 	
 	public static final int WIDTH = 800;
-	public static final int HEIGHT = 700;
+	public static final int HEIGHT = 800;
 	
 	public static final int UPDATES_PER_SECOND = 30;
 	public static final int FRAMES_PER_SECOND = 30;

+ 49 - 0
src/model/InfoPanel.java

@@ -0,0 +1,49 @@
+package model;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Graphics2D;
+
+import resources.image.Images;
+import resources.image.Images.ImageType;
+import main.Window;
+import model.objects.Player;
+
+public class InfoPanel {
+
+	private Player player1, player2;
+		
+	public InfoPanel(Player p1, Player p2){
+		player1 = p1;
+		player2 = p2;
+	}
+	
+	public void paint(Graphics2D g2d)
+	{
+		g2d.setColor(Color.black);
+		g2d.setFont(new Font("Century Schoolbook L", Font.ROMAN_BASELINE, 27));
+		
+		//Player 1
+		g2d.drawString(player1.getName(), 70, 648);
+		g2d.drawString(player1.getScore() + " xp", 70, 673);
+		for(int i = 0; i < player1.getHealth(); i++){
+			g2d.drawImage(Images.getImage(ImageType.HEARTH), null, 68+i*24,680);
+		}
+		g2d.drawString("<Powerups>", 70, 720);
+		
+		//Chat
+		g2d.drawLine(230, 600, 230, Window.HEIGHT);
+		g2d.drawString("hi!", 240, 630);
+		g2d.drawString("hello!", 460, 670);
+		g2d.drawLine(540, 600, 540, Window.HEIGHT);
+		
+		//Player 2
+		g2d.drawString(player2.getName(),560, 648);
+		g2d.drawString(player2.getScore() + " xp", 560, 673);
+		for(int i = 0; i < player2.getHealth(); i++){
+			g2d.drawImage(Images.getImage(ImageType.HEARTH), null, 558+i*24,680);
+		}
+		g2d.drawString("<Powerups>", 560, 720);
+	}
+	
+}

+ 19 - 1
src/model/objects/Player.java

@@ -11,11 +11,14 @@ public class Player extends DrawObject {
 	private long lastMovement;
 	private int lastDirection, frame;
 	private BufferedImage spriteimage;
+	private String name;
 	
-	public Player(BufferedImage image, int x, int y) {
+	public Player(BufferedImage image, int x, int y, String name) {
 		super(image.getSubimage(38, 0, 40, 54));
 		super.setPosition(x, y);
+		this.name = name;
 		spriteimage = image;
+		health = 5;
 		powerups = new ArrayList<PowerUp>();
 	}
 	
@@ -41,4 +44,19 @@ public class Player extends DrawObject {
 		lastDirection = 2;
 		lastMovement = System.currentTimeMillis();
 	}
+	
+	//** Getters and Setters **//
+	
+	public String getName(){
+		return name;
+	}
+
+	public int getScore() {
+		return score;
+	}
+
+	public int getHealth() {
+		return health;
+	}	
+	
 }

+ 0 - 1
src/model/objects/ShootingLine.java

@@ -1,7 +1,6 @@
 package model.objects;
 
 import java.awt.Graphics2D;
-import java.awt.Shape;
 import java.awt.geom.Path2D;
 
 public class ShootingLine {

+ 10 - 9
src/model/state/PlayState.java

@@ -8,6 +8,7 @@ import java.util.ListIterator;
 
 import main.Window;
 import model.GameStateManager;
+import model.InfoPanel;
 import model.objects.Ball;
 import model.objects.Player;
 import model.objects.ShootingLine;
@@ -19,12 +20,13 @@ public class PlayState extends State{
 	private Player player;
 	private ShootingLine line;
 	private ArrayList<Ball> balls;
+	private InfoPanel infopanel;
 	private int direction;
 	
 	public PlayState(GameStateManager gsm) {
 		super("play", gsm);
-		
-		player = new Player(Images.getImage(ImageType.PLAYER1), 40, 550);
+		player = new Player(Images.getImage(ImageType.PLAYER1), 40, 550, "Janco");
+		infopanel = new InfoPanel(player, player);
 		balls = new ArrayList<Ball>();
 		balls.add(new Ball(5, 400, Color.red, 300, 50,-1));
 	}
@@ -61,34 +63,32 @@ public class PlayState extends State{
 		player.update();
 		if(line != null){
 			line.setLength(line.getLength()+1);
-			if(line.getMaxHeight() < 0){ 		//Line hit the ceiling, so "remove" it
+			if(line.getMaxHeight() < 0){ 															//Line hit the ceiling, so "remove" it
 				line = null;
 			}
 		}
 		
-		//collision detection
+		//Collision detection
 		ListIterator<Ball> b = balls.listIterator(); 
 		while(b.hasNext()){
 			Ball bal = b.next(); 
-			if(line != null && bal.hitLine(line)){ // line - ball
+			if(line != null && bal.hitLine(line)){ 													// Collision between line and ball
 				line = null;
 				b.remove();
 				b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), -1));
 				b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), 1));
-			}else if(bal.hitPlayer(player)){
+			}else if(bal.hitPlayer(player)){														// Collision between player and ball
 				try {
 					throw new Exception("YOU DIED MOTHERFUCKER");
 				} catch (Exception e) {
 					e.printStackTrace();
 				}
-			}else if(bal.getSize() <= 0 ){
+			}else if(bal.getSize() <= 0 ){															// Remove ball from arraylist when size is 0 (not visible)
 				b.remove();
 			}else{
 				bal.update();
 			}
 		}
-		
-		
 	}
 
 	@Override
@@ -103,6 +103,7 @@ public class PlayState extends State{
 			b.paint(g2d);
 		}
 		player.paint(g2d);
+		infopanel.paint(g2d);
 	}
 
 	

+ 3 - 1
src/resources/image/Images.java

@@ -19,6 +19,7 @@ public class Images {
 			images.add(ImageIO.read(Main.class.getResource("/resources/image/player1_sprite.png"))); 
 			images.add(ImageIO.read(Main.class.getResource("/resources/image/player2.png"))); 
 			images.add(ImageIO.read(Main.class.getResource("/resources/image/background.png")));
+			images.add(ImageIO.read(Main.class.getResource("/resources/image/hearth.png")));
 		}catch(IOException e){
 			e.printStackTrace();
 		}
@@ -33,6 +34,7 @@ public class Images {
 	 {
 		 PLAYER1,
 		 PLAYER2,
-		 BACKGROUND
+		 BACKGROUND,
+		 HEARTH
 	 }
 }

二进制
src/resources/image/hearth.png


+ 2 - 6
src/view/GameView.java

@@ -14,6 +14,7 @@ import main.Window;
 import model.GameModel;
 import model.GameStateManager;
 
+@SuppressWarnings("serial")
 public class GameView extends JPanel implements ActionListener{
 	
 	private GameModel model;
@@ -22,7 +23,6 @@ public class GameView extends JPanel implements ActionListener{
 	
 	public GameView(GameModel model, GameStateManager gsm)
 	{
-		super();
 		this.model = model;
 		this.gsm = gsm;
 		
@@ -37,12 +37,8 @@ public class GameView extends JPanel implements ActionListener{
 	{
 		super.paintComponent(g);
 		Graphics2D g2d = (Graphics2D) g;
-		
-		RenderingHints rh = new RenderingHints(
-	             RenderingHints.KEY_ANTIALIASING,
-	             RenderingHints.VALUE_ANTIALIAS_ON);
+		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 	    g2d.setRenderingHints(rh);
-		
 		gsm.paint(g2d);
 	}