From 55508b9a2e898cc1adb490bc108fca4a95795101 Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:39:54 -0500 Subject: [PATCH 01/24] Create TankT.java --- src/game/TankT.java | 97 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/game/TankT.java diff --git a/src/game/TankT.java b/src/game/TankT.java new file mode 100644 index 0000000..bf1c770 --- /dev/null +++ b/src/game/TankT.java @@ -0,0 +1,97 @@ +package game; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.geom.AffineTransform; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class TankT extends JPanel implements KeyListener { + + private static final long serialVersionUID = 1L; + private int xPos = 50; + private int yPos = 50; + private int speed = 5; + private double angle = 0; + + public TankT() { + setPreferredSize(new Dimension(400, 400)); + addKeyListener(this); + setFocusable(true); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + g.setColor(Color.RED); + AffineTransform oldTransform = g2d.getTransform(); // Save the current transformation + + g2d.rotate(angle, xPos + 25, yPos + 25); // Rotate the rectangle around its center + g.fillRect(xPos, yPos, 50, 50); // Draw the rectangle at the current x and y position + g2d.setTransform(oldTransform); // Restore the old transformation + } + + @Override + public void keyPressed(KeyEvent e) { + int key = e.getKeyCode(); // Get the key that was pressed + if (key == KeyEvent.VK_W) { // Check if the W key was pressed + // Calculate the new x and y positions based on the current angle + double newX = xPos + speed * Math.cos(angle); + double newY = yPos + speed * Math.sin(angle); + + // Update the x and y positions + xPos = (int) newX; + yPos = (int) newY; + } + + if (key == KeyEvent.VK_S) { // Check if the S key was pressed + + // Calculate the new x and y positions based on the current angle + double newX = xPos - speed * Math.cos(angle); + double newY = yPos - speed * Math.sin(angle); + + // Update the x and y positions + xPos = (int) newX; + yPos = (int) newY; + } + + if (key == KeyEvent.VK_A) { // Check if the A key was pressed + + // Decrease the angle of the rectangle by a small amount + angle -= 0.1; + } + + if (key == KeyEvent.VK_D) { // Check if the D key was pressed + + // Increase the angle of the rectangle by a small amount + angle += 0.1; + } + repaint(); // Repaint the panel to update the position and rotation of the rectangle + + } + + @Override + public void keyReleased(KeyEvent e) { + // Not needed for this example + } + + @Override + public void keyTyped(KeyEvent e) { + // Not needed for this example + } + + public static void main(String[] args) { + JFrame frame = new JFrame("Rotating Rectangle Game"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + TankT panel = new TankT(); + frame.add(panel); + frame.pack(); + frame.setVisible(true); + } +} \ No newline at end of file From 723b4bf90df3f55c445c1c13d7f54a2903ef5fa5 Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:43:43 -0500 Subject: [PATCH 02/24] Update TankTrouble.java --- src/game/TankTrouble.java | 101 ++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/src/game/TankTrouble.java b/src/game/TankTrouble.java index 082b00b..c01c145 100644 --- a/src/game/TankTrouble.java +++ b/src/game/TankTrouble.java @@ -1,11 +1,8 @@ package game; - import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Polygon; -import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; @@ -15,9 +12,6 @@ import javax.swing.SwingUtilities; import javax.swing.Timer; -import java.util.ArrayList; - - public class TankTrouble implements ActionListener { private class Ball { @@ -47,8 +41,16 @@ private class Square { this.color = color; } } + + private class Point { + int x, y; + Point(int x, int y) { + this.x = x; + this.y = y; + } + } - Square grid [][]= new Square[5][5]; + public static void main(String[] args) { //using this makes animation more reliable @@ -61,23 +63,29 @@ public void run() { //global variables and constants DrawingPanel panel; + + static final int PANW = 700; + static final int PANH = 700; + static final int squareW = PANW/7; + static final int squareH = PANH/7; + + Ball ball = new Ball(200, 300, -1, -1, Color.yellow); + Square grid [][]= new Square[7][7]; - static final int PANW = 800; - static final int PANH = 600; - - Ball ball = new Ball(200,300,-1,-1,Color.yellow); - Rectangle box = new Rectangle (700,350,40,40); + Point points [][] = new Point[8][8]; + Timer animationTimer = new Timer(1, this); //delay double angle = 0.0; TankTrouble() { - JFrame window = new JFrame("Bouncing Ball"); + JFrame window = new JFrame("Tank Trouble"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new DrawingPanel(); window.add(panel); window.pack(); // because JPanel will set the size window.setLocationRelativeTo(null); + window.setResizable(false); window.setVisible(true); animationTimer.setInitialDelay(1000); // wait a second before starting @@ -93,70 +101,59 @@ public class DrawingPanel extends JPanel { @Override public void paintComponent(Graphics g) { // super.paintComponent(g); - Graphics2D g2 = (Graphics2D) g; - - // rotate the background - g.setColor(Color.RED); - g2.rotate(Math.toRadians(angle), PANW/2, PANH/2); - g.fillRect(box.x, box.y, box.width, box.height); - g2.rotate(Math.toRadians(-angle), PANW/2, PANH/2); + Graphics2D g2 = (Graphics2D) g; g.setColor(ball.color); g.fillOval(ball.x, ball.y, ball.size, ball.size); g.setColor(Color.GRAY); g.drawLine(PANW/2, PANH/2 , ball.x + ball.size/2, ball.y + ball.size/2); - g.drawLine(box.x, box.y , ball.x + ball.size/2, ball.y + ball.size/2); addObjects(g2); moveAndBounceBall(ball, grid, g2); } } - - - int w = PANW/5; - int h = PANH/5; + + public void addPoints(Graphics2D gs) { + for(int i = 0; i < 8; i++) { + for(int j = 0; j < 8; j++) { + points[i][j] = new Point(100*j, 100*i); + gs.setColor(new Color(0,0,0,50)); + gs.fillRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); + gs.drawRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); + } + } + } public void addObjects(Graphics2D gs) { - for(int i = 0; i < 5; i++) { - for(int j = 0; j < 5; j++) { - grid[i][j] = new Square(w*((5*i+j)%5), w*((5*i+j)%5) + w, h*((5*i+j)/5), h*((5*i+j)/5) + h, new Color(0,0,0,50)); // x1, x2, y1, y2, color + for(int i = 0; i < 7; i++) { + for(int j = 0; j < 7; j++) { + grid[i][j] = new Square(squareW*((7*i+j)%7), squareW*((7*i+j)%7) + squareW, squareH*((7*i+j)/7), squareH*((7*i+j)/7) + squareH, new Color(0,0,0,50)); // x1, x2, y1, y2, color gs.setColor(new Color(0,0,0,50)); - gs.fillRect(w*((5*i+j)%5), h*((5*i+j)/5), w, h); - gs.drawRect(w*((5*i+j)%5), h*((5*i+j)/5), w, h); + gs.fillRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); + gs.drawRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); } } } - public void moveAndBounceBall(Ball b, Square[][] s, Graphics2D g3) { b.x += b.vx; b.y += b.vy; - if (b.x + b.size < 0) { - b.x = PANW; // teleport to the other side - } + if (b.x < 0 && b.vx < 0) { // bounce of left if it's moving right + b.vx *= -1; + } - if (b.y < 0 && b.vy < 0) { - b.vy *= -1;// bounce off the top if it's moving up - //b.y -= b.vy; // undo the last move <<< this may not be necessary - b.color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)); - } + if (b.x + b.size > PANW && b.vx > 0) { // bounce off right if it's moving left + b.vx *= -1; + } - if (b.y + b.size > PANH && b.vy > 0) { //bounce off bottom + if (b.y < 0 && b.vy < 0) { // bounce off the top if it's moving up b.vy *= -1; - //b.y -= b.vy; // undo the last move - b.color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)); } -// for(int i = 0; i < 5; i++) { -// for(int j = 0; j < 5; j++) { -// if(b.x + b.size > s[i][j].x1 && b.x + b.size < s[i][j].x2 && b.y + b.size > s[i][j].y1 && b.x + b.size < s[i][j].y2) { -// s[i][j].color = Color.green; -// g3.setColor(Color.green.darker()); -// g3.fillRect(w*((5*i+j)%5), h*((5*i+j)/5), w, h); -// } -// } -// } + if (b.y + b.size > PANH && b.vy > 0) { // bounce off bottom + b.vy *= -1; + } } @@ -165,4 +162,4 @@ public void actionPerformed(ActionEvent e) { angle += 0.1; panel.repaint(); } -} +} \ No newline at end of file From fd3e9f1ec3026f823da1c7fc68d4eb6354583665 Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:22:21 -0500 Subject: [PATCH 03/24] Update TankTrouble.java --- src/game/TankTrouble.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/game/TankTrouble.java b/src/game/TankTrouble.java index c01c145..3b8b0d5 100644 --- a/src/game/TankTrouble.java +++ b/src/game/TankTrouble.java @@ -1,4 +1,5 @@ package game; +import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; @@ -109,20 +110,25 @@ public void paintComponent(Graphics g) { g.drawLine(PANW/2, PANH/2 , ball.x + ball.size/2, ball.y + ball.size/2); addObjects(g2); - moveAndBounceBall(ball, grid, g2); + moveAndBounceBall(ball, grid, g2); + addPoints(); + walls(g,g2); } } - public void addPoints(Graphics2D gs) { + public void addPoints() { for(int i = 0; i < 8; i++) { for(int j = 0; j < 8; j++) { points[i][j] = new Point(100*j, 100*i); - gs.setColor(new Color(0,0,0,50)); - gs.fillRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); - gs.drawRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); } } } + + public void walls(Graphics g, Graphics2D g2) { + g.setColor(Color.GREEN); + g2.setStroke(new BasicStroke(10)); + g.drawLine(points[0][0].x, points[0][0].y, points[1][0].x, points[1][0].y); + } public void addObjects(Graphics2D gs) { for(int i = 0; i < 7; i++) { From 2b00803d8962c38b4925d43582f178ab932069f8 Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:09:31 -0500 Subject: [PATCH 04/24] Little Update Updated the large game class a little --- src/game/Walls.java | 136 +++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 72 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 826feed..0ff9e88 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,6 +1,8 @@ package game; -/* Add comments here - * +/* + Decker, James, Tian + TankTrouble + Jan , 2023 */ import java.awt.BasicStroke; @@ -31,26 +33,25 @@ public class Walls implements ActionListener { - public static void main(String[] args) { - SwingUtilities.invokeLater(new Runnable() { + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { public void run() { new Walls(); } }); - } + } - final static int PANW = 500; - final static int PANH = 400; - final static int TIMERSPEED = 1; + static final int PANW = 500; + static final int PANH = 400; + static final int TIMERSPEED = 1; - /***** instance variables (global) *****/ + /***** instance variables (global) *****/ DrawingPanel drPanel = new DrawingPanel(); - void createAndShowGUI() { - JFrame frame = new JFrame("Awesome game!"); + void createAndShowGUI() { + JFrame frame = new JFrame("Tank Trouble"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setResizable(false); - frame.add(drPanel); frame.pack(); @@ -58,12 +59,12 @@ void createAndShowGUI() { frame.setVisible(true); } - void startTimer() { + void startTimer() { Timer timer = new Timer(TIMERSPEED, this); timer.start(); - } + } - class Ball extends Rectangle{ + class Ball extends Rectangle{ // int x,y; //position int vx, vy; //speed int size = 10; @@ -71,53 +72,47 @@ class Ball extends Rectangle{ Ball(int x, int y, int vx, int vy){ this.x = x; this.y = y; - this.width = size; - this.height = size; + this.width = size; + this.height = size; this.vx = vx; //move this many pixels each time this.vy = vy; } } - class Wall extends Rectangle{ + class Wall extends Rectangle{ //int x,y,width,hight; //position - boolean v = true; + boolean v = true; Wall(int x, int y, boolean vertical){ this.x = x; this.y = y; - this.v = vertical; - if(vertical){ - this.width = 2; - this.height = 100; - } else{ - this.width = 100; - this.height = 2; - } - + this.v = vertical; + if(vertical) { + this.width = 2; + this.height = 100; + } + else { + this.width = 100; + this.height = 2; + } } } - - - - ArrayList walls = new ArrayList(); - ArrayList bullets = new ArrayList(); - - Ball b1 = new Ball(200,300,3,2); - Wall w1 = new Wall(200,200, true); - - Walls() { + + ArrayList walls = new ArrayList(); + ArrayList bullets = new ArrayList(); + + Ball b1 = new Ball(200,300,3,2); + Wall w1 = new Wall(200,200, true); + + Walls() { createAndShowGUI(); startTimer(); - bullets.add(b1); - walls.add( w1 ); - walls.add( new Wall(200,200, false) ); - - // for(Rectangle w : walls){ - // System.out.println(w.x+", "+w.y+", "+w.width+", "+w.height+", "); - // } + bullets.add(b1); + walls.add(w1); + walls.add(new Wall(200,200, false)); } - public void moveAndBounceBall(Ball b, Graphics g) { + public void moveAndBounceBall(Ball b, Graphics g) { b.x += b.vx; b.y += b.vy; @@ -136,47 +131,44 @@ public void moveAndBounceBall(Ball b, Graphics g) { } - for(Wall w : walls){ - if (w.intersects(b)){ - if (w.v){ - b.vx *= -1; - }else{ - b.vy *= -1; - } - } - } + for(Wall w: walls){ + if (w.intersects(b)){ + if (w.v) { + b.vx *= -1; + } + else { + b.vy *= -1; + } + } + } } - class DrawingPanel extends JPanel { + class DrawingPanel extends JPanel { DrawingPanel() { this.setBackground(Color.LIGHT_GRAY); this.setPreferredSize(new Dimension(PANW,PANH)); //remember that the JPanel size is more accurate than JFrame. } - + @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - + g2.setStroke(new BasicStroke(2)); g.drawString("Here is your drawing panel", 100,100); - - //g.fillRect(wall1.x, wall1.y, wall1.w, wall1.h); + + //g.fillRect(wall1.x, wall1.y, wall1.w, wall1.h); g.fillOval(b1.x, b1.y, 10,10); - for(Rectangle w : walls){ - g.fillRect(w.x, w.y, w.width, w.height); - } - moveAndBounceBall(b1, g2); + for(Rectangle w : walls){ + g.fillRect(w.x, w.y, w.width, w.height); + } + moveAndBounceBall(b1, g2); } } - @Override - public void actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub - drPanel.repaint(); - - } - - + @Override + public void actionPerformed(ActionEvent e) { + drPanel.repaint(); + } } From ccf1cc9b18bec431bc4c903d0b67c5388fbca397 Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Wed, 18 Jan 2023 12:58:09 -0500 Subject: [PATCH 05/24] Kinda merged tank and main class --- src/game/TankT.java | 58 ++++++++++++++++++--------------------------- src/game/Walls.java | 14 ++++++++--- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/game/TankT.java b/src/game/TankT.java index bf1c770..6f06b06 100644 --- a/src/game/TankT.java +++ b/src/game/TankT.java @@ -6,18 +6,16 @@ import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import java.awt.geom.AffineTransform; import javax.swing.JFrame; import javax.swing.JPanel; public class TankT extends JPanel implements KeyListener { - private static final long serialVersionUID = 1L; - private int xPos = 50; - private int yPos = 50; - private int speed = 5; - private double angle = 0; + static int tankX = 50; + static int tankY = 50; + static int speed = 5; + static double angle = 0; public TankT() { setPreferredSize(new Dimension(400, 400)); @@ -25,55 +23,45 @@ public TankT() { setFocusable(true); } - @Override - public void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2d = (Graphics2D) g; + public void paintTank(Graphics g, Graphics2D g2) { g.setColor(Color.RED); - AffineTransform oldTransform = g2d.getTransform(); // Save the current transformation - - g2d.rotate(angle, xPos + 25, yPos + 25); // Rotate the rectangle around its center - g.fillRect(xPos, yPos, 50, 50); // Draw the rectangle at the current x and y position - g2d.setTransform(oldTransform); // Restore the old transformation + g2.rotate(angle, tankX + 25, tankY + 25); // Rotate the rectangle around its center + g.fillRect(tankX, tankY, 50, 50); // Draw the rectangle at the current x and y position } @Override public void keyPressed(KeyEvent e) { - int key = e.getKeyCode(); // Get the key that was pressed - if (key == KeyEvent.VK_W) { // Check if the W key was pressed - // Calculate the new x and y positions based on the current angle - double newX = xPos + speed * Math.cos(angle); - double newY = yPos + speed * Math.sin(angle); - + int key = e.getKeyCode(); // gets the key that was pressed + if (key == KeyEvent.VK_W) { // 'W' key was pressed + // Calculate new x and y positions based on the current angle + double newX = tankX + speed * Math.cos(angle); + double newY = tankY + speed * Math.sin(angle); + // Update the x and y positions - xPos = (int) newX; - yPos = (int) newY; + tankX = (int) newX; + tankY = (int) newY; } - if (key == KeyEvent.VK_S) { // Check if the S key was pressed - - // Calculate the new x and y positions based on the current angle - double newX = xPos - speed * Math.cos(angle); - double newY = yPos - speed * Math.sin(angle); - - // Update the x and y positions - xPos = (int) newX; - yPos = (int) newY; + if (key == KeyEvent.VK_S) { // 'S' key was pressed + double newX = tankX - speed * Math.cos(angle); + double newY = tankY - speed * Math.sin(angle); + + tankX = (int) newX; + tankY = (int) newY; } - if (key == KeyEvent.VK_A) { // Check if the A key was pressed + if (key == KeyEvent.VK_A) { // 'A' key was pressed // Decrease the angle of the rectangle by a small amount angle -= 0.1; } - if (key == KeyEvent.VK_D) { // Check if the D key was pressed + if (key == KeyEvent.VK_D) { // 'D' key was pressed // Increase the angle of the rectangle by a small amount angle += 0.1; } repaint(); // Repaint the panel to update the position and rotation of the rectangle - } @Override diff --git a/src/game/Walls.java b/src/game/Walls.java index 0ff9e88..25171eb 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -13,6 +13,7 @@ import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import java.util.ArrayList; import javax.swing.JFrame; @@ -55,7 +56,7 @@ void createAndShowGUI() { frame.add(drPanel); frame.pack(); - frame.setLocationRelativeTo( null ); + frame.setLocationRelativeTo(null); frame.setVisible(true); } @@ -64,7 +65,7 @@ void startTimer() { timer.start(); } - class Ball extends Rectangle{ + class Ball extends Rectangle { // int x,y; //position int vx, vy; //speed int size = 10; @@ -143,6 +144,8 @@ public void moveAndBounceBall(Ball b, Graphics g) { } } + TankT tank = new TankT(); + class DrawingPanel extends JPanel { DrawingPanel() { this.setBackground(Color.LIGHT_GRAY); @@ -164,9 +167,14 @@ public void paintComponent(Graphics g) { g.fillRect(w.x, w.y, w.width, w.height); } moveAndBounceBall(b1, g2); + + tank.paintTank(g,g2); } } - + public void keyPressed(KeyEvent k) { + int key = k.getKeyCode(); // gets the key that was pressed + + } @Override public void actionPerformed(ActionEvent e) { drPanel.repaint(); From 3767ea174a6f987985109397152ce7d172518205 Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Tue, 24 Jan 2023 12:04:20 -0500 Subject: [PATCH 06/24] latest game --- src/game/Walls.java | 323 +++++++++++++++++++++++++++++--------------- 1 file changed, 217 insertions(+), 106 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 25171eb..0776887 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,182 +1,293 @@ -package game; -/* - Decker, James, Tian - TankTrouble - Jan , 2023 - */ +package Graphics; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; +import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.geom.AffineTransform; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; -import javax.swing.SwingUtilities; import javax.swing.Timer; -import java.awt.Rectangle; - - - -import java.awt.Dimension; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.SwingUtilities; - -import game.MainGame.DrawingPanel; -public class Walls implements ActionListener { - - public static void main(String[] args) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - new Walls(); - } - }); - } +// import Graphics.TankGame.Ball; +// import Graphics.TankGame.Tank; +// import Graphics.TankGame.Wall; +public class AnimationAndKeys2 extends JFrame implements KeyListener, ActionListener { static final int PANW = 500; static final int PANH = 400; - static final int TIMERSPEED = 1; - - /***** instance variables (global) *****/ - DrawingPanel drPanel = new DrawingPanel(); - - void createAndShowGUI() { - JFrame frame = new JFrame("Tank Trouble"); - frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); - frame.setResizable(false); + static final int SLEEPTIME = 10; //in milliseconds + static final int TIMERSPEED = 10; - frame.add(drPanel); - frame.pack(); - frame.setLocationRelativeTo(null); - frame.setVisible(true); - } + DrawingPanel panel; + boolean isPlaying = true; + void startTimer() { Timer timer = new Timer(TIMERSPEED, this); timer.start(); - } + } - class Ball extends Rectangle { + class Ball extends Rectangle{ // int x,y; //position - int vx, vy; //speed - int size = 10; - - Ball(int x, int y, int vx, int vy){ - this.x = x; - this.y = y; - this.width = size; - this.height = size; + double vx, vy = 0; //speed + int size = 5; + boolean intersecting; + boolean player1sbullet; + + Ball(boolean player1sbullet){ + this.x = -10; + this.y = -10; + this.width = size; + this.height = size; this.vx = vx; //move this many pixels each time this.vy = vy; + this.intersecting = false; + this.player1sbullet = player1sbullet; } } - class Wall extends Rectangle{ + class Wall extends Rectangle{ //int x,y,width,hight; //position - boolean v = true; + boolean v = true; Wall(int x, int y, boolean vertical){ this.x = x; this.y = y; - this.v = vertical; + this.v = vertical; if(vertical) { - this.width = 2; - this.height = 100; - } + this.width = 2; + this.height = 100; + } else { - this.width = 100; - this.height = 2; - } + this.width = 100; + this.height = 2; + } } } - ArrayList walls = new ArrayList(); - ArrayList bullets = new ArrayList(); + class Tank extends Rectangle{ + int angle; + int speed; + Tank(int x, int y){ + this.angle=0; + this.x = x; + this.y = y; + this.width = 20; + this.height = 30; + this.angle = 90; + this.speed = 5; + } + } + Tank tank1 = new Tank(100,100); + Rectangle ship = new Rectangle(tank1.x, tank1.y, tank1.width,tank1.height); + + int bullet = 30; // bullet counter + + void createGUI() { + JFrame window = new JFrame("Tank Trouble"); + + window.setSize(PANW, PANH); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.setLocationRelativeTo(null); + + panel = new DrawingPanel(); + panel.setBackground(Color.WHITE); + panel.addKeyListener(this); + panel.setFocusable(true); + panel.requestFocus(); - Ball b1 = new Ball(200,300,3,2); - Wall w1 = new Wall(200,200, true); + window.add(panel); + window.setVisible(true); + } + + // With this method, two keys can be down at once allowing smooth diagonal movement + void moveShip() { + if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; + if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; + if (isKeyDown(KeyEvent.VK_W)){ + tank1.y +=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.x -=Math.sin(Math.toRadians(tank1.angle))*2; + } + if (isKeyDown(KeyEvent.VK_S)) { + tank1.y -=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.x +=Math.sin(Math.toRadians(tank1.angle))*2; + } + if (isKeyDown(KeyEvent.VK_C)) fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); + + + panel.repaint(); + } + + ArrayList walls = new ArrayList(); + ArrayList bulletCounter = new ArrayList(); - Walls() { - createAndShowGUI(); + + AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() + createGUI(); startTimer(); + createMap(); + + for (int i = 0;i<300;i++){ // creates 30 bullets for player 1 off screen + bulletCounter.add(new Ball(true)); + } + + for (int i = 0;i<300;i++){ // creates 30 bullets for player 2 off screen + bulletCounter.add(new Ball(false)); + } - bullets.add(b1); - walls.add(w1); - walls.add(new Wall(200,200, false)); + while(isPlaying) { + moveShip(); + Thread.sleep(SLEEPTIME); + } } - public void moveAndBounceBall(Ball b, Graphics g) { + public void createMap(){ + walls.add(new Wall(200,200, true)); + walls.add(new Wall(250,200, false)); + } + + public void moveAndBounceBall(Ball b) { + + for(Wall w : walls){ + if (w.intersects(b) && !b.intersecting){ + if (w.v){ + b.vx *= -1; + }else{ + b.vy *= -1; + } + b.intersecting = true; + }else{ + b.intersecting = false; + } + } + b.x += b.vx; b.y += b.vy; if (b.x + b.size < 0 ||b.x + b.size > PANW) { - b.vx *= -1; // teleport to the other side + b.vx *= -1; } - if (b.y < 0 && b.vy < 0) { - b.vy *= -1;// bounce off the top if it's moving up - //b.y -= b.vy; // undo the last move <<< this may not be necessary - } + // if (b.y < 0) { + // b.vy *= -1;// bounce off the top if it's moving up + // //b.y -= b.vy; // undo the last move <<< this may not be necessary + // } if (b.y + b.size > PANH || b.y + b.size < 0) { //bounce off bottom b.vy *= -1; - //b.y -= b.vy; // undo the last move - } - - - for(Wall w: walls){ - if (w.intersects(b)){ - if (w.v) { - b.vx *= -1; - } - else { - b.vy *= -1; - } - } } + } - TankT tank = new TankT(); + public void moveTank(){ - class DrawingPanel extends JPanel { + } + + class DrawingPanel extends JPanel { DrawingPanel() { this.setBackground(Color.LIGHT_GRAY); this.setPreferredSize(new Dimension(PANW,PANH)); //remember that the JPanel size is more accurate than JFrame. - } + this.setFont(new Font("Sans Serif", Font.PLAIN, 20)); + } + @Override public void paintComponent(Graphics g) { super.paintComponent(g); + + g.setColor(Color.BLACK); + //g.fillRect(tank1.x, tank1.y, ship.width, ship.height); + + //g.drawString("Weapon = " + bulletCounter, 30, 20); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - + g2.setStroke(new BasicStroke(2)); g.drawString("Here is your drawing panel", 100,100); + + // Save the current transformation + AffineTransform oldTransform = g2.getTransform(); - //g.fillRect(wall1.x, wall1.y, wall1.w, wall1.h); - g.fillOval(b1.x, b1.y, 10,10); - for(Rectangle w : walls){ - g.fillRect(w.x, w.y, w.width, w.height); - } - moveAndBounceBall(b1, g2); + // Rotate the rectangle around its center + g2.rotate(Math.toRadians(tank1.angle), tank1.x + tank1.width/2, tank1.y + tank1.height/2); + g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); + g2.setTransform(oldTransform); - tank.paintTank(g,g2); + for(Rectangle w : walls){ + g.fillRect(w.x, w.y, w.width, w.height); + } + for(Ball b : bulletCounter){ + g.fillOval(b.x, b.y, b.width,b.height); + moveAndBounceBall(b); + } } } - public void keyPressed(KeyEvent k) { - int key = k.getKeyCode(); // gets the key that was pressed - - } + + public void fire(int x, int y, double angle, boolean player1sbullet){ + for(Ball b : bulletCounter){ + if (b.player1sbullet == player1sbullet && b.vx == 0 && b.vy == 0){ + b.x=x; + b.y=y; + b.vx = Math.cos(Math.toRadians(angle))*5; + b.vy = Math.sin(Math.toRadians(angle))*5; + + System.out.println(Double.toString(b.vx)); + break; + } + } + } + + private final int numKeyCodes = 256; /** size of keysDown array **/ + private boolean[] keysDown = new boolean [numKeyCodes]; /** Array of booleans representing characters currently held down **/ + + public synchronized boolean isKeyDown(int key) { + // if ((key >=0) & (key < numKeyCodes)) + return keysDown[key]; + // return false; + } + + @Override + public void keyPressed(KeyEvent e) { + int key = e.getKeyCode (); + keysDown[key] = true; + + switch (key) { + case KeyEvent.VK_A: + case KeyEvent.VK_D: + case KeyEvent.VK_W: + case KeyEvent.VK_S: + case KeyEvent.VK_C: + break; + } + panel.repaint(); + } + + @Override + public void keyReleased(KeyEvent e) { + keysDown[e.getKeyCode()] = false; + } + + @Override + public void keyTyped(KeyEvent e) { + } + @Override - public void actionPerformed(ActionEvent e) { - drPanel.repaint(); - } + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + panel.repaint(); + } + + public static void main(String[] args) throws InterruptedException { + new AnimationAndKeys2(); + } } From 376f21f99145a3806c677ea583710a20e08e13cf Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Tue, 24 Jan 2023 12:13:03 -0500 Subject: [PATCH 07/24] Update Walls.java --- src/game/Walls.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 0776887..c8aff93 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,4 +1,4 @@ -package Graphics; +package game; import java.awt.BasicStroke; import java.awt.Color; @@ -23,7 +23,7 @@ // import Graphics.TankGame.Tank; // import Graphics.TankGame.Wall; -public class AnimationAndKeys2 extends JFrame implements KeyListener, ActionListener { +public class Walls extends JFrame implements KeyListener, ActionListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds @@ -132,7 +132,7 @@ void moveShip() { ArrayList bulletCounter = new ArrayList(); - AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() + Walls() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); startTimer(); createMap(); @@ -288,6 +288,6 @@ public void actionPerformed(ActionEvent e) { } public static void main(String[] args) throws InterruptedException { - new AnimationAndKeys2(); + new Walls(); } } From 81d3d002c552d29250fd035bf5c885060fb8f975 Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Tue, 24 Jan 2023 12:17:24 -0500 Subject: [PATCH 08/24] deleted unnecessary files --- src/game/LineWalls.java | 7 -- src/game/MainGame.java | 82 ------------------ src/game/TankT.java | 85 ------------------- src/game/TankTrouble.java | 171 -------------------------------------- 4 files changed, 345 deletions(-) delete mode 100644 src/game/LineWalls.java delete mode 100644 src/game/MainGame.java delete mode 100644 src/game/TankT.java delete mode 100644 src/game/TankTrouble.java diff --git a/src/game/LineWalls.java b/src/game/LineWalls.java deleted file mode 100644 index 099b8aa..0000000 --- a/src/game/LineWalls.java +++ /dev/null @@ -1,7 +0,0 @@ -package game; - -public class LineWalls { - public static void main(String[] args){ - - } -} diff --git a/src/game/MainGame.java b/src/game/MainGame.java deleted file mode 100644 index 4860e56..0000000 --- a/src/game/MainGame.java +++ /dev/null @@ -1,82 +0,0 @@ -package game; - -/* Add comments here - * - */ - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.RenderingHints; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.SwingUtilities; -import javax.swing.Timer; - - - -public class MainGame { - public static void main(String[] args){ - SwingUtilities.invokeLater(new Runnable() { - public void run() { - new MainGame(); - } - }); - } - - /***** constants *****/ - final static int PANW = 900; - final static int PANH = 800; - final static int TIMERSPEED = 10; - - - /***** instance variables (global) *****/ - DrawingPanel drPanel = new DrawingPanel(); - - //constructor - MainGame() { - createAndShowGUI(); - startTimer(); - } - - void createAndShowGUI() { - JFrame frame = new JFrame("Awesome game!"); - frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); - frame.setResizable(false); - - - frame.add(drPanel); - frame.pack(); - frame.setLocationRelativeTo( null ); - frame.setVisible(true); - } - - void startTimer() { -// Timer timer = new Timer(TIMERSPEED, this); -// timer.setInitialDelay(1000); -// timer.start(); - } - - class DrawingPanel extends JPanel { - DrawingPanel() { - this.setBackground(Color.LIGHT_GRAY); - this.setPreferredSize(new Dimension(PANW,PANH)); //remember that the JPanel size is more accurate than JFrame. - } - - @Override - public void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 = (Graphics2D) g; - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - - g2.setStroke(new BasicStroke(2)); - g.drawString("Here is your drawing panel", 100,100); - - g.drawOval(300, 300, 200,50); - } - } -} - diff --git a/src/game/TankT.java b/src/game/TankT.java deleted file mode 100644 index 6f06b06..0000000 --- a/src/game/TankT.java +++ /dev/null @@ -1,85 +0,0 @@ -package game; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -import javax.swing.JFrame; -import javax.swing.JPanel; - -public class TankT extends JPanel implements KeyListener { - - static int tankX = 50; - static int tankY = 50; - static int speed = 5; - static double angle = 0; - - public TankT() { - setPreferredSize(new Dimension(400, 400)); - addKeyListener(this); - setFocusable(true); - } - - public void paintTank(Graphics g, Graphics2D g2) { - g.setColor(Color.RED); - g2.rotate(angle, tankX + 25, tankY + 25); // Rotate the rectangle around its center - g.fillRect(tankX, tankY, 50, 50); // Draw the rectangle at the current x and y position - } - - @Override - public void keyPressed(KeyEvent e) { - int key = e.getKeyCode(); // gets the key that was pressed - if (key == KeyEvent.VK_W) { // 'W' key was pressed - // Calculate new x and y positions based on the current angle - double newX = tankX + speed * Math.cos(angle); - double newY = tankY + speed * Math.sin(angle); - - // Update the x and y positions - tankX = (int) newX; - tankY = (int) newY; - } - - if (key == KeyEvent.VK_S) { // 'S' key was pressed - double newX = tankX - speed * Math.cos(angle); - double newY = tankY - speed * Math.sin(angle); - - tankX = (int) newX; - tankY = (int) newY; - } - - if (key == KeyEvent.VK_A) { // 'A' key was pressed - - // Decrease the angle of the rectangle by a small amount - angle -= 0.1; - } - - if (key == KeyEvent.VK_D) { // 'D' key was pressed - - // Increase the angle of the rectangle by a small amount - angle += 0.1; - } - repaint(); // Repaint the panel to update the position and rotation of the rectangle - } - - @Override - public void keyReleased(KeyEvent e) { - // Not needed for this example - } - - @Override - public void keyTyped(KeyEvent e) { - // Not needed for this example - } - - public static void main(String[] args) { - JFrame frame = new JFrame("Rotating Rectangle Game"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - TankT panel = new TankT(); - frame.add(panel); - frame.pack(); - frame.setVisible(true); - } -} \ No newline at end of file diff --git a/src/game/TankTrouble.java b/src/game/TankTrouble.java deleted file mode 100644 index 3b8b0d5..0000000 --- a/src/game/TankTrouble.java +++ /dev/null @@ -1,171 +0,0 @@ -package game; -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.ArrayList; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.SwingUtilities; -import javax.swing.Timer; - -public class TankTrouble implements ActionListener { - - private class Ball { - int x,y; //position - int vx, vy; //speed - Color color; - int size = 40; - - Ball(int x, int y, int vx, int vy, Color color){ - this.x = x; - this.y = y; - this.vx = vx; //move this many pixels each time - this.vy = vy; - this.color = color; - } - } - - private class Square { - int x1, x2, y1, y2; //sides of rectangle - Color color; - - Square(int x1, int x2, int y1, int y2, Color color) { - this.x1 = x1; - this.x2 = x2; - this.y1 = y1; - this.y2 = y2; - this.color = color; - } - } - - private class Point { - int x, y; - Point(int x, int y) { - this.x = x; - this.y = y; - } - } - - - - public static void main(String[] args) { - //using this makes animation more reliable - SwingUtilities.invokeLater(new Runnable() { - public void run() { - new TankTrouble(); - } - }); - } - - //global variables and constants - DrawingPanel panel; - - static final int PANW = 700; - static final int PANH = 700; - static final int squareW = PANW/7; - static final int squareH = PANH/7; - - Ball ball = new Ball(200, 300, -1, -1, Color.yellow); - Square grid [][]= new Square[7][7]; - - Point points [][] = new Point[8][8]; - - - Timer animationTimer = new Timer(1, this); //delay - double angle = 0.0; - - TankTrouble() { - JFrame window = new JFrame("Tank Trouble"); - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - panel = new DrawingPanel(); - window.add(panel); - window.pack(); // because JPanel will set the size - window.setLocationRelativeTo(null); - window.setResizable(false); - window.setVisible(true); - - animationTimer.setInitialDelay(1000); // wait a second before starting - animationTimer.start(); - } - - public class DrawingPanel extends JPanel { - DrawingPanel(){ - this.setPreferredSize(new Dimension(PANW, PANH)); - this.setBackground(new Color(222,255,255)); - } - - @Override - public void paintComponent(Graphics g) { - // super.paintComponent(g); - Graphics2D g2 = (Graphics2D) g; - - g.setColor(ball.color); - g.fillOval(ball.x, ball.y, ball.size, ball.size); - g.setColor(Color.GRAY); - - g.drawLine(PANW/2, PANH/2 , ball.x + ball.size/2, ball.y + ball.size/2); - addObjects(g2); - moveAndBounceBall(ball, grid, g2); - addPoints(); - walls(g,g2); - } - } - - public void addPoints() { - for(int i = 0; i < 8; i++) { - for(int j = 0; j < 8; j++) { - points[i][j] = new Point(100*j, 100*i); - } - } - } - - public void walls(Graphics g, Graphics2D g2) { - g.setColor(Color.GREEN); - g2.setStroke(new BasicStroke(10)); - g.drawLine(points[0][0].x, points[0][0].y, points[1][0].x, points[1][0].y); - } - - public void addObjects(Graphics2D gs) { - for(int i = 0; i < 7; i++) { - for(int j = 0; j < 7; j++) { - grid[i][j] = new Square(squareW*((7*i+j)%7), squareW*((7*i+j)%7) + squareW, squareH*((7*i+j)/7), squareH*((7*i+j)/7) + squareH, new Color(0,0,0,50)); // x1, x2, y1, y2, color - gs.setColor(new Color(0,0,0,50)); - gs.fillRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); - gs.drawRect(squareW*((7*i+j)%7), squareH*((7*i+j)/7), squareW, squareH); - } - } - } - - public void moveAndBounceBall(Ball b, Square[][] s, Graphics2D g3) { - b.x += b.vx; - b.y += b.vy; - - if (b.x < 0 && b.vx < 0) { // bounce of left if it's moving right - b.vx *= -1; - } - - if (b.x + b.size > PANW && b.vx > 0) { // bounce off right if it's moving left - b.vx *= -1; - } - - if (b.y < 0 && b.vy < 0) { // bounce off the top if it's moving up - b.vy *= -1; - } - - if (b.y + b.size > PANH && b.vy > 0) { // bounce off bottom - b.vy *= -1; - } - - } - - @Override - public void actionPerformed(ActionEvent e) { - angle += 0.1; - panel.repaint(); - } -} \ No newline at end of file From a86368976791f116f73a321e9e4926007874ea0a Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Tue, 24 Jan 2023 12:39:34 -0500 Subject: [PATCH 09/24] final --- src/game/Walls.java | 75 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index c8aff93..478e9ac 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,4 +1,4 @@ -package game; +package Graphics; import java.awt.BasicStroke; import java.awt.Color; @@ -23,7 +23,7 @@ // import Graphics.TankGame.Tank; // import Graphics.TankGame.Wall; -public class Walls extends JFrame implements KeyListener, ActionListener { +public class AnimationAndKeys2 extends JFrame implements KeyListener, ActionListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds @@ -78,8 +78,10 @@ class Wall extends Rectangle{ class Tank extends Rectangle{ int angle; int speed; + double xx = 100; + double yy = 200; Tank(int x, int y){ - this.angle=0; + this.angle = 90; this.x = x; this.y = y; this.width = 20; @@ -89,6 +91,7 @@ class Tank extends Rectangle{ } } Tank tank1 = new Tank(100,100); + Tank tank2 = new Tank(200,200); Rectangle ship = new Rectangle(tank1.x, tank1.y, tank1.width,tank1.height); int bullet = 30; // bullet counter @@ -111,20 +114,37 @@ void createGUI() { } // With this method, two keys can be down at once allowing smooth diagonal movement - void moveShip() { + void moveTank() { if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; if (isKeyDown(KeyEvent.VK_W)){ - tank1.y +=Math.cos(Math.toRadians(tank1.angle))*2; - tank1.x -=Math.sin(Math.toRadians(tank1.angle))*2; + tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; } if (isKeyDown(KeyEvent.VK_S)) { - tank1.y -=Math.cos(Math.toRadians(tank1.angle))*2; - tank1.x +=Math.sin(Math.toRadians(tank1.angle))*2; + tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*2; } if (isKeyDown(KeyEvent.VK_C)) fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); + + if (isKeyDown(KeyEvent.VK_LEFT)) tank2.angle-=5; + if (isKeyDown(KeyEvent.VK_RIGHT)) tank2.angle+=5; + if (isKeyDown(KeyEvent.VK_UP)){ + tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*2; + tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*2; + } + if (isKeyDown(KeyEvent.VK_DOWN)) { + tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*2; + tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*2; + } + if (isKeyDown(KeyEvent.VK_SPACE)) fire(tank2.x+tank2.width/2, tank2.y+tank2.height/2, tank2.angle+90, false); + + tank1.x = (int)tank1.xx; + tank1.y = (int)tank1.yy; + tank2.x = (int)tank2.xx; + tank2.y = (int)tank2.yy; panel.repaint(); } @@ -132,7 +152,7 @@ void moveShip() { ArrayList bulletCounter = new ArrayList(); - Walls() throws InterruptedException { // this exception is required for Thread.sleep() + AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); startTimer(); createMap(); @@ -146,7 +166,7 @@ void moveShip() { } while(isPlaying) { - moveShip(); + moveTank(); Thread.sleep(SLEEPTIME); } } @@ -156,7 +176,7 @@ public void createMap(){ walls.add(new Wall(250,200, false)); } - public void moveAndBounceBall(Ball b) { + public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { for(Wall w : walls){ if (w.intersects(b) && !b.intersecting){ @@ -169,6 +189,22 @@ public void moveAndBounceBall(Ball b) { }else{ b.intersecting = false; } + + if (w.intersects(tank1)){ + if (w.v){ + tank1.xx = w.x; + }else{ + tank1.yy = w.y; + } + } + + if (w.intersects(tank2)){ + if (w.v){ + tank2.xx = w.x; + }else{ + tank2.yy = w.y; + } + } } b.x += b.vx; @@ -188,10 +224,6 @@ public void moveAndBounceBall(Ball b) { } } - - public void moveTank(){ - - } class DrawingPanel extends JPanel { DrawingPanel() { @@ -222,13 +254,17 @@ public void paintComponent(Graphics g) { g2.rotate(Math.toRadians(tank1.angle), tank1.x + tank1.width/2, tank1.y + tank1.height/2); g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); g2.setTransform(oldTransform); + + g2.rotate(Math.toRadians(tank2.angle), tank2.x + tank2.width/2, tank2.y + tank2.height/2); + g.fillRect(tank2.x, tank2.y, tank2.width, tank2.height); + g2.setTransform(oldTransform); for(Rectangle w : walls){ g.fillRect(w.x, w.y, w.width, w.height); } for(Ball b : bulletCounter){ g.fillOval(b.x, b.y, b.width,b.height); - moveAndBounceBall(b); + moveAndBounceBall(b, tank1, tank2); } } } @@ -267,6 +303,11 @@ public void keyPressed(KeyEvent e) { case KeyEvent.VK_W: case KeyEvent.VK_S: case KeyEvent.VK_C: + case KeyEvent.VK_UP: + case KeyEvent.VK_DOWN: + case KeyEvent.VK_LEFT: + case KeyEvent.VK_RIGHT: + case KeyEvent.VK_SPACE: break; } panel.repaint(); @@ -288,6 +329,6 @@ public void actionPerformed(ActionEvent e) { } public static void main(String[] args) throws InterruptedException { - new Walls(); + new AnimationAndKeys2(); } } From 7ba0dd225af836e3b07a33fb20e19f5332b34ec5 Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Tue, 24 Jan 2023 13:07:42 -0500 Subject: [PATCH 10/24] 2 --- src/game/Walls.java | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 478e9ac..598d074 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -81,6 +81,8 @@ class Tank extends Rectangle{ double xx = 100; double yy = 200; Tank(int x, int y){ + this.xx = x; + this.yy = y; this.angle = 90; this.x = x; this.y = y; @@ -113,17 +115,35 @@ void createGUI() { window.setVisible(true); } + + boolean checkTankintersect(Tank tank, boolean wallIsVertical){ + for(Wall w : walls){ + if (w.intersects(tank) && w.v == wallIsVertical){ + return false; + } + } + return true; + } + // With this method, two keys can be down at once allowing smooth diagonal movement void moveTank() { if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; if (isKeyDown(KeyEvent.VK_W)){ - tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*2; - tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; + if(checkTankintersect(tank1, false)){ + tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*2; + } + if(checkTankintersect(tank1, true)){ + tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; + } } if (isKeyDown(KeyEvent.VK_S)) { - tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*2; - tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*2; + if(checkTankintersect(tank1, false)){ + tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*2; + } + if(checkTankintersect(tank1, true)){ + tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*2; + } } if (isKeyDown(KeyEvent.VK_C)) fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); @@ -189,22 +209,6 @@ public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { }else{ b.intersecting = false; } - - if (w.intersects(tank1)){ - if (w.v){ - tank1.xx = w.x; - }else{ - tank1.yy = w.y; - } - } - - if (w.intersects(tank2)){ - if (w.v){ - tank2.xx = w.x; - }else{ - tank2.yy = w.y; - } - } } b.x += b.vx; From 553802fcf18b0df55335da99d3383621e97ed85e Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Wed, 25 Jan 2023 14:45:14 -0500 Subject: [PATCH 11/24] renamed package to fit repository "game" package --- src/game/Walls.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 598d074..4c4f36b 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,4 +1,4 @@ -package Graphics; +package game; import java.awt.BasicStroke; import java.awt.Color; @@ -23,7 +23,7 @@ // import Graphics.TankGame.Tank; // import Graphics.TankGame.Wall; -public class AnimationAndKeys2 extends JFrame implements KeyListener, ActionListener { +public class Walls extends JFrame implements KeyListener, ActionListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds @@ -172,7 +172,7 @@ void moveTank() { ArrayList bulletCounter = new ArrayList(); - AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() + Walls() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); startTimer(); createMap(); From 1c92877a719920707a08cccadc95faab961b752e Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Wed, 25 Jan 2023 14:45:43 -0500 Subject: [PATCH 12/24] forgot one oops --- src/game/Walls.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 4c4f36b..f26dc0d 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -333,6 +333,6 @@ public void actionPerformed(ActionEvent e) { } public static void main(String[] args) throws InterruptedException { - new AnimationAndKeys2(); + new Walls(); } } From 7cd3783bc6ca2560494d76592ac4590a3ee4188f Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Wed, 25 Jan 2023 18:33:21 -0500 Subject: [PATCH 13/24] this one --- src/game/Walls.java | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index f26dc0d..781b49a 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,4 +1,4 @@ -package game; +package Graphics; import java.awt.BasicStroke; import java.awt.Color; @@ -23,7 +23,7 @@ // import Graphics.TankGame.Tank; // import Graphics.TankGame.Wall; -public class Walls extends JFrame implements KeyListener, ActionListener { +public class AnimationAndKeys2 extends JFrame implements KeyListener, ActionListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds @@ -83,7 +83,7 @@ class Tank extends Rectangle{ Tank(int x, int y){ this.xx = x; this.yy = y; - this.angle = 90; + this.angle = 0; this.x = x; this.y = y; this.width = 20; @@ -119,10 +119,10 @@ void createGUI() { boolean checkTankintersect(Tank tank, boolean wallIsVertical){ for(Wall w : walls){ if (w.intersects(tank) && w.v == wallIsVertical){ - return false; + return true; } } - return true; + return false; } // With this method, two keys can be down at once allowing smooth diagonal movement @@ -130,19 +130,23 @@ void moveTank() { if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; if (isKeyDown(KeyEvent.VK_W)){ + tank1.yy += Math.cos(Math.toRadians(tank1.angle))*2; + tank1.xx -= Math.sin(Math.toRadians(tank1.angle))*2; if(checkTankintersect(tank1, false)){ - tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*3; } if(checkTankintersect(tank1, true)){ - tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; + tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*3; } } if (isKeyDown(KeyEvent.VK_S)) { + tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*2; if(checkTankintersect(tank1, false)){ - tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*2; + tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*2; } if(checkTankintersect(tank1, true)){ - tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*2; + tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; } } if (isKeyDown(KeyEvent.VK_C)) fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); @@ -153,10 +157,22 @@ void moveTank() { if (isKeyDown(KeyEvent.VK_UP)){ tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*2; tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*2; + if(checkTankintersect(tank2, false)){ + tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*3; + } + if(checkTankintersect(tank2, true)){ + tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*3; + } } if (isKeyDown(KeyEvent.VK_DOWN)) { tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*2; tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*2; + if(checkTankintersect(tank2, false)){ + tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*3; + } + if(checkTankintersect(tank2, true)){ + tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*3; + } } if (isKeyDown(KeyEvent.VK_SPACE)) fire(tank2.x+tank2.width/2, tank2.y+tank2.height/2, tank2.angle+90, false); @@ -172,7 +188,7 @@ void moveTank() { ArrayList bulletCounter = new ArrayList(); - Walls() throws InterruptedException { // this exception is required for Thread.sleep() + AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); startTimer(); createMap(); @@ -333,6 +349,6 @@ public void actionPerformed(ActionEvent e) { } public static void main(String[] args) throws InterruptedException { - new Walls(); + new AnimationAndKeys2(); } } From c757a5cd615dfaa860a46e196deb0d9107c41b9a Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Wed, 25 Jan 2023 19:26:01 -0500 Subject: [PATCH 14/24] Update Walls.java --- src/game/Walls.java | 48 ++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/game/Walls.java b/src/game/Walls.java index 781b49a..a36b8b4 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -19,24 +19,39 @@ import javax.swing.JPanel; import javax.swing.Timer; +import Graphics.TankGame.DrawingPanel; +import Graphics.TankGame.Tank; +import Graphics.TankGame.Timer1; + // import Graphics.TankGame.Ball; // import Graphics.TankGame.Tank; // import Graphics.TankGame.Wall; -public class AnimationAndKeys2 extends JFrame implements KeyListener, ActionListener { +public class AnimationAndKeys2 extends JFrame implements KeyListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds static final int TIMERSPEED = 10; + static final int delayTime = 50; + int bullets = 30; // bullet counter + + + Timer animationTimer = new Timer(4, new Timer1()); + int actualTime1 = 0; + int actualTime2 = 0; DrawingPanel panel; boolean isPlaying = true; - - void startTimer() { - Timer timer = new Timer(TIMERSPEED, this); - timer.start(); - } + + class Timer1 implements ActionListener{ + @Override + public void actionPerformed(ActionEvent e) { + actualTime1++; + actualTime2++; + panel.repaint(); + } + } class Ball extends Rectangle{ // int x,y; //position @@ -94,9 +109,7 @@ class Tank extends Rectangle{ } Tank tank1 = new Tank(100,100); Tank tank2 = new Tank(200,200); - Rectangle ship = new Rectangle(tank1.x, tank1.y, tank1.width,tank1.height); - int bullet = 30; // bullet counter void createGUI() { JFrame window = new JFrame("Tank Trouble"); @@ -149,7 +162,10 @@ void moveTank() { tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; } } - if (isKeyDown(KeyEvent.VK_C)) fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); + if (isKeyDown(KeyEvent.VK_C) && actualTime1 > delayTime) { + fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); + actualTime1 = 0; + } if (isKeyDown(KeyEvent.VK_LEFT)) tank2.angle-=5; @@ -174,8 +190,10 @@ void moveTank() { tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*3; } } - if (isKeyDown(KeyEvent.VK_SPACE)) fire(tank2.x+tank2.width/2, tank2.y+tank2.height/2, tank2.angle+90, false); - + if (isKeyDown(KeyEvent.VK_SPACE) && actualTime2 > delayTime) { + fire(tank2.x+tank2.width/2, tank2.y+tank2.height/2, tank2.angle+90, false); + actualTime2 = 0; + } tank1.x = (int)tank1.xx; tank1.y = (int)tank1.yy; @@ -190,7 +208,7 @@ void moveTank() { AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); - startTimer(); + animationTimer.start(); createMap(); for (int i = 0;i<300;i++){ // creates 30 bullets for player 1 off screen @@ -341,12 +359,6 @@ public void keyReleased(KeyEvent e) { @Override public void keyTyped(KeyEvent e) { } - - @Override - public void actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub - panel.repaint(); - } public static void main(String[] args) throws InterruptedException { new AnimationAndKeys2(); From 77df1a86b92547f3b53e5756f418b5bc6810764f Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Wed, 25 Jan 2023 22:36:50 -0500 Subject: [PATCH 15/24] IMAGES WORK LETS GO --- src/game/Tank.java | 25 +++++++++++++++ src/game/Walls.java | 75 ++++++++++++++++++++++++++++---------------- src/game/tank1.png | Bin 0 -> 718 bytes src/game/tank2.png | Bin 0 -> 691 bytes 4 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 src/game/Tank.java create mode 100644 src/game/tank1.png create mode 100644 src/game/tank2.png diff --git a/src/game/Tank.java b/src/game/Tank.java new file mode 100644 index 0000000..12fac05 --- /dev/null +++ b/src/game/Tank.java @@ -0,0 +1,25 @@ +package game; +import java.awt.Rectangle; +import java.awt.image.BufferedImage; +class Tank extends Rectangle{ + BufferedImage img; + BufferedImage img2; + int angle; + int speed; + double xx = 100; + double yy = 200; + Tank(int x, int y){ + this.xx = x; + this.yy = y; + this.angle = 0; + this.x = x; + this.y = y; + this.width = 20; + this.height = 30; + this.angle = 90; + this.speed = 5; + img = Walls.loadImage("src/game/tank2.png"); + img2 = Walls.loadImage("src/game/tank1.png"); + + } +} \ No newline at end of file diff --git a/src/game/Walls.java b/src/game/Walls.java index a36b8b4..254c7f6 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -1,4 +1,4 @@ -package Graphics; +package game; import java.awt.BasicStroke; import java.awt.Color; @@ -14,20 +14,23 @@ import java.awt.event.KeyListener; import java.awt.geom.AffineTransform; import java.util.ArrayList; - +import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; +import java.io.*; +import javax.imageio.ImageIO; +import javax.swing.JOptionPane; -import Graphics.TankGame.DrawingPanel; -import Graphics.TankGame.Tank; -import Graphics.TankGame.Timer1; +//import Graphics.TankGame.DrawingPanel; +//import Graphics.TankGame.Tank; +//import Graphics.TankGame.Timer1; // import Graphics.TankGame.Ball; // import Graphics.TankGame.Tank; // import Graphics.TankGame.Wall; -public class AnimationAndKeys2 extends JFrame implements KeyListener { +public class Walls extends JFrame implements KeyListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds @@ -90,23 +93,7 @@ class Wall extends Rectangle{ } } - class Tank extends Rectangle{ - int angle; - int speed; - double xx = 100; - double yy = 200; - Tank(int x, int y){ - this.xx = x; - this.yy = y; - this.angle = 0; - this.x = x; - this.y = y; - this.width = 20; - this.height = 30; - this.angle = 90; - this.speed = 5; - } - } + Tank tank1 = new Tank(100,100); Tank tank2 = new Tank(200,200); @@ -206,7 +193,7 @@ void moveTank() { ArrayList bulletCounter = new ArrayList(); - AnimationAndKeys2() throws InterruptedException { // this exception is required for Thread.sleep() + Walls() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); animationTimer.start(); createMap(); @@ -290,11 +277,29 @@ public void paintComponent(Graphics g) { // Rotate the rectangle around its center g2.rotate(Math.toRadians(tank1.angle), tank1.x + tank1.width/2, tank1.y + tank1.height/2); - g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); + + if (tank1.img == null){ + g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); + } + else{ + g.drawImage(tank1.img2, tank1.x, tank1.y, tank1.width, tank1.height, null); + + } + + //g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); g2.setTransform(oldTransform); g2.rotate(Math.toRadians(tank2.angle), tank2.x + tank2.width/2, tank2.y + tank2.height/2); - g.fillRect(tank2.x, tank2.y, tank2.width, tank2.height); + + if (tank1.img == null){ + g.fillRect(tank2.x, tank2.y, tank2.width, tank2.height); + } + else{ + g.drawImage(tank2.img, tank2.x, tank2.y, tank2.width, tank2.height, null); + + } + + //g.fillRect(tank2.x, tank2.y, tank2.width, tank2.height); g2.setTransform(oldTransform); for(Rectangle w : walls){ @@ -361,6 +366,22 @@ public void keyTyped(KeyEvent e) { } public static void main(String[] args) throws InterruptedException { - new AnimationAndKeys2(); + new Walls(); } + + static BufferedImage loadImage(String filename) { + BufferedImage img = null; + try { + img = ImageIO.read(new File(filename)); + } catch (IOException e) { + System.out.println(e.toString()); + JOptionPane.showMessageDialog(null, "An image failed to load: " + filename , "ERROR", JOptionPane.ERROR_MESSAGE); + } + //DEBUG + //if (img == null) System.out.println("null"); + //else System.out.printf("w=%d, h=%d%n",img.getWidth(),img.getHeight()); + + return img; + } + } diff --git a/src/game/tank1.png b/src/game/tank1.png new file mode 100644 index 0000000000000000000000000000000000000000..66ec198f2d733861161ff18439e0a6b41c85a41b GIT binary patch literal 718 zcmeAS@N?(olHy`uVBq!ia0vp^%0R5a!3HEtm=!=nA~QoGN+NuH ztdjF{^%7I^lT!66atlBT7;Gx6fXv*~l0=1y+?>2(s|s5sunH?68zii+qySb@l5MLL z;TxdfoL`ixV5VoFXP{)qrJ$f-QP|K%!RuMVYBUn?dH=8QOqVqKHB5vC#)v zj^r4KPr;%m?4*KWVS z<0}qitp2R1;_-0jXU)gYExR+N#MrdV9=LoH_+024P~-Y;`^g%$#&!X(Lv1Oa@hKh|=1}P!j61(_+s{vGcLzncr>mdKI;Vst0NyUv A_W%F@ literal 0 HcmV?d00001 diff --git a/src/game/tank2.png b/src/game/tank2.png new file mode 100644 index 0000000000000000000000000000000000000000..1e0b2409732086fd031d619cbdd08c5a2d361de4 GIT binary patch literal 691 zcmeAS@N?(olHy`uVBq!ia0vp^%0R5a!3HEtm=!=nA~QoGN+NuH ztdjF{^%7I^lT!66atlBT7;Gx6fXv*~l0=1y+?>2(s|s5sunH?68zii+qySb@l5MLL z;TxdfoL`ixV5VoFXP{)qrJ$f-QP|K%!RuMVYBUn?dH=8QOqVqKHB5vC#)v zj^r4KPr;%b!hW)~8_Z~U}TUc0LH{q>n=+xBnId8Pl%C?(E#mLl_!vps1i z7z&LSOBy-cIg`=GBfxJvh0WNmy70xR&F|~#E;FQ0lV2uzb^_z0v}G-lQ9 Date: Thu, 26 Jan 2023 11:34:06 -0500 Subject: [PATCH 16/24] separation of classes --- src/game/Ball.java | 21 ++++++++++++++++ src/game/Wall.java | 20 +++++++++++++++ src/game/Walls.java | 61 +++++++++++++++++---------------------------- 3 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 src/game/Ball.java create mode 100644 src/game/Wall.java diff --git a/src/game/Ball.java b/src/game/Ball.java new file mode 100644 index 0000000..3c96c52 --- /dev/null +++ b/src/game/Ball.java @@ -0,0 +1,21 @@ +package game; +import java.awt.Rectangle; + +class Ball extends Rectangle{ + // int x,y; //position + double vx, vy = 0; //speed + int size = 5; + boolean intersecting; + boolean player1sbullet; + + Ball(boolean player1sbullet){ + this.x = -10; + this.y = -10; + this.width = size; + this.height = size; + this.vx = vx; //move this many pixels each time + this.vy = vy; + this.intersecting = false; + this.player1sbullet = player1sbullet; + } +} diff --git a/src/game/Wall.java b/src/game/Wall.java new file mode 100644 index 0000000..0e284a7 --- /dev/null +++ b/src/game/Wall.java @@ -0,0 +1,20 @@ +package game; +import java.awt.Rectangle; + +class Wall extends Rectangle{ + //int x,y,width,hight; //position + boolean v = true; + Wall(int x, int y, boolean vertical){ + this.x = x; + this.y = y; + this.v = vertical; + if(vertical) { + this.width = 2; + this.height = 100; + } + else { + this.width = 100; + this.height = 2; + } + } +} \ No newline at end of file diff --git a/src/game/Walls.java b/src/game/Walls.java index 254c7f6..ea96ddb 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -21,6 +21,7 @@ import java.io.*; import javax.imageio.ImageIO; import javax.swing.JOptionPane; +import java.lang.Math; //import Graphics.TankGame.DrawingPanel; //import Graphics.TankGame.Tank; @@ -37,7 +38,7 @@ public class Walls extends JFrame implements KeyListener { static final int TIMERSPEED = 10; static final int delayTime = 50; int bullets = 30; // bullet counter - + Timer animationTimer = new Timer(4, new Timer1()); int actualTime1 = 0; @@ -56,43 +57,6 @@ public void actionPerformed(ActionEvent e) { } } - class Ball extends Rectangle{ - // int x,y; //position - double vx, vy = 0; //speed - int size = 5; - boolean intersecting; - boolean player1sbullet; - - Ball(boolean player1sbullet){ - this.x = -10; - this.y = -10; - this.width = size; - this.height = size; - this.vx = vx; //move this many pixels each time - this.vy = vy; - this.intersecting = false; - this.player1sbullet = player1sbullet; - } - } - - class Wall extends Rectangle{ - //int x,y,width,hight; //position - boolean v = true; - Wall(int x, int y, boolean vertical){ - this.x = x; - this.y = y; - this.v = vertical; - if(vertical) { - this.width = 2; - this.height = 100; - } - else { - this.width = 100; - this.height = 2; - } - } - } - Tank tank1 = new Tank(100,100); Tank tank2 = new Tank(200,200); @@ -213,10 +177,31 @@ void moveTank() { } public void createMap(){ + + + } + + public void map1(){ walls.add(new Wall(200,200, true)); walls.add(new Wall(250,200, false)); } + public void map2(){ + + } + + public void map3(){ + + } + + public void map4(){ + + } + + public void map5(){ + + } + public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { for(Wall w : walls){ From d854079872e98a08ba76e46d75847255e3c2f31a Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Thu, 26 Jan 2023 13:24:13 -0500 Subject: [PATCH 17/24] full game not fully commented --- src/game/Ball.java | 1 + src/game/Wall.java | 4 +- src/game/Walls.java | 204 +++++++++++++++++++++++--------------------- 3 files changed, 109 insertions(+), 100 deletions(-) diff --git a/src/game/Ball.java b/src/game/Ball.java index 3c96c52..150aa9a 100644 --- a/src/game/Ball.java +++ b/src/game/Ball.java @@ -3,6 +3,7 @@ class Ball extends Rectangle{ // int x,y; //position + double xx, yy = -10; double vx, vy = 0; //speed int size = 5; boolean intersecting; diff --git a/src/game/Wall.java b/src/game/Wall.java index 0e284a7..c2947c2 100644 --- a/src/game/Wall.java +++ b/src/game/Wall.java @@ -9,12 +9,12 @@ class Wall extends Rectangle{ this.y = y; this.v = vertical; if(vertical) { - this.width = 2; + this.width = 3; this.height = 100; } else { this.width = 100; - this.height = 2; + this.height = 3; } } } \ No newline at end of file diff --git a/src/game/Walls.java b/src/game/Walls.java index ea96ddb..b860b35 100644 --- a/src/game/Walls.java +++ b/src/game/Walls.java @@ -23,20 +23,16 @@ import javax.swing.JOptionPane; import java.lang.Math; -//import Graphics.TankGame.DrawingPanel; -//import Graphics.TankGame.Tank; -//import Graphics.TankGame.Timer1; - -// import Graphics.TankGame.Ball; -// import Graphics.TankGame.Tank; -// import Graphics.TankGame.Wall; - public class Walls extends JFrame implements KeyListener { static final int PANW = 500; static final int PANH = 400; static final int SLEEPTIME = 10; //in milliseconds static final int TIMERSPEED = 10; static final int delayTime = 50; + static int blueScore = 0; + static int redScore = 0; + static String blueScoreSTR = "0"; + static String redScoreSTR = "0"; int bullets = 30; // bullet counter @@ -58,8 +54,8 @@ public void actionPerformed(ActionEvent e) { } - Tank tank1 = new Tank(100,100); - Tank tank2 = new Tank(200,200); + Tank tank1 = new Tank(50,60); + Tank tank2 = new Tank(400,300); void createGUI() { @@ -68,21 +64,22 @@ void createGUI() { window.setSize(PANW, PANH); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLocationRelativeTo(null); + window.setResizable(false); panel = new DrawingPanel(); panel.setBackground(Color.WHITE); panel.addKeyListener(this); panel.setFocusable(true); panel.requestFocus(); + window.add(panel); window.setVisible(true); } - - boolean checkTankintersect(Tank tank, boolean wallIsVertical){ + boolean checkTankintersect(Tank tank){ for(Wall w : walls){ - if (w.intersects(tank) && w.v == wallIsVertical){ + if (w.intersects(tank)){ return true; } } @@ -94,23 +91,21 @@ void moveTank() { if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; if (isKeyDown(KeyEvent.VK_W)){ - tank1.yy += Math.cos(Math.toRadians(tank1.angle))*2; - tank1.xx -= Math.sin(Math.toRadians(tank1.angle))*2; - if(checkTankintersect(tank1, false)){ - tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*3; - } - if(checkTankintersect(tank1, true)){ - tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*3; + if(checkTankintersect(tank1)){ + tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*1.5; + tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*1.5; + }else{ + tank1.yy += Math.cos(Math.toRadians(tank1.angle))*1.5; + tank1.xx -= Math.sin(Math.toRadians(tank1.angle))*1.5; } } if (isKeyDown(KeyEvent.VK_S)) { - tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*2; - tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*2; - if(checkTankintersect(tank1, false)){ - tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*2; - } - if(checkTankintersect(tank1, true)){ - tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*2; + if(checkTankintersect(tank1)){ + tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*1.5; + tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*1.5; + }else{ + tank1.yy -= Math.cos(Math.toRadians(tank1.angle))*1.5; + tank1.xx += Math.sin(Math.toRadians(tank1.angle))*1.5; } } if (isKeyDown(KeyEvent.VK_C) && actualTime1 > delayTime) { @@ -122,23 +117,21 @@ void moveTank() { if (isKeyDown(KeyEvent.VK_LEFT)) tank2.angle-=5; if (isKeyDown(KeyEvent.VK_RIGHT)) tank2.angle+=5; if (isKeyDown(KeyEvent.VK_UP)){ - tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*2; - tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*2; - if(checkTankintersect(tank2, false)){ - tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*3; - } - if(checkTankintersect(tank2, true)){ - tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*3; + if(checkTankintersect(tank2)){ + tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*1.5; + tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*1.5; + }else{ + tank2.yy += Math.cos(Math.toRadians(tank2.angle))*1.5; + tank2.xx -= Math.sin(Math.toRadians(tank2.angle))*1.5; } } if (isKeyDown(KeyEvent.VK_DOWN)) { - tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*2; - tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*2; - if(checkTankintersect(tank2, false)){ - tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*3; - } - if(checkTankintersect(tank2, true)){ - tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*3; + if(checkTankintersect(tank2)){ + tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*1.5; + tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*1.5; + }else{ + tank2.yy -= Math.cos(Math.toRadians(tank2.angle))*1.5; + tank2.xx += Math.sin(Math.toRadians(tank2.angle))*1.5; } } if (isKeyDown(KeyEvent.VK_SPACE) && actualTime2 > delayTime) { @@ -154,6 +147,7 @@ void moveTank() { } ArrayList walls = new ArrayList(); + ArrayList bulletCounter = new ArrayList(); @@ -172,34 +166,23 @@ void moveTank() { while(isPlaying) { moveTank(); + hitTank(); Thread.sleep(SLEEPTIME); } } - public void createMap(){ - - - } - - public void map1(){ - walls.add(new Wall(200,200, true)); - walls.add(new Wall(250,200, false)); - } - - public void map2(){ - - } - - public void map3(){ - - } - - public void map4(){ - - } - - public void map5(){ - + public void createMap() { //make the walls and add to walls arraylist + walls.add(new Wall(0,100, false)); + walls.add(new Wall(0,200, false)); + walls.add(new Wall(0,250, false)); + walls.add(new Wall(130,100, true)); + walls.add(new Wall(500,100, false)); + walls.add(new Wall(350,200, true)); + walls.add(new Wall(500,100, false)); + walls.add(new Wall(400,250, false)); + walls.add(new Wall(200,100, true)); + walls.add(new Wall(250,140, false)); + walls.add(new Wall(200,250, true)); } public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { @@ -208,31 +191,28 @@ public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { if (w.intersects(b) && !b.intersecting){ if (w.v){ b.vx *= -1; - }else{ + } + else{ b.vy *= -1; } b.intersecting = true; - }else{ + } + else{ b.intersecting = false; } } - b.x += b.vx; - b.y += b.vy; + b.xx += b.vx; + b.yy += b.vy; + b.x= (int)b.xx; + b.y= (int)b.yy; if (b.x + b.size < 0 ||b.x + b.size > PANW) { b.vx *= -1; } - - // if (b.y < 0) { - // b.vy *= -1;// bounce off the top if it's moving up - // //b.y -= b.vy; // undo the last move <<< this may not be necessary - // } - if (b.y + b.size > PANH || b.y + b.size < 0) { //bounce off bottom b.vy *= -1; - } - + } } class DrawingPanel extends JPanel { @@ -240,7 +220,6 @@ class DrawingPanel extends JPanel { this.setBackground(Color.LIGHT_GRAY); this.setPreferredSize(new Dimension(PANW,PANH)); //remember that the JPanel size is more accurate than JFrame. this.setFont(new Font("Sans Serif", Font.PLAIN, 20)); - } @Override @@ -248,25 +227,27 @@ public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); - //g.fillRect(tank1.x, tank1.y, ship.width, ship.height); - //g.drawString("Weapon = " + bulletCounter, 30, 20); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g2.setStroke(new BasicStroke(2)); - g.drawString("Here is your drawing panel", 100,100); - + g2.setStroke(new BasicStroke(2)); + g2.setColor(Color.RED); + g.drawString(blueScoreSTR,400,50); + g2.setColor(new Color(50, 170, 255)); + g.drawString(redScoreSTR,450,50); + g2.setColor(Color.BLACK); + // Save the current transformation AffineTransform oldTransform = g2.getTransform(); // Rotate the rectangle around its center g2.rotate(Math.toRadians(tank1.angle), tank1.x + tank1.width/2, tank1.y + tank1.height/2); - if (tank1.img == null){ + if (tank1.img == null) { g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); } - else{ + else { g.drawImage(tank1.img2, tank1.x, tank1.y, tank1.width, tank1.height, null); } @@ -283,8 +264,6 @@ public void paintComponent(Graphics g) { g.drawImage(tank2.img, tank2.x, tank2.y, tank2.width, tank2.height, null); } - - //g.fillRect(tank2.x, tank2.y, tank2.width, tank2.height); g2.setTransform(oldTransform); for(Rectangle w : walls){ @@ -300,12 +279,10 @@ public void paintComponent(Graphics g) { public void fire(int x, int y, double angle, boolean player1sbullet){ for(Ball b : bulletCounter){ if (b.player1sbullet == player1sbullet && b.vx == 0 && b.vy == 0){ - b.x=x; - b.y=y; - b.vx = Math.cos(Math.toRadians(angle))*5; - b.vy = Math.sin(Math.toRadians(angle))*5; - - System.out.println(Double.toString(b.vx)); + b.vx = Math.cos(Math.toRadians(angle))*1; + b.vy = Math.sin(Math.toRadians(angle))*1; + b.xx=x+b.vx*20; + b.yy=y+b.vy*20; break; } } @@ -341,6 +318,42 @@ public void keyPressed(KeyEvent e) { panel.repaint(); } + void hitTank(){ + for(Ball bullet: bulletCounter){ + if(bullet.intersects(tank1)){ + System.out.println("hit"); + bullet.xx=bullet.yy=-10; + bullet.vx=bullet.vy=0; + bullet.vx=bullet.vy=0; + for(Ball b : bulletCounter){ + b.xx=b.yy=-10; + b.vx=b.vy=0; + } + tank1.xx = 60; + tank1.yy = 50; + tank2.xx = 400; + tank2.yy = 300; + blueScore++; + blueScoreSTR = "" + blueScore; + } + if(bullet.intersects(tank2)){ + System.out.println("2"); + bullet.xx=bullet.yy=-10; + bullet.vx=bullet.vy=0; + for(Ball b : bulletCounter){ + b.xx=b.yy=-10; + b.vx=b.vy=0; + } + tank1.xx = 60; + tank1.yy = 50; + tank2.xx = 400; + tank2.yy = 300; + redScore++; + redScoreSTR = "" + redScore; + } + } + } + @Override public void keyReleased(KeyEvent e) { keysDown[e.getKeyCode()] = false; @@ -362,11 +375,6 @@ static BufferedImage loadImage(String filename) { System.out.println(e.toString()); JOptionPane.showMessageDialog(null, "An image failed to load: " + filename , "ERROR", JOptionPane.ERROR_MESSAGE); } - //DEBUG - //if (img == null) System.out.println("null"); - //else System.out.printf("w=%d, h=%d%n",img.getWidth(),img.getHeight()); - return img; } - } From 2c46e8798cce2d7b86398d690484a40ef80be433 Mon Sep 17 00:00:00 2001 From: DeckerScrivens <121455996+DeckerScrivens@users.noreply.github.com> Date: Thu, 26 Jan 2023 13:27:42 -0500 Subject: [PATCH 18/24] renamed file to mainGameFile --- src/game/{Walls.java => mainGameFile.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/game/{Walls.java => mainGameFile.java} (100%) diff --git a/src/game/Walls.java b/src/game/mainGameFile.java similarity index 100% rename from src/game/Walls.java rename to src/game/mainGameFile.java From 143cba8a5808cf59c0059d56b14a9ee4cd625574 Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Sat, 28 Jan 2023 18:56:16 -0500 Subject: [PATCH 19/24] added comments i think i added everything on the parts i wrote. --- src/game/mainGameFile.java | 124 +++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 48 deletions(-) diff --git a/src/game/mainGameFile.java b/src/game/mainGameFile.java index b860b35..f82383c 100644 --- a/src/game/mainGameFile.java +++ b/src/game/mainGameFile.java @@ -1,4 +1,6 @@ package game; +//James, Tian, Decker +//Jan 28th import java.awt.BasicStroke; import java.awt.Color; @@ -33,7 +35,6 @@ public class Walls extends JFrame implements KeyListener { static int redScore = 0; static String blueScoreSTR = "0"; static String redScoreSTR = "0"; - int bullets = 30; // bullet counter Timer animationTimer = new Timer(4, new Timer1()); @@ -77,7 +78,9 @@ void createGUI() { window.setVisible(true); } + //This method will check and see if a tank is touching any walls boolean checkTankintersect(Tank tank){ + //Cycles through all the walls for(Wall w : walls){ if (w.intersects(tank)){ return true; @@ -86,19 +89,25 @@ boolean checkTankintersect(Tank tank){ return false; } - // With this method, two keys can be down at once allowing smooth diagonal movement + // This is what moves the tanks, two keys can be down at once allowing smooth diagonal movement void moveTank() { - if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; + + //these will adjust the angle of the tank + if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; + + //this will move it forward based off the angle if (isKeyDown(KeyEvent.VK_W)){ - if(checkTankintersect(tank1)){ + if(checkTankintersect(tank1)){//if it touches a wall, move backwards tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*1.5; tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*1.5; - }else{ + }else{ //otherwise move forwards tank1.yy += Math.cos(Math.toRadians(tank1.angle))*1.5; tank1.xx -= Math.sin(Math.toRadians(tank1.angle))*1.5; } } + + //same but backwards if (isKeyDown(KeyEvent.VK_S)) { if(checkTankintersect(tank1)){ tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*1.5; @@ -108,12 +117,15 @@ void moveTank() { tank1.xx += Math.sin(Math.toRadians(tank1.angle))*1.5; } } + + //this calls the fire bullet method if (isKeyDown(KeyEvent.VK_C) && actualTime1 > delayTime) { fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); actualTime1 = 0; } + //same for tank2 if (isKeyDown(KeyEvent.VK_LEFT)) tank2.angle-=5; if (isKeyDown(KeyEvent.VK_RIGHT)) tank2.angle+=5; if (isKeyDown(KeyEvent.VK_UP)){ @@ -139,6 +151,7 @@ void moveTank() { actualTime2 = 0; } + //converts all the doubles to ints to be painted tank1.x = (int)tank1.xx; tank1.y = (int)tank1.yy; tank2.x = (int)tank2.xx; @@ -147,26 +160,24 @@ void moveTank() { } ArrayList walls = new ArrayList(); - - ArrayList bulletCounter = new ArrayList(); + ArrayList bulletCounter = new ArrayList(); - - Walls() throws InterruptedException { // this exception is required for Thread.sleep() + Walls() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); animationTimer.start(); - createMap(); + createMap(); //creates the map - for (int i = 0;i<300;i++){ // creates 30 bullets for player 1 off screen + for (int i = 0;i<30;i++){ // creates 30 bullets for player 1 off screen bulletCounter.add(new Ball(true)); } - for (int i = 0;i<300;i++){ // creates 30 bullets for player 2 off screen + for (int i = 0;i<30;i++){ // creates 30 bullets for player 2 off screen bulletCounter.add(new Ball(false)); } while(isPlaying) { - moveTank(); - hitTank(); + moveTank();//moves the tanks + hitTank();//checks if a tank has been hit by any bullets Thread.sleep(SLEEPTIME); } } @@ -185,37 +196,43 @@ public void createMap() { //make the walls and add to walls arraylist walls.add(new Wall(200,250, true)); } - public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { + //this will move the bullets + public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { + //this will go through all the walls for(Wall w : walls){ - if (w.intersects(b) && !b.intersecting){ - if (w.v){ - b.vx *= -1; - } + if (w.intersects(b) && !b.intersecting){ //will only check if the bullets is not already intersecting a wall, to stop it from going along the wall + //if the wall is vertical, it will reverse the bullets horizontal speed + if (w.v){ + b.vx *= -1; + } + //otherwise it will reverse the bullets vertical speed else{ - b.vy *= -1; - } - b.intersecting = true; - } + b.vy *= -1; + } + b.intersecting = true; //this boolean will tell it know not to check again untill it has cleared the wall + } else{ b.intersecting = false; } - } + } - b.xx += b.vx; + b.xx += b.vx; //changes the locaton of the bullet based on the speed b.yy += b.vy; - b.x= (int)b.xx; + b.x= (int)b.xx; // converts the location to int to be painted b.y= (int)b.yy; + //bounce the bullet off the sides of the screen if (b.x + b.size < 0 ||b.x + b.size > PANW) { b.vx *= -1; } + //bounce the bullet off the top and bottom of the screen if (b.y + b.size > PANH || b.y + b.size < 0) { //bounce off bottom b.vy *= -1; } } - class DrawingPanel extends JPanel { + class DrawingPanel extends JPanel { DrawingPanel() { this.setBackground(Color.LIGHT_GRAY); this.setPreferredSize(new Dimension(PANW,PANH)); //remember that the JPanel size is more accurate than JFrame. @@ -241,7 +258,7 @@ public void paintComponent(Graphics g) { // Save the current transformation AffineTransform oldTransform = g2.getTransform(); - // Rotate the rectangle around its center + // Rotate the tank around its center g2.rotate(Math.toRadians(tank1.angle), tank1.x + tank1.width/2, tank1.y + tank1.height/2); if (tank1.img == null) { @@ -252,9 +269,9 @@ public void paintComponent(Graphics g) { } - //g.fillRect(tank1.x, tank1.y, tank1.width, tank1.height); - g2.setTransform(oldTransform); + g2.setTransform(oldTransform);//rotates it back + //draws tank 2 the same way g2.rotate(Math.toRadians(tank2.angle), tank2.x + tank2.width/2, tank2.y + tank2.height/2); if (tank1.img == null){ @@ -264,23 +281,30 @@ public void paintComponent(Graphics g) { g.drawImage(tank2.img, tank2.x, tank2.y, tank2.width, tank2.height, null); } - g2.setTransform(oldTransform); + g2.setTransform(oldTransform); - for(Rectangle w : walls){ - g.fillRect(w.x, w.y, w.width, w.height); - } + //draws all the walls + for(Rectangle w : walls){ + g.fillRect(w.x, w.y, w.width, w.height); + } + //draws all the bullets for(Ball b : bulletCounter){ g.fillOval(b.x, b.y, b.width,b.height); - moveAndBounceBall(b, tank1, tank2); + moveAndBounceBall(b, tank1, tank2);//moves the ball every frame } } } - public void fire(int x, int y, double angle, boolean player1sbullet){ + //this method will fire the bullets + public void fire(int x, int y, double angle, boolean player1sbullet){ //it will take in the the x and y and angle of the tank that fired it, + //will cycle through all the bullets untill it finds one that is not in motion and belongs to the tank that fired it for(Ball b : bulletCounter){ if (b.player1sbullet == player1sbullet && b.vx == 0 && b.vy == 0){ + //calculates the horizontal and vetical speeds of the bullet based on the angle of the tank with some trig b.vx = Math.cos(Math.toRadians(angle))*1; b.vy = Math.sin(Math.toRadians(angle))*1; + //this will give the bullet a slight head start, otherwise the bullet would be intersecting with the tank + //as soon as it is fired, and the game would think it was hit b.xx=x+b.vx*20; b.yy=y+b.vy*20; break; @@ -318,28 +342,32 @@ public void keyPressed(KeyEvent e) { panel.repaint(); } + //this cecks to see if a tank is hit by a bullet void hitTank(){ + //cycles through all the bullets for(Ball bullet: bulletCounter){ - if(bullet.intersects(tank1)){ - System.out.println("hit"); - bullet.xx=bullet.yy=-10; - bullet.vx=bullet.vy=0; - bullet.vx=bullet.vy=0; + if(bullet.intersects(tank1)){ +// System.out.println("hit"); +// bullet.xx=bullet.yy=-10; //teleports the bullet off screen +// bullet.vx=bullet.vy=0; //sets the speeds to zero +// bullet.vx=bullet.vy=0; for(Ball b : bulletCounter){ - b.xx=b.yy=-10; - b.vx=b.vy=0; + b.xx=b.yy=-10; //teleports all the bullets off screen + b.vx=b.vy=0; //sets all the speeds to zero } - tank1.xx = 60; + tank1.xx = 60; //puts both tanks back to their starting points for the next round tank1.yy = 50; tank2.xx = 400; tank2.yy = 300; - blueScore++; + blueScore++; //increase the other players score blueScoreSTR = "" + blueScore; } + + //same for tank 2 if(bullet.intersects(tank2)){ - System.out.println("2"); - bullet.xx=bullet.yy=-10; - bullet.vx=bullet.vy=0; +// System.out.println("2"); +// bullet.xx=bullet.yy=-10; +// bullet.vx=bullet.vy=0; for(Ball b : bulletCounter){ b.xx=b.yy=-10; b.vx=b.vy=0; From 686160b92d174b03b136408a96bf7f59f1c7c06f Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Sat, 28 Jan 2023 19:00:10 -0500 Subject: [PATCH 20/24] added comments --- src/game/Ball.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/game/Ball.java b/src/game/Ball.java index 150aa9a..05bff48 100644 --- a/src/game/Ball.java +++ b/src/game/Ball.java @@ -1,15 +1,16 @@ package game; +//this class creates the bullets import java.awt.Rectangle; class Ball extends Rectangle{ - // int x,y; //position - double xx, yy = -10; + double xx, yy = -10; //position double vx, vy = 0; //speed - int size = 5; - boolean intersecting; - boolean player1sbullet; + int size = 5; //the diameter + boolean intersecting; //wether or not a bullet is touching a wall + boolean player1sbullet; //this shows who the bullet belongs to Ball(boolean player1sbullet){ + //puts them off screen, not moving this.x = -10; this.y = -10; this.width = size; @@ -17,6 +18,6 @@ class Ball extends Rectangle{ this.vx = vx; //move this many pixels each time this.vy = vy; this.intersecting = false; - this.player1sbullet = player1sbullet; + this.player1sbullet = player1sbullet; } } From 785069c02573896887ba9e3979aa97f0b328eba9 Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Sat, 28 Jan 2023 19:06:27 -0500 Subject: [PATCH 21/24] Update Tank.java not many comments i can add here --- src/game/Tank.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/game/Tank.java b/src/game/Tank.java index 12fac05..ba96d65 100644 --- a/src/game/Tank.java +++ b/src/game/Tank.java @@ -1,4 +1,5 @@ package game; +//this will create the tank objects import java.awt.Rectangle; import java.awt.image.BufferedImage; class Tank extends Rectangle{ @@ -6,8 +7,8 @@ class Tank extends Rectangle{ BufferedImage img2; int angle; int speed; - double xx = 100; - double yy = 200; + double xx; //position + double yy; Tank(int x, int y){ this.xx = x; this.yy = y; @@ -22,4 +23,4 @@ class Tank extends Rectangle{ img2 = Walls.loadImage("src/game/tank1.png"); } -} \ No newline at end of file +} From 03e9cc0b24bac6135916cb42af5ae791e2a9afa9 Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Sat, 28 Jan 2023 19:08:15 -0500 Subject: [PATCH 22/24] comments --- src/game/Wall.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/game/Wall.java b/src/game/Wall.java index c2947c2..ddbfa1a 100644 --- a/src/game/Wall.java +++ b/src/game/Wall.java @@ -1,13 +1,15 @@ package game; +//this will create the walls import java.awt.Rectangle; class Wall extends Rectangle{ //int x,y,width,hight; //position - boolean v = true; + boolean v; //wether or not it is vertical Wall(int x, int y, boolean vertical){ this.x = x; this.y = y; this.v = vertical; + //sets the width and height based on wether or not it is vertical if(vertical) { this.width = 3; this.height = 100; @@ -17,4 +19,4 @@ class Wall extends Rectangle{ this.height = 3; } } -} \ No newline at end of file +} From 0030b0d137eb011d979ee2fc220feb1c4d0f24dd Mon Sep 17 00:00:00 2001 From: TianYao12 <110309420+TianYao12@users.noreply.github.com> Date: Sat, 28 Jan 2023 19:58:29 -0500 Subject: [PATCH 23/24] updated with comments --- src/game/mainGameFile.java | 223 ++++++++++++++++++------------------- 1 file changed, 110 insertions(+), 113 deletions(-) diff --git a/src/game/mainGameFile.java b/src/game/mainGameFile.java index f82383c..610f4f3 100644 --- a/src/game/mainGameFile.java +++ b/src/game/mainGameFile.java @@ -1,6 +1,6 @@ package game; -//James, Tian, Decker -//Jan 28th +// James, Tian, Decker +// Jan 28th import java.awt.BasicStroke; import java.awt.Color; @@ -26,39 +26,38 @@ import java.lang.Math; public class Walls extends JFrame implements KeyListener { + static final int PANW = 500; static final int PANH = 400; - static final int SLEEPTIME = 10; //in milliseconds + + static final int SLEEPTIME = 10; // in milliseconds static final int TIMERSPEED = 10; - static final int delayTime = 50; + static final int delayTime = 50; // delay time for tank shooting + static int blueScore = 0; static int redScore = 0; static String blueScoreSTR = "0"; static String redScoreSTR = "0"; - + Tank tank1 = new Tank(50,60); // creates first tank + Tank tank2 = new Tank(400,300); // creates second tank + Timer animationTimer = new Timer(4, new Timer1()); - int actualTime1 = 0; - int actualTime2 = 0; - + int actualTime1 = 0; // time since last shot for tank1 + int actualTime2 = 0; // time since last shot for tank2 DrawingPanel panel; boolean isPlaying = true; - class Timer1 implements ActionListener{ + class Timer1 implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - actualTime1++; - actualTime2++; - panel.repaint(); + actualTime1++; // increase time since last shot for tank1 + actualTime2++; // increase time since last shot for tank2 + panel.repaint(); } } - - Tank tank1 = new Tank(50,60); - Tank tank2 = new Tank(400,300); - - void createGUI() { JFrame window = new JFrame("Tank Trouble"); @@ -72,76 +71,71 @@ void createGUI() { panel.addKeyListener(this); panel.setFocusable(true); panel.requestFocus(); - - + window.add(panel); window.setVisible(true); } - //This method will check and see if a tank is touching any walls - boolean checkTankintersect(Tank tank){ - //Cycles through all the walls - for(Wall w : walls){ - if (w.intersects(tank)){ + + boolean checkTankintersect(Tank tank) { // checks if a tank is touching any walls + for(Wall w : walls) { // cycles through all the walls + if (w.intersects(tank)) { return true; } } return false; } - // This is what moves the tanks, two keys can be down at once allowing smooth diagonal movement + // moves tanks; two keys can be down at once allowing for smooth diagonal movement void moveTank() { + // adjust the angle of the tank + if (isKeyDown(KeyEvent.VK_A)) tank1.angle -= 5; + if (isKeyDown(KeyEvent.VK_D)) tank1.angle += 5; - //these will adjust the angle of the tank - if (isKeyDown(KeyEvent.VK_A)) tank1.angle-=5; - if (isKeyDown(KeyEvent.VK_D)) tank1.angle+=5; - - //this will move it forward based off the angle - if (isKeyDown(KeyEvent.VK_W)){ - if(checkTankintersect(tank1)){//if it touches a wall, move backwards - tank1.yy -=Math.cos(Math.toRadians(tank1.angle))*1.5; - tank1.xx +=Math.sin(Math.toRadians(tank1.angle))*1.5; - }else{ //otherwise move forwards + // move it forward based off the angle + if (isKeyDown(KeyEvent.VK_W)) { + if(checkTankintersect(tank1)) { // if it touches a wall, move backwards + tank1.yy -= Math.cos(Math.toRadians(tank1.angle))*1.5; + tank1.xx += Math.sin(Math.toRadians(tank1.angle))*1.5; + }else { // otherwise move forwards tank1.yy += Math.cos(Math.toRadians(tank1.angle))*1.5; tank1.xx -= Math.sin(Math.toRadians(tank1.angle))*1.5; } } - //same but backwards + // same but for backwards key if (isKeyDown(KeyEvent.VK_S)) { - if(checkTankintersect(tank1)){ - tank1.yy +=Math.cos(Math.toRadians(tank1.angle))*1.5; - tank1.xx -=Math.sin(Math.toRadians(tank1.angle))*1.5; - }else{ + if(checkTankintersect(tank1)) { + tank1.yy += Math.cos(Math.toRadians(tank1.angle))*1.5; + tank1.xx -= Math.sin(Math.toRadians(tank1.angle))*1.5; + }else { tank1.yy -= Math.cos(Math.toRadians(tank1.angle))*1.5; tank1.xx += Math.sin(Math.toRadians(tank1.angle))*1.5; } } - //this calls the fire bullet method - if (isKeyDown(KeyEvent.VK_C) && actualTime1 > delayTime) { + if (isKeyDown(KeyEvent.VK_C) && actualTime1 > delayTime) { // calls fire bullet method as long as the shooting delay time has passed fire(tank1.x+tank1.width/2, tank1.y+tank1.height/2, tank1.angle+90, true); - actualTime1 = 0; + actualTime1 = 0; // set time since last shot to zero } - - //same for tank2 - if (isKeyDown(KeyEvent.VK_LEFT)) tank2.angle-=5; - if (isKeyDown(KeyEvent.VK_RIGHT)) tank2.angle+=5; - if (isKeyDown(KeyEvent.VK_UP)){ - if(checkTankintersect(tank2)){ - tank2.yy -=Math.cos(Math.toRadians(tank2.angle))*1.5; - tank2.xx +=Math.sin(Math.toRadians(tank2.angle))*1.5; - }else{ + // Same for tank2 + if (isKeyDown(KeyEvent.VK_LEFT)) tank2.angle -= 5; + if (isKeyDown(KeyEvent.VK_RIGHT)) tank2.angle += 5; + if (isKeyDown(KeyEvent.VK_UP)) { + if(checkTankintersect(tank2)) { + tank2.yy -= Math.cos(Math.toRadians(tank2.angle))*1.5; + tank2.xx += Math.sin(Math.toRadians(tank2.angle))*1.5; + }else { tank2.yy += Math.cos(Math.toRadians(tank2.angle))*1.5; tank2.xx -= Math.sin(Math.toRadians(tank2.angle))*1.5; } } if (isKeyDown(KeyEvent.VK_DOWN)) { - if(checkTankintersect(tank2)){ - tank2.yy +=Math.cos(Math.toRadians(tank2.angle))*1.5; - tank2.xx -=Math.sin(Math.toRadians(tank2.angle))*1.5; - }else{ + if(checkTankintersect(tank2)) { + tank2.yy += Math.cos(Math.toRadians(tank2.angle))*1.5; + tank2.xx -= Math.sin(Math.toRadians(tank2.angle))*1.5; + }else { tank2.yy -= Math.cos(Math.toRadians(tank2.angle))*1.5; tank2.xx += Math.sin(Math.toRadians(tank2.angle))*1.5; } @@ -151,21 +145,21 @@ void moveTank() { actualTime2 = 0; } - //converts all the doubles to ints to be painted + // converts all the doubles to ints to be painted tank1.x = (int)tank1.xx; tank1.y = (int)tank1.yy; tank2.x = (int)tank2.xx; tank2.y = (int)tank2.yy; - panel.repaint(); + panel.repaint(); } - ArrayList walls = new ArrayList(); - ArrayList bulletCounter = new ArrayList(); + ArrayList walls = new ArrayList(); // arraylist that stores walls to be painted + ArrayList bulletCounter = new ArrayList(); // arraylist that stores bullets Walls() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); animationTimer.start(); - createMap(); //creates the map + createMap(); // creates the map for (int i = 0;i<30;i++){ // creates 30 bullets for player 1 off screen bulletCounter.add(new Ball(true)); @@ -176,13 +170,13 @@ void moveTank() { } while(isPlaying) { - moveTank();//moves the tanks - hitTank();//checks if a tank has been hit by any bullets + moveTank(); // moves the tanks + hitTank(); // checks if a tank has been hit by any bullets Thread.sleep(SLEEPTIME); } } - public void createMap() { //make the walls and add to walls arraylist + public void createMap() { // makes the walls and add to walls arraylist walls.add(new Wall(0,100, false)); walls.add(new Wall(0,200, false)); walls.add(new Wall(0,250, false)); @@ -196,38 +190,38 @@ public void createMap() { //make the walls and add to walls arraylist walls.add(new Wall(200,250, true)); } - //this will move the bullets + // move the bullets public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { - //this will go through all the walls - for(Wall w : walls){ - if (w.intersects(b) && !b.intersecting){ //will only check if the bullets is not already intersecting a wall, to stop it from going along the wall - //if the wall is vertical, it will reverse the bullets horizontal speed + // this will go through all the walls + for(Wall w : walls) { + if (w.intersects(b) && !b.intersecting) { // will only check if the bullets is not already intersecting a wall, to stop it from going along the wall + // if the wall is vertical, it will reverse the bullets horizontal speed if (w.v){ b.vx *= -1; } - //otherwise it will reverse the bullets vertical speed - else{ + // otherwise the wall is horizontal and it will reverse the bullets vertical speed + else { b.vy *= -1; } - b.intersecting = true; //this boolean will tell it know not to check again untill it has cleared the wall + b.intersecting = true; // this boolean will tell it know not to check again until it has cleared the wall } - else{ + else { b.intersecting = false; } } - b.xx += b.vx; //changes the locaton of the bullet based on the speed + b.xx += b.vx; // changes the locaton of the bullet based on the speed b.yy += b.vy; - b.x= (int)b.xx; // converts the location to int to be painted - b.y= (int)b.yy; + b.x = (int)b.xx; // converts the location to int to be painted + b.y = (int)b.yy; - //bounce the bullet off the sides of the screen + // bounce the bullet off the sides of the screen if (b.x + b.size < 0 ||b.x + b.size > PANW) { b.vx *= -1; } - //bounce the bullet off the top and bottom of the screen - if (b.y + b.size > PANH || b.y + b.size < 0) { //bounce off bottom + // bounce the bullet off the top and bottom of the screen + if (b.y + b.size > PANH || b.y + b.size < 0) { // bounce off bottom b.vy *= -1; } } @@ -235,7 +229,7 @@ public void moveAndBounceBall(Ball b, Tank tank1, Tank tank2) { class DrawingPanel extends JPanel { DrawingPanel() { this.setBackground(Color.LIGHT_GRAY); - this.setPreferredSize(new Dimension(PANW,PANH)); //remember that the JPanel size is more accurate than JFrame. + this.setPreferredSize(new Dimension(PANW,PANH)); // remember that the JPanel size is more accurate than JFrame. this.setFont(new Font("Sans Serif", Font.PLAIN, 20)); } @@ -248,17 +242,17 @@ public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g2.setStroke(new BasicStroke(2)); + g2.setStroke(new BasicStroke(2)); // scores for each tank g2.setColor(Color.RED); g.drawString(blueScoreSTR,400,50); g2.setColor(new Color(50, 170, 255)); g.drawString(redScoreSTR,450,50); g2.setColor(Color.BLACK); - // Save the current transformation + // save the current transformation AffineTransform oldTransform = g2.getTransform(); - // Rotate the tank around its center + // rotate the tank around its center g2.rotate(Math.toRadians(tank1.angle), tank1.x + tank1.width/2, tank1.y + tank1.height/2); if (tank1.img == null) { @@ -269,44 +263,44 @@ public void paintComponent(Graphics g) { } - g2.setTransform(oldTransform);//rotates it back + g2.setTransform(oldTransform); //rotates it back - //draws tank 2 the same way + // draws tank 2 the same way g2.rotate(Math.toRadians(tank2.angle), tank2.x + tank2.width/2, tank2.y + tank2.height/2); - if (tank1.img == null){ + if (tank1.img == null) { g.fillRect(tank2.x, tank2.y, tank2.width, tank2.height); } - else{ + else { g.drawImage(tank2.img, tank2.x, tank2.y, tank2.width, tank2.height, null); } g2.setTransform(oldTransform); - //draws all the walls + // draws all the walls for(Rectangle w : walls){ g.fillRect(w.x, w.y, w.width, w.height); } - //draws all the bullets + // draws all the bullets for(Ball b : bulletCounter){ g.fillOval(b.x, b.y, b.width,b.height); - moveAndBounceBall(b, tank1, tank2);//moves the ball every frame + moveAndBounceBall(b, tank1, tank2); // moves the ball every frame } } } - //this method will fire the bullets - public void fire(int x, int y, double angle, boolean player1sbullet){ //it will take in the the x and y and angle of the tank that fired it, - //will cycle through all the bullets untill it finds one that is not in motion and belongs to the tank that fired it - for(Ball b : bulletCounter){ + // this method will fire the bullets + public void fire(int x, int y, double angle, boolean player1sbullet) { // it will take in the the x and y and angle of the tank that fired it, + // will cycle through all the bullets until it finds one that is not in motion and belongs to the tank that fired it + for(Ball b : bulletCounter) { if (b.player1sbullet == player1sbullet && b.vx == 0 && b.vy == 0){ //calculates the horizontal and vetical speeds of the bullet based on the angle of the tank with some trig b.vx = Math.cos(Math.toRadians(angle))*1; b.vy = Math.sin(Math.toRadians(angle))*1; - //this will give the bullet a slight head start, otherwise the bullet would be intersecting with the tank - //as soon as it is fired, and the game would think it was hit - b.xx=x+b.vx*20; - b.yy=y+b.vy*20; + // this gives the bullet a slight head start, otherwise it intersects tank + // as soon as it is fired, and the game would think it was hit + b.xx = x+b.vx*20; + b.yy = y+b.vy*20; break; } } @@ -342,33 +336,36 @@ public void keyPressed(KeyEvent e) { panel.repaint(); } - //this cecks to see if a tank is hit by a bullet - void hitTank(){ - //cycles through all the bullets - for(Ball bullet: bulletCounter){ - if(bullet.intersects(tank1)){ + // this checks to see if a tank is hit by a bullet + void hitTank() { + // cycles through all the bullets + for(Ball bullet: bulletCounter) { + if(bullet.intersects(tank1)) { // System.out.println("hit"); -// bullet.xx=bullet.yy=-10; //teleports the bullet off screen -// bullet.vx=bullet.vy=0; //sets the speeds to zero -// bullet.vx=bullet.vy=0; - for(Ball b : bulletCounter){ - b.xx=b.yy=-10; //teleports all the bullets off screen - b.vx=b.vy=0; //sets all the speeds to zero +// bullet.xx = bullet.yy = -10; // teleports the bullet off screen +// bullet.vx = bullet.vy = 0; // sets the speeds to zero +// bullet.vx = bullet.vy = 0; + + for(Ball b : bulletCounter) { + b.xx=b.yy=-10; // teleports all the bullets off screen + b.vx=b.vy=0; // sets all the speeds to zero } - tank1.xx = 60; //puts both tanks back to their starting points for the next round + + tank1.xx = 60; // puts both tanks back to their starting points for the next round tank1.yy = 50; tank2.xx = 400; tank2.yy = 300; - blueScore++; //increase the other players score + + blueScore++; // increase the other player's score blueScoreSTR = "" + blueScore; } //same for tank 2 - if(bullet.intersects(tank2)){ + if(bullet.intersects(tank2)) { // System.out.println("2"); // bullet.xx=bullet.yy=-10; // bullet.vx=bullet.vy=0; - for(Ball b : bulletCounter){ + for(Ball b : bulletCounter) { b.xx=b.yy=-10; b.vx=b.vy=0; } From 0d2ec9924e85cd94656144d8acd3b1177eea564a Mon Sep 17 00:00:00 2001 From: jcgullberg <120407863+jcgullberg@users.noreply.github.com> Date: Sat, 28 Jan 2023 22:12:42 -0500 Subject: [PATCH 24/24] final --- src/game/mainGameFile.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/game/mainGameFile.java b/src/game/mainGameFile.java index 610f4f3..ed6cda5 100644 --- a/src/game/mainGameFile.java +++ b/src/game/mainGameFile.java @@ -25,7 +25,7 @@ import javax.swing.JOptionPane; import java.lang.Math; -public class Walls extends JFrame implements KeyListener { +public class MainGame extends JFrame implements KeyListener { static final int PANW = 500; static final int PANH = 400; @@ -156,7 +156,7 @@ void moveTank() { ArrayList walls = new ArrayList(); // arraylist that stores walls to be painted ArrayList bulletCounter = new ArrayList(); // arraylist that stores bullets - Walls() throws InterruptedException { // this exception is required for Thread.sleep() + MainGame() throws InterruptedException { // this exception is required for Thread.sleep() createGUI(); animationTimer.start(); createMap(); // creates the map @@ -389,7 +389,7 @@ public void keyTyped(KeyEvent e) { } public static void main(String[] args) throws InterruptedException { - new Walls(); + new MainGame(); } static BufferedImage loadImage(String filename) {