Commit 598bed0a authored by 9731301's avatar 9731301

add walls

parent e4e29e50
......@@ -2,20 +2,13 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/UI/WallState.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/Server_Client/Server.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/Server_Client/Server.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState$KeyHandler.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState$KeyHandler.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState$MouseHandler.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState$MouseHandler.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Main.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Main.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainFrame.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainFrame.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel$3.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel$3.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MainInitialPanel.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pics/tankBody_blue_outline.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/WallState.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/WallState.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameLoop.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/MainFrame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/WallState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/WallState.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -92,7 +85,10 @@
<workItem from="1595519390600" duration="8731000" />
<workItem from="1595580635762" duration="18427000" />
<workItem from="1595610264637" duration="6164000" />
<workItem from="1595631601242" duration="3984000" />
<workItem from="1595631601242" duration="7682000" />
<workItem from="1595671067716" duration="13031000" />
<workItem from="1595695509035" duration="23884000" />
<workItem from="1595724202134" duration="6601000" />
</task>
<servers />
</component>
......@@ -112,30 +108,34 @@
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1593430104059" />
<state x="593" y="0" key="CommitChangelistDialog2" timestamp="1595526973946">
<state x="593" y="0" key="CommitChangelistDialog2" timestamp="1595639194593">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="593" y="0" key="CommitChangelistDialog2/0.0.1536.824@0.0.1536.824" timestamp="1595526973946" />
<state width="1493" height="120" key="GridCell.Tab.0.bottom" timestamp="1595635273015">
<state x="593" y="0" key="CommitChangelistDialog2/0.0.1536.824@0.0.1536.824" timestamp="1595639194593" />
<state width="1493" height="118" key="GridCell.Tab.0.bottom" timestamp="1595730990190">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="120" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1595635273014" />
<state width="1493" height="120" key="GridCell.Tab.0.center" timestamp="1595635273013">
<state width="1493" height="118" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1595730990190" />
<state width="1493" height="118" key="GridCell.Tab.0.center" timestamp="1595730990190">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="120" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1595635273013" />
<state width="1493" height="120" key="GridCell.Tab.0.left" timestamp="1595635273013">
<state width="1493" height="118" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1595730990190" />
<state width="1493" height="118" key="GridCell.Tab.0.left" timestamp="1595730990190">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="120" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1595635273013" />
<state width="1493" height="120" key="GridCell.Tab.0.right" timestamp="1595635273014">
<state width="1493" height="118" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1595730990190" />
<state width="1493" height="118" key="GridCell.Tab.0.right" timestamp="1595730990190">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="120" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1595635273014" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1595611936192">
<state width="1493" height="118" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1595730990190" />
<state x="589" y="114" key="RollbackChangesDialog" timestamp="1595702094731">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1595611936192" />
<state x="589" y="114" key="RollbackChangesDialog/0.0.1536.824@0.0.1536.824" timestamp="1595702094731" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1595724201885">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1595724201885" />
<state x="209" y="33" key="new project wizard" timestamp="1593723788909">
<screen x="0" y="0" width="1536" height="824" />
</state>
......@@ -144,13 +144,13 @@
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="511" y="41" key="refactoring.ChangeSignatureDialog/0.0.1536.824@0.0.1536.824" timestamp="1595279815795" />
<state x="426" y="187" key="run.anything.popup" timestamp="1595584056607">
<state x="426" y="187" key="run.anything.popup" timestamp="1595636897284">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="426" y="187" key="run.anything.popup/0.0.1536.824@0.0.1536.824" timestamp="1595584056607" />
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1595612989869">
<state x="426" y="187" key="run.anything.popup/0.0.1536.824@0.0.1536.824" timestamp="1595636897284" />
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1595684580506">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1595612989869" />
<state x="431" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1595684580506" />
</component>
</project>
\ No newline at end of file
1111111
1000001
1010001
1111111
\ No newline at end of file
1111111
1001001
1001011
1001001
1010001
1001011
1001001
1111111
\ No newline at end of file
......@@ -36,7 +36,7 @@ public class GameLoop implements Runnable {
*/
public void init() {
// Perform all initializations ...
state = new GameState();
state = new GameState(canvas.wallState);
canvas.addKeyListener(state.getKeyListener());
canvas.addMouseListener(state.getMouseListener());
canvas.addMouseMotionListener(state.getMouseMotionListener());
......
......@@ -23,9 +23,10 @@ public class GameState {
private int mouseX, mouseY;
private KeyHandler keyHandler;
private MouseHandler mouseHandler;
private WallState walls;
public GameState() {
public GameState(WallState walls) {
this.walls = walls;
// Initialize the game state and all elements ...
locX = 100;
locY = 100;
......@@ -57,6 +58,9 @@ public class GameState {
// Update the state of all game elements
// based on user input and elapsed time ...
//
int lastx = locX,lasty = locY,lastr = rotate;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
......@@ -102,6 +106,10 @@ public class GameState {
rotate =270;
}
if (walls.isWall(locX,locY)){
locX = lastx;
locY = lasty;
}
locX = Math.max(locX, 0);
locX = Math.min(locX, MainFrame.GAME_WIDTH - diam);
......
......@@ -10,7 +10,6 @@ import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class MainFrame extends JFrame {
......@@ -21,16 +20,17 @@ public class MainFrame extends JFrame {
private Image image2;
public static final int GAME_HEIGHT = 720; // 720p game resolution
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
public WallState wallState = new WallState(GAME_WIDTH ,GAME_HEIGHT);
public MainFrame(String gameName) {
lastRender = -1;
setTitle(gameName);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(GAME_WIDTH, GAME_HEIGHT);
setResizable(false);
int lastRender = -1;
fpsHistory = new ArrayList<>(100);
}
/**
......@@ -49,13 +49,16 @@ public class MainFrame extends JFrame {
*/
public void render(GameState state) {
do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do {
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try {
doRendering(graphics, state);
} finally {
......@@ -82,9 +85,14 @@ public class MainFrame extends JFrame {
// to the game 'state' using 'g2d' ...
private void doRendering(Graphics2D g2d, GameState state) {
// Draw background
WallState wallState = new WallState();
//wallState.paintComponent(g2d);
g2d.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
// g2d.setColor(Color.GREEN);
// g2d.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
wallState.paintComponent(g2d);
// Draw tank;
try {
image1 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_blue.png"));
......@@ -103,9 +111,6 @@ public class MainFrame extends JFrame {
// Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter( image11, null),state.locX , state.locY , null);
// g2d.rotate(state.rotateR);
// g2d.fillOval(state.locX, state.locY, state.diam, state.diam);
// g2d.drawImage(image1, state.locX, state.locY, null);
// g2d.drawImage(image2 , state.locX+30 , state.locY+30 , null);
......@@ -145,8 +150,5 @@ public class MainFrame extends JFrame {
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, GAME_HEIGHT / 2);
}
}
}
package UI;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class WallState {
public void paintComponent(Graphics g)
{
try{
paintComponent(g);
FileReader fr = new FileReader("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map1.txt");
BufferedReader br=new BufferedReader(fr);
g.setColor(Color.black); //colour of the walls
String s;
s=br.readLine();
int numFilas= Integer.parseInt(s); //number of rows
s=br.readLine();
int numColumnas= Integer.parseInt(s); //number of columns
public class WallState {
private int frameX, frameY;
int ladoFilas=650/numFilas; //sideY
int ladoColumnas=400/numColumnas; //sideX
int grosor=5; //width of the wall
private String source;
private String[] lines;
private int numRows, numColumns, w, h;
private final int wallWidth = 5;
private ArrayList<WallData> walls = new ArrayList<>();
int y=0;
for(int i=0;i<numFilas;i++){
int x=0;
for(int j=0;j<numColumnas;j++){
int lectura=br.read();
char b= (char) lectura; //char from File
//here I create rectangles with different position, width, etc depending on the read value from the file. These rectangles are the walls. I use fillRect(x,y,width,height)
public WallState(int GAME_WIDTH, int GAME_HEIGHT) {
frameX = GAME_WIDTH;
frameY = GAME_HEIGHT;
if(b=='1'){
g.fillRect(x, y, ladoColumnas, grosor);
try {
source = new String(Files.readAllBytes(Paths.get("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map1.txt")), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
lectura=br.read();
b= (char) lectura;
lines = source.split("\r\n|\r|\n");
if(b=='1'){
g.fillRect(x+(ladoColumnas-grosor), y, grosor, ladoFilas);
numRows = lines.length; //number of rows
numColumns = lines[0].trim().length(); //number of columns
h = (frameY-30) / (numRows); //sideY
w = (frameX-10) / (numColumns); //sideX
}
lectura=br.read();
b= (char) lectura;
if(b=='1'){
g.fillRect(x, y+(ladoFilas-grosor),ladoColumnas, grosor);
public boolean isWall(int x,int y){
for(WallData d : walls){
if (d.isWall(x,y)) return true;
}
return false;
}
lectura=br.read();
b= (char) lectura;
if(b=='1'){
g.fillRect(x, y, grosor, ladoFilas);
public void paintComponent(Graphics g) {
// Draw background
walls.clear();
g.setColor(Color.WHITE);
g.fillRect(0, 0, frameX, frameY);
g.setColor(Color.black); //colour of the walls
int start = 10;
int startTop = 30;
for (int r = 0; r < numRows; r++) {
String rowChars = lines[r];
for (int c = 0; c < numColumns; c++) {
if (rowChars.charAt(c)=='0') continue;
if (c==0 && r==0 ){
if (rowChars.charAt(c+1)=='1') draw(g,start,startTop,w+(w/2),wallWidth);
if (lines[r+1].charAt(c)=='1') draw(g,start,start,wallWidth,h);
continue;
}
x+=ladoColumnas;
if (r==0){
if (lines[r+1].charAt(c)=='1'){
if (c==numColumns-1) {
draw(g,frameX-start-wallWidth,startTop,wallWidth,h);
}else{
draw(g,(c*w)+(w/2)+start,startTop,wallWidth,h);
}
}
br.readLine();
y+=ladoFilas;
}
//exit position, which is the last two lines of the the file
s=br.readLine();
int filaSalida=Integer.parseInt(s); //exit row
s=br.readLine();
int columnaSalida=Integer.parseInt(s); //exit column
g.setColor(Color.green);
g.fillRect((ladoColumnas*columnaSalida)-ladoColumnas,ladoFilas*filaSalida, ladoFilas, ladoColumnas); //I paint a square in the exit
if ((c<numColumns-1 && rowChars.charAt(c+1)=='1') || (c>0 && rowChars.charAt(c-1)=='1')){ //horizontal
int y = (r*h) + startTop;
int x = start;
if (r == numRows -1)
y = frameY - start - wallWidth;//last row
if (r<numRows-1 && r>0)
y += h/4;
if (c>0)
x = (w*c)+(w/2)+start;
if (r==numRows-1 && c>0)
x -=w/2;
draw(g,x,y,w,wallWidth);
}
if ((r>0 && lines[r-1].charAt(c)=='1') || (r>0 && r<numRows-1 && lines[r+1].charAt(c)=='1')) { //vertical
int x = start;
int y = r * h +start;
int th = h;
if (c>0)
x = (c*w) + (w/2) + start;
if (r==numRows-1)
th += startTop*2;
if (c==numColumns-1)//last column
x = frameX - start - wallWidth;
draw(g,x, y, wallWidth, th);
}
}
}
br.close();
//draw frame
g.setColor(Color.WHITE);
g.fillRect(0,frameX,frameX,startTop);
g.fillRect(0,0,start,frameY);
g.fillRect(frameX-start,0,start,frameY);
g.fillRect(0,frameY-start,frameX,start);
}
private void draw(Graphics g,int x,int y,int w,int h){
walls.add(new WallData(x,y,w,h));
g.fillRect(x,y,w,h);
}
catch(Exception e){
public class WallData {
int x,y,w,h;
public WallData(int x,int y,int w,int h){
this.x = x;
this.y = y;
this.h = h;
this.w = w;
}
public boolean isWall (int x,int y){
//BUG
return (x>=this.x && x<=this.x+w && y>=this.y && y<this.y+h);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment