Compare commits
15 Commits
a97e122e58
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f6aaa63a5f | |||
| bb85a44235 | |||
| 3e96095c66 | |||
| c3318f834c | |||
| 984c37a833 | |||
| ce61482b9e | |||
| 6e6c222d57 | |||
| d834371ca3 | |||
| 677eae9119 | |||
| 7f9cdfea63 | |||
|
|
94e4377364 | ||
|
|
76c4dea36a | ||
| d380615978 | |||
| 998b4d3b7d | |||
|
|
018b24bf71 |
29
.replit
@@ -1,29 +0,0 @@
|
||||
[agent]
|
||||
expertMode = true
|
||||
|
||||
[nix]
|
||||
packages = ["nvim.pkgs"]
|
||||
channel = "stable-25_05"
|
||||
|
||||
[workflows]
|
||||
runButton = "Project"
|
||||
|
||||
[[workflows.workflow]]
|
||||
name = "Project"
|
||||
mode = "parallel"
|
||||
author = "agent"
|
||||
|
||||
[[workflows.workflow.tasks]]
|
||||
task = "workflow.run"
|
||||
args = "Start application"
|
||||
|
||||
[[workflows.workflow]]
|
||||
name = "Start application"
|
||||
author = "agent"
|
||||
|
||||
[[workflows.workflow.tasks]]
|
||||
task = "shell.exec"
|
||||
args = "cd /home/runner/workspace/American-Identity-Project && java Display"
|
||||
|
||||
[workflows.workflow.metadata]
|
||||
outputType = "vnc"
|
||||
@@ -1,8 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Amendment extends Collectable {
|
||||
|
||||
public Amendment(int x, int y, int w, int h) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Amendment.png"));
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Brick extends Tile {
|
||||
|
||||
public Brick(int x, int y, int w, int h) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Bricks/Brick.png"));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Collectable extends Collidable {
|
||||
public Collectable(int x, int y, int w, int h, ImageIcon i) {
|
||||
super(x, y, w, h, i);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Collidable extends Sprite {
|
||||
int x, y, width, height;
|
||||
Rectangle rect;
|
||||
|
||||
public Collidable(int x1, int y1, int w, int h, ImageIcon icon) {
|
||||
super(icon);
|
||||
x = x1;
|
||||
y = y1;
|
||||
width = w;
|
||||
height = h;
|
||||
rect = new Rectangle(x1, y1, w, h);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
sprite = icon.getImage();
|
||||
g.drawImage(sprite, x, y, width, height, null);
|
||||
}
|
||||
|
||||
public boolean collidesWith(Collidable other) {
|
||||
return this.rect.intersects(other.rect);
|
||||
}
|
||||
|
||||
public void onCollide(Collidable other) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class Display {
|
||||
public static void main(String[] args) {
|
||||
int boardWidth = 800;
|
||||
int boardHeight = 600;
|
||||
int tileSize = 20;
|
||||
|
||||
JFrame game = new JFrame();
|
||||
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
game.setSize(boardWidth, boardHeight);
|
||||
game.setVisible(true);
|
||||
game.setLocationRelativeTo(null);
|
||||
game.setResizable(false);
|
||||
|
||||
Platformer platformer = new Platformer(boardWidth, boardHeight, tileSize);
|
||||
game.add(platformer);
|
||||
game.pack();
|
||||
platformer.requestFocus();
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Enemy extends Collidable {
|
||||
int xVelo, yVelo;
|
||||
boolean alive;
|
||||
|
||||
public Enemy(int x, int y, int w, int h, int level) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Enemies/" + level + ".png"));
|
||||
xVelo = 2;
|
||||
yVelo = 0;
|
||||
alive = true;
|
||||
}
|
||||
|
||||
public void moveX(int moveX) {
|
||||
this.x += moveX;
|
||||
this.rect.x = this.x;
|
||||
}
|
||||
|
||||
public void moveY(int moveY) {
|
||||
this.y += moveY;
|
||||
this.rect.y = this.y;
|
||||
}
|
||||
|
||||
public void patrol(ArrayList<Collidable> collidables) {
|
||||
moveX(xVelo);
|
||||
|
||||
for (Collidable c : collidables) {
|
||||
if (this.collidesWith(c)) {
|
||||
xVelo = -xVelo;
|
||||
moveX(xVelo * 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check edge detection - is there ground below next step?
|
||||
boolean edgeAhead = true;
|
||||
int nextX = this.x + xVelo;
|
||||
for (Collidable c : collidables) {
|
||||
if (c instanceof Tile) {
|
||||
Tile t = (Tile) c;
|
||||
// check if tile is below enemy's next position
|
||||
if (nextX + this.width > t.x && nextX < t.x + t.width && t.y == this.y + this.height) {
|
||||
edgeAhead = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (edgeAhead) {
|
||||
xVelo = -xVelo;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Flag extends Collidable {
|
||||
public Flag(int x, int y, int w, int h) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Flag.png"));
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y, int w, int h) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = w;
|
||||
this.height = h;
|
||||
this.rect.setBounds(x, y, w, h);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import java.awt.Graphics;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class InvisibleTile extends Tile {
|
||||
public InvisibleTile(int x, int y, int w, int h) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Bricks/Brick.png"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics g) {
|
||||
// draw nothing
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class LevelLoader {
|
||||
static int enemyWidth[] = {0, 31, 20, 20, 20, 20, 20, 29, 29, 20, 20};
|
||||
static int enemyHeight[] = {0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
|
||||
|
||||
public static void load(
|
||||
int tileSize,
|
||||
ArrayList<Collidable> collidables,
|
||||
ArrayList<Collectable> collectables,
|
||||
ArrayList<Enemy> enemies,
|
||||
Flag flag,
|
||||
Player player,
|
||||
int level)
|
||||
throws IOException {
|
||||
collidables.clear();
|
||||
collectables.clear();
|
||||
enemies.clear();
|
||||
|
||||
/* left wall */ collidables.add(new Brick(-20, 0, 20, 2000));
|
||||
|
||||
BufferedReader br = new BufferedReader(new FileReader("Levels/level" + level + ".txt"));
|
||||
String line;
|
||||
int row = 0;
|
||||
while ((line = br.readLine()) != null) {
|
||||
for (int col = 0; col < line.length(); col++) {
|
||||
char c = line.charAt(col);
|
||||
int x = col * tileSize;
|
||||
int y = row * tileSize;
|
||||
switch (c) {
|
||||
case 'B':
|
||||
collidables.add(new Brick(x, y, tileSize, tileSize));
|
||||
break;
|
||||
case 'Q':
|
||||
collidables.add(new PowerBrick(x, y, tileSize, tileSize, 1));
|
||||
break;
|
||||
case 'A':
|
||||
collectables.add(new Amendment(x, y, tileSize, tileSize));
|
||||
break;
|
||||
case 'F':
|
||||
flag.setPosition(x, y, tileSize, tileSize);
|
||||
break;
|
||||
case 'P':
|
||||
player.x = x;
|
||||
player.y = y;
|
||||
player.rect.x = x;
|
||||
player.rect.y = y;
|
||||
break;
|
||||
case 'E':
|
||||
enemies.add(new Enemy(x, y, enemyWidth[level], enemyHeight[level], level));
|
||||
break;
|
||||
case 'X':
|
||||
collidables.add(new InvisibleTile(x, y, tileSize, tileSize));
|
||||
break;
|
||||
}
|
||||
}
|
||||
row++;
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
....................................................................................................
|
||||
................................................................................
|
||||
.
|
||||
.
|
||||
.
|
||||
..................................................
|
||||
..............................BBBBB....BBBBB..................................A..........................
|
||||
............A.......BBBBB........................BBBBB.......BBBBB.........BBQBB...................................
|
||||
..........BBBBB.......................................................................BBBBB..............
|
||||
....................................................................................................
|
||||
.P........................A........E...................E...A...............E.....................F..
|
||||
BBBBBBBBBBBBBBBBBBBB..BBBBBBBBBBBBBBBBBBBBBBB..BBBBBBBBBBBBBBBBBBBBBBB..BBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
@@ -1,24 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..P................................................................................................................
|
||||
..............................................B...................................................................
|
||||
BBBBBBBBBBB...................................B..............................................................................
|
||||
..............................................B...................................................................
|
||||
..............................................B.......................................................................BBBBBBBBBBBBBBBBBBBBBBBBB.........................................
|
||||
..............................................B.......................................................................B..............................................................
|
||||
....B.........................................B.......................................................................B.............................................................................................
|
||||
....B.................BBBB....................B.......................................................................B........E......A.....E......................................................................
|
||||
....B.........................................B..........B............................................................B.....BBBBBBBBBBBBBBBBBBBBBB......XXXXXX................................................
|
||||
....B.........................................B..........B............................................................B..........................B....................A....................A.......................
|
||||
....BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..........B............................................................B..........................B.................XXXXXX..............XXXXXXX...............
|
||||
.........................................................B............................................................BBBBBBBBBBBBBBBB...........B..............................................................
|
||||
.........................................................B...........................................................................B...........B............................XXXXX..............................................
|
||||
.........................................................BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...........B................................................................F
|
||||
.................................................Q...............................................................................................B......................................A...................XXXXXXXXX....
|
||||
...................AAAAAAAAAAAAAAAAAA......................................E.......................A....E....................E...................B...................................XXXXXXX...........................
|
||||
.................BBBBBBBBBBBBBBBBBBBBBBB.....BBBBBBBBBBBBBBBBBBBBB....BBBBBBBBBBBBBBBBBB........BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB......................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
@@ -1,39 +0,0 @@
|
||||
....................................................................................................
|
||||
......................................................
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
...............................................................................................A...........................
|
||||
............................................................................................BBBBBB...........
|
||||
..........................................................................BBBBBB
|
||||
........................................................................
|
||||
............................................................A............
|
||||
..........................................................BBBBB
|
||||
.......................................................
|
||||
..................................................................................................................A
|
||||
............................................BBBBB.......................................................
|
||||
.................................BBBBB..............................................................
|
||||
...........................A.........................................................................
|
||||
.........................BBBBB........................................................................
|
||||
....................................................................................................
|
||||
.................BBBBB...................................................................................
|
||||
....................................................................................................
|
||||
.............................................................................................
|
||||
.............................................................................................
|
||||
.................................................................
|
||||
.
|
||||
.
|
||||
............E.....................................
|
||||
........BBBBB.............A....................................................................
|
||||
.........................QBQQQQQBBBB
|
||||
....................................................................................................
|
||||
.P............................E...................E............................................................F..
|
||||
BBBBBBBBBBBBBBBBBB...BBBBBBBBBBBBBBBBB...BBBBBBBBBBBBBBBBBBB...BBBBBBB......................................BBBBBBBBBBBBBBBBBBBB
|
||||
@@ -1,29 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................
|
||||
..................................................................................................
|
||||
....................Q.............................................................................
|
||||
..................................................................................................
|
||||
....................A.............................................................................
|
||||
...................BBBBB.........................................................................
|
||||
.
|
||||
.
|
||||
.................................BBBBB
|
||||
..........................................................
|
||||
.................................................A................................................
|
||||
..............................................BBBBBBBB..
|
||||
.
|
||||
..................................................
|
||||
..........................................................BBBBB..
|
||||
......................................
|
||||
.....................................................................BBBBBBBBBB....A............................A
|
||||
..................................................................................BBBBB.....BBBBB............BBBBBBB.........E....................
|
||||
.......................................................................................................................BBBBBBBB........................................
|
||||
.....................................................................................................A...................................................................
|
||||
....................................................................................................BBBBBB...........................A...........................
|
||||
.................................................................................................................................BBBBBBBB........
|
||||
...............................................................A
|
||||
..P...................A..................BBBQBBBBB............BBBBBBBB.................BBBQBBBB....................................................................
|
||||
..............................................................................................
|
||||
.................................................................
|
||||
...............E.............A..............................................A........................................................................F......
|
||||
BBBBBBBB......BBBBBBBBBB....BBBBB...BBBBBBBBBBBBB........................BBBBBBBBBB.......BB................................................BBBBBBBBBB
|
||||
@@ -1,24 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..P................................................................................................................
|
||||
..................................................................................................................
|
||||
BBBBBBBBBBB..................................................................................................................
|
||||
..................................................................................................................
|
||||
....
|
||||
....B.........................................B
|
||||
....B.........................................B......................
|
||||
....B................BB.......................B....................................................................
|
||||
....B.........................................B....................................................................
|
||||
....B.........................................B.....................................................................
|
||||
....BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..............................................................................................................
|
||||
................................................
|
||||
.
|
||||
..................................................................
|
||||
..................................................................................................................
|
||||
...................AAAAAAAAAAAAAAAAAA..........................F............................................
|
||||
.................BBBBBBBBBBBBBBBBBBBBBBB.....BBBBBBBBBBBBBBBBBBBBB............................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
@@ -1,20 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
...............................................B...................................................................
|
||||
...............................................B...................................................................
|
||||
...............................................B...................................................................
|
||||
...............................................B...................................................................
|
||||
...............................................B..................................................................
|
||||
...............................................B..................................................................
|
||||
...............................................B...............E....E.....A...E...................A.................
|
||||
...............................................BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB................................
|
||||
...............................................B..................................................................
|
||||
...............................................B.................................................................
|
||||
............................Q............................BBBBBBBBBBBBBB.......BBBBBBBBBBBB.......BBBBBBBBBBBB...............A..........................
|
||||
..P.........................................E............B.............................................................BBBBBBBBBBB........
|
||||
............................A.........BBBBBBBBBBBB...BBBBB........................................................................................................EEEEEEEEE.............F
|
||||
......A....................BBBBBB.................................................................................................................A...........BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
BBBBBBBBBB.....BBBBBB...............................................................................................................BBBBBBBBBB...BBB...BBB....
|
||||
@@ -1,25 +0,0 @@
|
||||
.....................................................................................................B
|
||||
.....................................................................................................B
|
||||
.....................................................................................................B
|
||||
.....................................................................................................B
|
||||
.....................................................................................................B
|
||||
.....................................................................................................B...............BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
.....................................................................................................B...............B...................................B
|
||||
.....................................................................................................B...............B...................................B
|
||||
.....................................................................................................B..............BB........BB.........................B
|
||||
.....................................................................................................B.............B.........BB..........................B
|
||||
.....................................................................................................B............B..........BB..........................B
|
||||
.....................................................................................................B............B.........BB...........................B
|
||||
.............................................................A.......................................B...........B........BB.............................B
|
||||
........................................BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB....BBBBBBBBBBBB.......BB...............................B
|
||||
........................................B...............................................................................B................................B
|
||||
..........................Q.............B.................E............................................................B.................................B
|
||||
........................................B........BBBBBBBBBBBBBBBB........................................A............B..................................B
|
||||
............................A...........B........B..............B...........................BBBBBBBBBBBBBBBBBBBBBBBBBB...................................B
|
||||
........................BBBBBBBBBBBBBBBBB........B..............B...........................B........................B........................A..........B
|
||||
........................B.......................QB..............B...........................B........................B...................................B
|
||||
........................B........................B..............B...........................B........................B.......Q...........................B
|
||||
BBBBBBBBBBBBBBBBBBBBBBBBB....BBBBBBBBBBBBBBBBBBBBB..............B.............Q.............B........................B....................................BBBBBBBB
|
||||
.............................B..................................B...........................B........................B....................A......................B
|
||||
.P................E..........B..................................B...........E.........E.....B........................B....A.................................F....B
|
||||
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..................................BBBBBBBBBBBBBBBBBBBBBBBBBBBBB........................BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
@@ -1,20 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..............................................................A...........................................................E...A
|
||||
...........................................................BBBBBBBBB.......................A...........................EBBBBBBBBB...
|
||||
........................................................................................BBBBBBB.....................E.BBBBBBBBBBB...
|
||||
...................................................................................................................EBBBBBBBBBBBBB
|
||||
............................................................................E...................................E.BBBBBBBBBBBBBBB
|
||||
..P........................Q..................EEE.........................BBBBBBB.........................A.....BBBBBBBBBBBBBBBBB.........F
|
||||
..........................................BBBBBBBBBBBB..................................................BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB......................
|
||||
........E........A......BBBBBBBBB..........................................................................................
|
||||
BBBBBBBBBB.....BBBBBB.............................................................................................................
|
||||
@@ -1,20 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..........................................................................................................A........
|
||||
....................................................................................................BBBBBBBBB..............
|
||||
.................................................................A.................................................
|
||||
............................................................BBBBBBBBB......................................................
|
||||
..................................................E...A....................................................BBBBBBBBBBB.........
|
||||
.............................................BBBBBBBBBBBB...................................BBBBBB..................................
|
||||
..P......................................EE..............................AE..........................................
|
||||
.............................E.....BBBBBBBBB...........................BBBBBBB.........................................BBBBBBB...........
|
||||
.......................A...BBBBBB....................................................BBBBBBB...............................................F
|
||||
BBBBBBBBBB.....BBBBBB..BBBBBBBBBB...................................................................................................BBBBBBBBBBBBBB........
|
||||
@@ -1,25 +0,0 @@
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
.A................................................................................................................
|
||||
XXXX................................................................A.............................................
|
||||
....................................A.............................XXXX......XXXX...........................................
|
||||
............XXXXX................XXXXX.........A.........XX........................................................
|
||||
....................XXXXXXX...................XXXXXX...............................XXXX......................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
...........................................................................................F......................
|
||||
......................................................XXXXXXXXXXX........................XXXXXX....................................
|
||||
..................................................................................................................
|
||||
..................................................................................................................
|
||||
..P.......A........XXXXX...................Q...........................................................................
|
||||
.........XXXXX...............A......................................................................................X.
|
||||
............................XXXXXX.......XXXXXXX....................................................................X.........
|
||||
BBBBB...............................................................................................................X
|
||||
.....................................................................................................................
|
||||
...........
|
||||
.
|
||||
.........................................................................................................
|
||||
...XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
||||
@@ -1,590 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import javax.sound.sampled.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Platformer extends JPanel implements KeyListener, ActionListener {
|
||||
|
||||
// constants
|
||||
static final int GRAVITY = 1;
|
||||
static final Color playerColor = Color.RED;
|
||||
static final Color tileColor = Color.BLUE;
|
||||
static final Color SKY = new Color(135, 206, 235);
|
||||
static final Color NIGHT_SKY = new Color(4, 26, 64);
|
||||
static final int FRICTION = 1;
|
||||
static final int MAXYVELO = 15;
|
||||
static final int MAXXVELO = 5;
|
||||
static final int totalLevels = 10;
|
||||
static int[] numAm = {4, 5, 10, 18, 6, 6, 5, 5, 6, 23};
|
||||
|
||||
// game objects
|
||||
Player player;
|
||||
ArrayList<Collidable> collidables;
|
||||
ArrayList<Collectable> collectables;
|
||||
Flag flag;
|
||||
ArrayList<Enemy> enemies;
|
||||
ArrayList<Projectile> projectiles;
|
||||
|
||||
// game vars
|
||||
int boardWidth;
|
||||
int boardHeight;
|
||||
int tileSize;
|
||||
int enemiesKilled;
|
||||
Timer gameTimer;
|
||||
HashMap<Integer, Boolean> pressedKeys;
|
||||
boolean jumpPressed;
|
||||
int cameraX, cameraY;
|
||||
int currentLevel;
|
||||
boolean allCollected;
|
||||
boolean gameOver;
|
||||
boolean gameStarted;
|
||||
Image heart, emptyHeart, slash, amendmentImg, powerImg, pressRImg, endImg, winImg, titleImg;
|
||||
ArrayList<Image> numbers;
|
||||
Clip jumpSound, shootSound, hitSound, collectSound;
|
||||
|
||||
public Platformer(int boardWidth, int boardHeight, int tileSize) {
|
||||
// setup game
|
||||
this.boardWidth = boardWidth;
|
||||
this.boardHeight = boardHeight;
|
||||
this.tileSize = tileSize;
|
||||
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
|
||||
addKeyListener(this);
|
||||
this.setFocusable(true);
|
||||
this.setLayout(null);
|
||||
|
||||
pressedKeys = new HashMap<>();
|
||||
jumpPressed = false;
|
||||
gameOver = false;
|
||||
gameStarted = false;
|
||||
setBackground(SKY);
|
||||
|
||||
// setup objects
|
||||
heart = new ImageIcon("Sprites/Hearts/heart.png").getImage();
|
||||
titleImg = new ImageIcon("Sprites/Title.png").getImage();
|
||||
emptyHeart = new ImageIcon("Sprites/Hearts/emptyHeart.png").getImage();
|
||||
endImg = new ImageIcon("Sprites/end.png").getImage();
|
||||
pressRImg = new ImageIcon("Sprites/PressR.png").getImage();
|
||||
winImg = new ImageIcon("Sprites/win.png").getImage();
|
||||
jumpSound = loadClip("Sounds/jump.wav");
|
||||
shootSound = loadClip("Sounds/shoot.wav");
|
||||
hitSound = loadClip("Sounds/hit.wav");
|
||||
collectSound = loadClip("Sounds/collect.wav");
|
||||
gameTimer = new Timer(15, this);
|
||||
player = new Player(-20, 0, tileSize, tileSize);
|
||||
collidables = new ArrayList<>();
|
||||
collectables = new ArrayList<>();
|
||||
enemies = new ArrayList<>();
|
||||
projectiles = new ArrayList<>();
|
||||
flag = new Flag(-20, 0, tileSize, tileSize);
|
||||
cameraX = 0;
|
||||
cameraY = 0;
|
||||
currentLevel = 0;
|
||||
enemiesKilled = 0;
|
||||
numbers = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
numbers.add((new ImageIcon("Sprites/Numbers/" + i + ".png")).getImage());
|
||||
slash = new ImageIcon("Sprites/Numbers/Slash.png").getImage();
|
||||
amendmentImg = new ImageIcon("Sprites/Amendment.png").getImage();
|
||||
powerImg = new ImageIcon("Sprites/Powerup1.png").getImage();
|
||||
|
||||
gameTimer.start();
|
||||
|
||||
// if i wanna add a button
|
||||
/*
|
||||
* JButton gameStart = new JButton("Start Game");
|
||||
* gameStart.addActionListener(e -> {
|
||||
* loadLevel(currentLevel);
|
||||
* gameTimer.start();
|
||||
* this.remove(gameStart);
|
||||
* this.revalidate();
|
||||
* this.repaint();
|
||||
* });
|
||||
* gameStart.setBorderPainted(false);
|
||||
* gameStart.setFocusPainted(false);
|
||||
* gameStart.setBounds(193,200,114,15);
|
||||
* gameStart.setForeground(new Color(52, 152, 219));
|
||||
* this.add(gameStart);
|
||||
*/
|
||||
}
|
||||
|
||||
// gameloop
|
||||
public void gameLoop() {
|
||||
if (currentLevel > totalLevels) return;
|
||||
|
||||
// lvl 3 arena
|
||||
if (currentLevel == 4 || currentLevel == 10) {
|
||||
boolean empty = currentLevel == 4 ? enemies.isEmpty() : enemies.size() <= 5;
|
||||
if (enemiesKilled < 100 * (currentLevel == 4 ? 1 : 4) && empty) {
|
||||
Random rand = new Random();
|
||||
int xOff = rand.nextInt(700);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
xOff = rand.nextInt(700);
|
||||
if (xOff + 80 <= player.x && player.x <= xOff + 140) xOff += 60;
|
||||
enemies.add(new Enemy(100 + xOff, 280, 20, 20, currentLevel));
|
||||
}
|
||||
|
||||
} else if (enemiesKilled >= 100 * (currentLevel == 4 ? 1 : 4)) {
|
||||
collidables.removeIf(c -> c.y == 300 && c.x >= 440 && c.x <= 520);
|
||||
}
|
||||
}
|
||||
|
||||
// camera
|
||||
cameraX = player.x - boardWidth / 2;
|
||||
cameraX = Math.max(0, cameraX);
|
||||
cameraY = player.y - boardHeight / 2;
|
||||
// cameraY = Math.max(0, cameraY);
|
||||
|
||||
// win
|
||||
allCollected = player.numAmendments >= numAm[currentLevel - 1];
|
||||
if (player.collidesWith(flag) && allCollected) {
|
||||
currentLevel++;
|
||||
player.health = 3;
|
||||
if (currentLevel > totalLevels) {
|
||||
gameTimer.stop();
|
||||
System.out.println("You win!");
|
||||
return;
|
||||
} else {
|
||||
loadLevel(currentLevel);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// keys
|
||||
if (Math.abs(player.xVelo) < MAXXVELO) {
|
||||
if (isKeyPressed(KeyEvent.VK_D) || isKeyPressed(KeyEvent.VK_RIGHT)) {
|
||||
player.xVelo += 1;
|
||||
} else if (isKeyPressed(KeyEvent.VK_A) || isKeyPressed(KeyEvent.VK_LEFT)) {
|
||||
player.xVelo -= 1;
|
||||
}
|
||||
}
|
||||
if (!isKeyPressed(KeyEvent.VK_D)
|
||||
&& !isKeyPressed(KeyEvent.VK_RIGHT)
|
||||
&& !isKeyPressed(KeyEvent.VK_A)
|
||||
&& !isKeyPressed(KeyEvent.VK_LEFT)) { // friction
|
||||
if (player.xVelo > 0) {
|
||||
player.xVelo = Math.max(0, player.xVelo - FRICTION);
|
||||
} else if (player.xVelo < 0) {
|
||||
player.xVelo = Math.min(0, player.xVelo + FRICTION);
|
||||
}
|
||||
}
|
||||
|
||||
// jump
|
||||
boolean jumpKeyDown = (isKeyPressed(KeyEvent.VK_W) || isKeyPressed(KeyEvent.VK_UP));
|
||||
if (jumpKeyDown && !jumpPressed) {
|
||||
if (player.onGround) {
|
||||
playClip(jumpSound);
|
||||
player.yVelo = -15;
|
||||
player.onGround = false;
|
||||
jumpPressed = true;
|
||||
player.airJumps = 0;
|
||||
} else if (player.curPower == 1 && player.airJumps < 1) {
|
||||
playClip(jumpSound);
|
||||
player.yVelo = -15;
|
||||
player.airJumps++;
|
||||
jumpPressed = true;
|
||||
}
|
||||
}
|
||||
if (!jumpKeyDown) {
|
||||
jumpPressed = false;
|
||||
}
|
||||
|
||||
// gravity
|
||||
if (player.yVelo < MAXYVELO) {
|
||||
player.yVelo += GRAVITY;
|
||||
}
|
||||
|
||||
// fall out of world
|
||||
if (player.y > 1400) { // 1400/20 = 70 rows to work with per level
|
||||
loadLevel(currentLevel);
|
||||
player.health--;
|
||||
}
|
||||
|
||||
for (Collectable c : collectables) {
|
||||
if (c instanceof Powerup) {
|
||||
Powerup pu = (Powerup) c;
|
||||
if (pu.yVelo < MAXYVELO && !pu.onGround) {
|
||||
pu.yVelo += GRAVITY;
|
||||
}
|
||||
pu.moveY(pu.yVelo);
|
||||
for (Collidable col : collidables) {
|
||||
if (pu.collidesWith(col)) {
|
||||
pu.yVelo = 0;
|
||||
pu.onGround = true;
|
||||
pu.y = ((Tile) col).y - pu.height;
|
||||
pu.rect.y = pu.y;
|
||||
}
|
||||
}
|
||||
if (player.collidesWith(pu)) {
|
||||
player.curPower = pu.id;
|
||||
player.powerTimer = Player.POWER_DURATION;
|
||||
}
|
||||
} else if (c instanceof Amendment) {
|
||||
Amendment am = (Amendment) c;
|
||||
if (player.collidesWith(am)) {
|
||||
player.numAmendments++;
|
||||
playClip(collectSound);
|
||||
}
|
||||
}
|
||||
}
|
||||
collectables.removeIf(c -> player.collidesWith(c));
|
||||
|
||||
// update x
|
||||
player.moveX(player.xVelo);
|
||||
|
||||
// collision with all tiles x
|
||||
for (Collidable c : collidables) {
|
||||
if (player.collidesWith(c)) {
|
||||
player.onCollideX(c);
|
||||
}
|
||||
}
|
||||
|
||||
// update y
|
||||
player.moveY(player.yVelo);
|
||||
// assume not on ground
|
||||
player.onGround = false;
|
||||
|
||||
// collision with all tiles y
|
||||
for (Collidable c : collidables) {
|
||||
if (player.collidesWith(c)) {
|
||||
player.onCollideY(c, collectables);
|
||||
}
|
||||
}
|
||||
|
||||
// Powerup timer
|
||||
if (player.curPower > 0) {
|
||||
player.powerTimer--;
|
||||
if (player.powerTimer <= 0) {
|
||||
player.curPower = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// update facing
|
||||
if (player.xVelo > 0) player.facing = 1;
|
||||
else if (player.xVelo < 0) player.facing = -1;
|
||||
|
||||
// shoot cooldown
|
||||
if (player.shootCooldown > 0) player.shootCooldown--;
|
||||
|
||||
// invincibility timer
|
||||
if (player.invincibleTimer > 0) player.invincibleTimer--;
|
||||
|
||||
// shoot
|
||||
// projectiles
|
||||
if (isKeyPressed(KeyEvent.VK_SPACE) && player.shootCooldown == 0) {
|
||||
playClip(shootSound);
|
||||
int projX = player.facing == 1 ? player.x + player.width : player.x - 10;
|
||||
projectiles.add(new Projectile(projX, player.y, tileSize, 10, currentLevel, player.facing));
|
||||
player.shootCooldown = Player.SHOOT_COOLDOWN;
|
||||
}
|
||||
|
||||
projectiles.removeIf(p -> p.x < -50 + cameraX || p.x > boardWidth + cameraX + 200);
|
||||
for (Projectile p : new ArrayList<>(projectiles)) {
|
||||
p.move();
|
||||
// projectile hits tile
|
||||
for (Collidable c : collidables) {
|
||||
if (p.collidesWith(c)) {
|
||||
projectiles.remove(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update enemies
|
||||
for (Enemy e : enemies) {
|
||||
e.patrol(collidables);
|
||||
// enemy hits player
|
||||
if (player.collidesWith(e)) {
|
||||
player.takeDamage();
|
||||
playClip(hitSound);
|
||||
}
|
||||
}
|
||||
|
||||
// die
|
||||
if (player.health <= 0) {
|
||||
System.out.print("Game Over - You Died!");
|
||||
gameOver = true;
|
||||
gameTimer.stop();
|
||||
}
|
||||
|
||||
// projectile hits enemy
|
||||
for (Projectile p : new ArrayList<>(projectiles)) {
|
||||
for (Enemy e : new ArrayList<>(enemies)) {
|
||||
if (p.collidesWith(e)) {
|
||||
enemies.remove(e);
|
||||
enemiesKilled++;
|
||||
projectiles.remove(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
enemies.removeIf(e -> !e.alive);
|
||||
}
|
||||
|
||||
public void loadLevel(int level) {
|
||||
projectiles.clear();
|
||||
enemiesKilled = 0;
|
||||
try {
|
||||
LevelLoader.load(tileSize, collidables, collectables, enemies, flag, player, level);
|
||||
player.reset();
|
||||
player.setLevel(level);
|
||||
cameraX = 0;
|
||||
cameraY = 0;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Could not load level " + level);
|
||||
}
|
||||
}
|
||||
|
||||
// paintComponent
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
// draw function
|
||||
public void draw(Graphics g) {
|
||||
// gameover screen
|
||||
if (gameOver) {
|
||||
g.drawImage(endImg, boardWidth / 2 - 100, boardHeight / 2 - 150, null);
|
||||
g.drawImage(pressRImg, boardWidth / 2 - 80, boardHeight / 2, null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentLevel > totalLevels) {
|
||||
g.drawImage(winImg, boardWidth / 2 - 100, boardHeight / 2 - 50, null);
|
||||
g.drawImage(pressRImg, boardWidth / 2 - 80, boardHeight / 2 + 60, null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentLevel == 6) {
|
||||
this.setBackground(NIGHT_SKY);
|
||||
}
|
||||
if (currentLevel != 6) {
|
||||
this.setBackground(SKY);
|
||||
}
|
||||
|
||||
g.translate(-cameraX, -cameraY);
|
||||
|
||||
player.draw(g);
|
||||
for (Collidable c : collidables) c.draw(g);
|
||||
for (Collectable c : collectables) c.draw(g);
|
||||
for (Enemy e : enemies) e.draw(g);
|
||||
for (Projectile p : projectiles) p.draw(g);
|
||||
flag.draw(g);
|
||||
if (currentLevel == 2) {
|
||||
g.drawString("Take a leap of faith....", flag.x - 25, flag.y - 400);
|
||||
}
|
||||
if (currentLevel == 4 || currentLevel == 10) {
|
||||
g.drawString("Kill " + 100 * (currentLevel == 4 ? 1 : 4) + " of them...", 220, 200);
|
||||
}
|
||||
|
||||
if (currentLevel == 10) {
|
||||
g.drawString("Final Level.... Time for the gauntlet", 80, 80);
|
||||
}
|
||||
// flag counter
|
||||
int amOnes = player.numAmendments % 10;
|
||||
int amTens = player.numAmendments / 10;
|
||||
|
||||
if (currentLevel > 0 && player.numAmendments < numAm[currentLevel - 1]) {
|
||||
int lvlAmOnes = numAm[currentLevel - 1] % 10;
|
||||
int lvlAmTens = numAm[currentLevel - 1] / 10;
|
||||
if (amTens > 0) g.drawImage(numbers.get(amTens), flag.x - 20, flag.y - 30, null);
|
||||
g.drawImage(numbers.get(amOnes), flag.x + 5, flag.y - 30, null);
|
||||
g.drawImage(slash, flag.x + 28, flag.y - 32, null);
|
||||
if (lvlAmTens > 0) {
|
||||
g.drawImage(numbers.get(lvlAmTens), flag.x + 55, flag.y - 30, null);
|
||||
g.drawImage(numbers.get(lvlAmOnes), flag.x + 80, flag.y - 30, null);
|
||||
g.drawImage(amendmentImg, flag.x + 110, flag.y - 30, null);
|
||||
|
||||
} else {
|
||||
g.drawImage(numbers.get(lvlAmOnes), flag.x + 55, flag.y - 30, null);
|
||||
g.drawImage(amendmentImg, flag.x + 85, flag.y - 30, null);
|
||||
}
|
||||
}
|
||||
|
||||
g.translate(cameraX, cameraY);
|
||||
|
||||
if (currentLevel == 8) {
|
||||
BufferedImage darkness =
|
||||
new BufferedImage(boardWidth, boardHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2 = darkness.createGraphics();
|
||||
|
||||
// fill entire overlay with fully opaque black
|
||||
g2.setColor(new Color(0, 0, 0, 255));
|
||||
g2.fillRect(0, 0, boardWidth + 1000, boardHeight + 1000);
|
||||
|
||||
// player's position in screen coordinates
|
||||
int screenX = (player.x + player.width / 2) - cameraX;
|
||||
int screenY = (player.y + player.height / 2) - cameraY;
|
||||
|
||||
// cut circle centered on player
|
||||
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
|
||||
g2.fillOval(screenX - 80, screenY - 100, 160, 160);
|
||||
|
||||
g2.dispose();
|
||||
|
||||
// draw overlay in screen space (after translate reset)
|
||||
g.drawImage(darkness, 0, 0, null);
|
||||
}
|
||||
|
||||
int modAmt = 2000;
|
||||
int curTime = (int) System.currentTimeMillis() % modAmt;
|
||||
curTime = Math.abs(curTime);
|
||||
|
||||
// start screen
|
||||
int startTime = 0;
|
||||
if (curTime >= 0 && curTime <= modAmt / 4 - 1) startTime = 0;
|
||||
else if (curTime >= modAmt / 4 && curTime <= modAmt / 2 - 1) startTime = 1;
|
||||
else if (curTime >= modAmt / 2 && curTime <= modAmt * 3 / 4 - 1) startTime = 2;
|
||||
else if (curTime >= modAmt * 3 / 4 && curTime <= modAmt - 1) startTime = 3;
|
||||
|
||||
if (currentLevel == 0) {
|
||||
g.drawImage(titleImg, boardWidth / 2 - 150, boardHeight / 2 - 200, null);
|
||||
|
||||
g.drawString("An American Identity Project", boardWidth / 2 - 75, boardHeight / 2 - 40);
|
||||
String text = "Press P to Start!";
|
||||
int xBase = 340;
|
||||
int yBase = 400;
|
||||
int spacing = 8;
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
|
||||
// x moves linearly
|
||||
int x = xBase + (i * spacing);
|
||||
|
||||
// y uses a sine wave
|
||||
// Math.sin takes radians. We use (startTime + i) to give each letter a
|
||||
// different phase.
|
||||
double waveOffset = Math.sin(startTime + i * 0.5) * 10;
|
||||
int y = yBase + (int) waveOffset;
|
||||
|
||||
g.drawString(String.valueOf(c), x, y);
|
||||
}
|
||||
}
|
||||
|
||||
if (currentLevel > 0) {
|
||||
|
||||
// draw hearts:
|
||||
int heartTime = 0;
|
||||
if (curTime >= 0 && curTime <= modAmt / 2 - 1) heartTime = 1;
|
||||
else if (curTime >= modAmt / 2 && curTime <= modAmt - 1) heartTime = 0;
|
||||
for (int i = 0; i < player.health; i++) {
|
||||
g.drawImage(heart, (((i + 1) * 20) - 10) + heartTime, 10 + heartTime * 2, null);
|
||||
}
|
||||
for (int i = 0; i < 3 - player.health; i++) {
|
||||
g.drawImage(emptyHeart, (50 - (i * 20)) + heartTime, 10 + heartTime * 2, null);
|
||||
}
|
||||
|
||||
// draw amendments counter in top right
|
||||
|
||||
if (amTens > 0) g.drawImage(numbers.get(amTens), 315, 10, null);
|
||||
g.drawImage(numbers.get(amOnes), 340, 10, null);
|
||||
int lvlAmOnes = numAm[currentLevel - 1] % 10;
|
||||
int lvlAmTens = numAm[currentLevel - 1] / 10;
|
||||
g.drawImage(slash, 363, 12, null);
|
||||
if (lvlAmTens > 0) {
|
||||
g.drawImage(numbers.get(lvlAmTens), 390, 10, null);
|
||||
g.drawImage(numbers.get(lvlAmOnes), 415, 10, null);
|
||||
|
||||
g.drawImage(amendmentImg, 445, 10, null);
|
||||
} else {
|
||||
g.drawImage(numbers.get(lvlAmOnes), 390, 10, null);
|
||||
g.drawImage(amendmentImg, 420, 10, null);
|
||||
}
|
||||
}
|
||||
|
||||
// draw powerup timer
|
||||
if (player.curPower == 1) {
|
||||
int secs = player.powerTimer / 66;
|
||||
int tens = secs / 10;
|
||||
int ones = secs % 10;
|
||||
if (tens > 0) {
|
||||
g.drawImage(numbers.get(tens), 184, 10, null);
|
||||
}
|
||||
g.drawImage(numbers.get(ones), 205, 10, null);
|
||||
g.drawImage(powerImg, 220, 10, null);
|
||||
}
|
||||
}
|
||||
|
||||
// is key pressed
|
||||
public boolean isKeyPressed(int key) {
|
||||
return pressedKeys.getOrDefault(key, false);
|
||||
}
|
||||
|
||||
// every tick
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (gameStarted) {
|
||||
gameLoop();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
|
||||
// check for key presses
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_P) {
|
||||
if (currentLevel == 0) {
|
||||
currentLevel = 1;
|
||||
loadLevel(currentLevel);
|
||||
gameStarted = true;
|
||||
}
|
||||
gameTimer.start();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_R) {
|
||||
if (gameOver) {
|
||||
gameTimer.stop();
|
||||
loadLevel(1);
|
||||
currentLevel = 1;
|
||||
jumpPressed = false;
|
||||
player.health = 3;
|
||||
gameOver = false;
|
||||
repaint();
|
||||
gameTimer.start();
|
||||
gameStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_O) {
|
||||
currentLevel++;
|
||||
loadLevel(currentLevel);
|
||||
}
|
||||
|
||||
pressedKeys.put(e.getKeyCode(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
pressedKeys.put(e.getKeyCode(), false);
|
||||
}
|
||||
|
||||
// dont need
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {}
|
||||
|
||||
public Clip loadClip(String path) {
|
||||
try {
|
||||
AudioInputStream audio = AudioSystem.getAudioInputStream(new File(path));
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(audio);
|
||||
return clip;
|
||||
} catch (Exception e) {
|
||||
System.out.println("Could not load sound: " + path);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void playClip(Clip clip) {
|
||||
if (clip != null) {
|
||||
clip.setFramePosition(0); // rewind to start
|
||||
clip.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Player extends Collidable {
|
||||
|
||||
static final int JUMP_HEIGHT = 10;
|
||||
static final int POWER_DURATION = 2000; // 2000 ticks ≈ 30 seconds
|
||||
static final int I_FRAMES = 67; // ~1 second
|
||||
static final int SHOOT_COOLDOWN = 33; // ~0.5 second
|
||||
|
||||
int health;
|
||||
int yVelo;
|
||||
int xVelo;
|
||||
boolean onGround;
|
||||
int curPower;
|
||||
int powerTimer;
|
||||
int airJumps;
|
||||
int facing; // 1 is right -1 is left
|
||||
int invincibleTimer;
|
||||
int shootCooldown;
|
||||
int numAmendments;
|
||||
|
||||
public Player(int x, int y, int w, int h) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Player/1.png"));
|
||||
xVelo = 0;
|
||||
yVelo = 0;
|
||||
onGround = false;
|
||||
curPower = 0;
|
||||
powerTimer = 0;
|
||||
airJumps = 0;
|
||||
invincibleTimer = 0;
|
||||
shootCooldown = 0;
|
||||
health = 3;
|
||||
facing = 1;
|
||||
numAmendments = 0;
|
||||
}
|
||||
|
||||
public void moveX(int moveX) {
|
||||
this.x += moveX;
|
||||
this.rect.x = this.x;
|
||||
}
|
||||
|
||||
public void moveY(int moveY) {
|
||||
this.y += moveY;
|
||||
this.rect.y = this.y;
|
||||
}
|
||||
|
||||
public void setLevel(int level) {
|
||||
if (level >= 2) {
|
||||
this.icon = new ImageIcon("Sprites/Player/2.png");
|
||||
return;
|
||||
}
|
||||
this.icon = new ImageIcon("Sprites/Player/" + level + ".png");
|
||||
}
|
||||
|
||||
public void onCollideX(Collidable other) {
|
||||
if (other instanceof Tile) {
|
||||
Tile t = (Tile) other;
|
||||
int playerCenterX = this.x + this.width / 2;
|
||||
int tileCenterX = t.x + t.width / 2;
|
||||
if (playerCenterX > tileCenterX) { // player on right side of tiile
|
||||
this.x = t.x + t.width;
|
||||
} else { // player on left side of tile
|
||||
this.x = t.x - this.width;
|
||||
}
|
||||
this.xVelo = 0;
|
||||
this.rect.x = this.x;
|
||||
}
|
||||
}
|
||||
|
||||
public void onCollideY(Collidable other, ArrayList<Collectable> collectables) {
|
||||
if (other instanceof Tile) {
|
||||
Tile t = (Tile) other;
|
||||
if (this.yVelo >= 0) { // falling down, land on top of tile
|
||||
this.y = t.y - this.height;
|
||||
onGround = true;
|
||||
airJumps = 0;
|
||||
} else { // moving up, hit underside of tile
|
||||
this.y = t.y + t.height;
|
||||
if (other instanceof PowerBrick) {
|
||||
PowerBrick pb = (PowerBrick) other;
|
||||
if (!pb.hit) {
|
||||
pb.spawnPower(collectables);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.yVelo = 0;
|
||||
this.rect.y = this.y;
|
||||
}
|
||||
}
|
||||
|
||||
public void takeDamage() {
|
||||
if (invincibleTimer <= 0) {
|
||||
health--;
|
||||
invincibleTimer = I_FRAMES;
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.rect.x = this.x;
|
||||
this.rect.y = this.y;
|
||||
this.xVelo = this.yVelo = 0;
|
||||
this.curPower = 0;
|
||||
this.powerTimer = 0;
|
||||
this.airJumps = 0;
|
||||
this.onGround = false;
|
||||
this.invincibleTimer = 0;
|
||||
this.shootCooldown = 0;
|
||||
this.facing = 1;
|
||||
this.numAmendments = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class PowerBrick extends Tile {
|
||||
int id;
|
||||
boolean hit;
|
||||
|
||||
public PowerBrick(int x, int y, int w, int h, int id) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Bricks/PowerBrick.png"));
|
||||
this.id = id;
|
||||
hit = false;
|
||||
}
|
||||
|
||||
public void spawnPower(ArrayList<Collectable> powerups) {
|
||||
this.icon = new ImageIcon("Sprites/Bricks/EmptyBrick.png");
|
||||
hit = true;
|
||||
powerups.add(new Powerup(this.x, this.y - this.height, this.width, this.height, 1));
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
hit = false;
|
||||
this.icon = new ImageIcon("Sprites/Bricks/PowerBrick.png");
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Powerup extends Collectable {
|
||||
int yVelo, xVelo, id;
|
||||
boolean onGround;
|
||||
|
||||
public Powerup(int x, int y, int w, int h, int id) {
|
||||
super(x, y, w, h, new ImageIcon("Sprites/Powerup" + id + ".png"));
|
||||
xVelo = 0;
|
||||
yVelo = -7;
|
||||
onGround = false;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void moveX(int moveX) {
|
||||
this.x += moveX;
|
||||
this.rect.x = this.x;
|
||||
}
|
||||
|
||||
public void moveY(int moveY) {
|
||||
this.y += moveY;
|
||||
this.rect.y = this.y;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Projectile extends Collidable {
|
||||
int xVelo;
|
||||
|
||||
public Projectile(int x, int y, int w, int h, int curLevel, int direction) {
|
||||
super(
|
||||
x, y, w, h, new ImageIcon("Sprites/Projectiles/" + ((curLevel == 1) ? "1" : "2") + ".png"));
|
||||
xVelo = 10 * direction;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
this.x += xVelo;
|
||||
this.rect.x = this.x;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import java.awt.Image;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Sprite {
|
||||
ImageIcon icon;
|
||||
Image sprite;
|
||||
|
||||
public Sprite(ImageIcon i) {
|
||||
icon = i;
|
||||
sprite = i.getImage();
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 281 B |
|
Before Width: | Height: | Size: 140 B |
|
Before Width: | Height: | Size: 180 B |
|
Before Width: | Height: | Size: 260 B |
|
Before Width: | Height: | Size: 337 B |
|
Before Width: | Height: | Size: 364 B |
|
Before Width: | Height: | Size: 315 B |
|
Before Width: | Height: | Size: 387 B |
|
Before Width: | Height: | Size: 290 B |
|
Before Width: | Height: | Size: 317 B |
|
Before Width: | Height: | Size: 354 B |
|
Before Width: | Height: | Size: 418 B |
|
Before Width: | Height: | Size: 495 B |
|
Before Width: | Height: | Size: 426 B |
|
Before Width: | Height: | Size: 180 B |
|
Before Width: | Height: | Size: 261 B |
|
Before Width: | Height: | Size: 264 B |
|
Before Width: | Height: | Size: 312 B |
|
Before Width: | Height: | Size: 249 B |
|
Before Width: | Height: | Size: 301 B |
|
Before Width: | Height: | Size: 311 B |
|
Before Width: | Height: | Size: 274 B |
|
Before Width: | Height: | Size: 286 B |
|
Before Width: | Height: | Size: 392 B |
|
Before Width: | Height: | Size: 223 B |
|
Before Width: | Height: | Size: 307 B |
|
Before Width: | Height: | Size: 313 B |
|
Before Width: | Height: | Size: 207 B |
|
Before Width: | Height: | Size: 275 B |
|
Before Width: | Height: | Size: 400 B |
|
Before Width: | Height: | Size: 275 B |
|
Before Width: | Height: | Size: 235 B |
|
Before Width: | Height: | Size: 444 B |
|
Before Width: | Height: | Size: 188 B |
|
Before Width: | Height: | Size: 282 B |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 694 B |
|
Before Width: | Height: | Size: 1.2 KiB |
@@ -1,8 +0,0 @@
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Tile extends Collidable {
|
||||
|
||||
public Tile(int x, int y, int w, int h, ImageIcon i) {
|
||||
super(x, y, w, h, i);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
Main-Class: Display
|
||||
|
||||
29
Bishop.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Bishop extends Piece {
|
||||
|
||||
public Bishop(int x, int y, String color) {
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.png").getImage());
|
||||
}
|
||||
|
||||
public Piece copy() {
|
||||
Piece newP = new Bishop(this.pos.x, this.pos.y, this.color);
|
||||
return newP;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getPseudoLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
positions.addAll(slide(board, -1, -1));
|
||||
positions.addAll(slide(board, 1, -1));
|
||||
positions.addAll(slide(board, -1, 1));
|
||||
positions.addAll(slide(board, 1, 1));
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
65
Board.java
Normal file
@@ -0,0 +1,65 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Board {
|
||||
Piece[][] board;
|
||||
|
||||
public Board() {
|
||||
board = new Piece[8][8];
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
board[i][1] = new Pawn(i, 1, "Black");
|
||||
}
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
board[i][6] = new Pawn(i, 6, "White");
|
||||
}
|
||||
}
|
||||
|
||||
public Board(boolean isCopy) {
|
||||
board = new Piece[8][8];
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
for (Piece[] row : board) {
|
||||
for (Piece p : row) {
|
||||
if (p != null) p.draw(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean inBounds(Position pos) {
|
||||
return (pos.x >= 0 && pos.x <= 7) && (pos.y >= 0 && pos.y <= 7);
|
||||
}
|
||||
|
||||
public boolean isOpen(Position pos) {
|
||||
return this.getPiece(pos) == null;
|
||||
}
|
||||
|
||||
public Piece getPiece(Position pos) {
|
||||
return this.board[pos.x][pos.y];
|
||||
}
|
||||
|
||||
public Piece getPiece(int x, int y) {
|
||||
return this.board[x][y];
|
||||
}
|
||||
|
||||
public void setPiece(int x, int y, Piece p) {
|
||||
this.board[x][y] = p;
|
||||
}
|
||||
|
||||
public void setPiece(Position pos, Piece p) {
|
||||
this.board[pos.x][pos.y] = p;
|
||||
}
|
||||
|
||||
public static Board copy(Board b) {
|
||||
Board newBoard = new Board();
|
||||
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
for (int j = 0; j <= 7; j++) {
|
||||
Piece p = b.getPiece(i, j);
|
||||
if (p == null) continue;
|
||||
newBoard.board[i][j] = p.copy();
|
||||
}
|
||||
}
|
||||
|
||||
return newBoard;
|
||||
}
|
||||
}
|
||||
58
Chess.java
Normal file
@@ -0,0 +1,58 @@
|
||||
import java.awt.*;
|
||||
import java.awt.Color;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Chess extends JPanel implements ActionListener {
|
||||
|
||||
// pieces stuff
|
||||
Board board;
|
||||
|
||||
// game vars
|
||||
int boardWidth, boardHeight;
|
||||
boolean whiteTurn;
|
||||
Timer gameTimer;
|
||||
Color creme = new Color(254, 245, 218);
|
||||
Color brown = new Color(121, 92, 50);
|
||||
|
||||
public Chess(int boardWidth, int boardHeight) {
|
||||
this.boardWidth = boardWidth;
|
||||
this.boardHeight = boardHeight;
|
||||
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
|
||||
setBackground(Color.WHITE);
|
||||
setFocusable(true);
|
||||
|
||||
board = new Board();
|
||||
|
||||
gameTimer = new Timer(200, this);
|
||||
|
||||
gameTimer.start();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void gameLoop() {}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
||||
// draw board
|
||||
for (int i = 1; i <= 8; i++) {
|
||||
for (int j = 1; j <= 8; j++) {
|
||||
g.setColor((i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ? creme : brown);
|
||||
g.fillRect(i * 40, j * 40, 40, 40);
|
||||
}
|
||||
}
|
||||
|
||||
// draw pieces
|
||||
board.draw(g);
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
gameLoop();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import java.util.ArrayList;
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.border.*;
|
||||
import java.awt.Color;
|
||||
|
||||
|
||||
public class Chess extends JPanel implements ActionListener{
|
||||
|
||||
//pieces stuff
|
||||
ArrayList<Piece> white;
|
||||
ArrayList<Piece> black;
|
||||
|
||||
//game vars
|
||||
int boardWidth, boardHeight;
|
||||
boolean whiteTurn;
|
||||
Timer gameTimer;
|
||||
Color creme = new Color(254,245,218);
|
||||
Color brown = new Color(121,92,50);
|
||||
|
||||
public Chess(int boardWidth, int boardHeight){
|
||||
this.boardWidth = boardWidth;
|
||||
this.boardHeight = boardHeight;
|
||||
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
|
||||
setBackground(Color.WHITE);
|
||||
setFocusable(true);
|
||||
|
||||
white = new ArrayList<>();
|
||||
black = new ArrayList<>();
|
||||
|
||||
for (int i =0 ; i <= 7; i++){
|
||||
white.add(new Pawn(i+1,2,"White"));
|
||||
}
|
||||
for (int i =0 ; i <= 7; i++){
|
||||
black.add(new Pawn(i+1,7,"Black"));
|
||||
}
|
||||
|
||||
gameTimer = new Timer(200,this);
|
||||
|
||||
gameTimer.start();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void gameLoop(){
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics g){
|
||||
|
||||
//draw board
|
||||
for (int i = 1; i <= 8; i++){
|
||||
for (int j = 1; j<= 8; j++){
|
||||
g.setColor((i%2 == 1 && j%2 == 1 ) || (i%2==0 && j%2 == 0)? creme : brown);
|
||||
g.fillRect(i * 40, j * 40, 40, 40);
|
||||
}
|
||||
}
|
||||
|
||||
//draw pieces
|
||||
for (Piece p : white) p.draw(g);
|
||||
for (Piece p : black) p.draw(g);
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g){
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e){
|
||||
gameLoop();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class Display {
|
||||
public static void main(String[] args) throws Exception{
|
||||
//creating instance of JFrame
|
||||
int boardWidth = 400;
|
||||
int boardHeight = 400;
|
||||
|
||||
JFrame game = new JFrame();
|
||||
game.setSize(boardWidth, boardHeight);
|
||||
game.setVisible(true);
|
||||
game.setLocationRelativeTo(null);
|
||||
game.setResizable(false);
|
||||
|
||||
Chess chess = new Chess(boardWidth, boardHeight);
|
||||
game.add(chess);
|
||||
game.pack();
|
||||
chess.requestFocus();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//images : https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQN6qOyhiUDLlTlwl19PaMTeiY5rSOqkUqu-g&s
|
||||
@@ -1,10 +0,0 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Pawn extends Piece{
|
||||
|
||||
|
||||
public Pawn(int x, int y,String color){
|
||||
super(x,y,new ImageIcon("Sprites/" + color + "/Pawn.png").getImage());
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Piece{
|
||||
ArrayList<Integer> legalMoves;
|
||||
int x,y;
|
||||
Image sprite;
|
||||
|
||||
public Piece(int x, int y, Image sprite){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.sprite = sprite;
|
||||
}
|
||||
|
||||
public void draw(Graphics g){
|
||||
g.drawImage(sprite,x * 40,y * 40,null);
|
||||
}
|
||||
}
|
||||
24
Display.java
Normal file
@@ -0,0 +1,24 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class Display {
|
||||
public static void main(String[] args) throws Exception {
|
||||
// creating instance of JFrame
|
||||
int boardWidth = 400;
|
||||
int boardHeight = 400;
|
||||
|
||||
JFrame game = new JFrame();
|
||||
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
game.setSize(boardWidth, boardHeight);
|
||||
game.setVisible(true);
|
||||
game.setLocationRelativeTo(null);
|
||||
game.setResizable(false);
|
||||
|
||||
Chess chess = new Chess(boardWidth, boardHeight);
|
||||
game.add(chess);
|
||||
game.pack();
|
||||
chess.requestFocus();
|
||||
}
|
||||
}
|
||||
|
||||
// images :
|
||||
// https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQN6qOyhiUDLlTlwl19PaMTeiY5rSOqkUqu-g&s
|
||||
66
King.java
Normal file
@@ -0,0 +1,66 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class King extends Piece {
|
||||
boolean hasMoved;
|
||||
static int[] xDir = {-1, 0, 1, -1, 1, -1, 0, 1};
|
||||
static int[] yDir = {-1, -1, -1, 0, 0, 1, 1, 1};
|
||||
|
||||
public King(int x, int y, String color) {
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/king.png").getImage());
|
||||
hasMoved = false;
|
||||
}
|
||||
|
||||
public Piece copy() {
|
||||
Piece newP = new King(this.pos.x, this.pos.y, this.color);
|
||||
return newP;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getPseudoLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
for (int i = 0; i < xDir.length; i++) {
|
||||
Position test = new Position(pos.x + xDir[i], pos.y + yDir[i]);
|
||||
if (!Board.inBounds(test)) continue;
|
||||
if (board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
continue;
|
||||
}
|
||||
if (!board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
}
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = getPseudoLegalMoves(board);
|
||||
for (Position p : new ArrayList<>(positions)) {
|
||||
Piece tempPiece = board.getPiece(p);
|
||||
board.setPiece(pos, null);
|
||||
board.setPiece(p, this);
|
||||
if (inCheck(board, p)) {
|
||||
positions.remove(p);
|
||||
}
|
||||
board.setPiece(p, tempPiece);
|
||||
board.setPiece(pos, this);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
public boolean inCheck(Board board, Position pos) {
|
||||
for (Piece[] pieces : board.board) {
|
||||
for (Piece p : pieces) {
|
||||
if (p == null) continue;
|
||||
if (!p.colorMatches(this) && !(p instanceof King)) {
|
||||
ArrayList<Position> ar = p.getPseudoLegalMoves(board);
|
||||
for (Position posi : ar) {
|
||||
if (pos.equals(posi)) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
38
Knight.java
Normal file
@@ -0,0 +1,38 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Knight extends Piece {
|
||||
static int[] xDir = {-1, -2, -2, -1, 1, 2, 2, 1};
|
||||
static int[] yDir = {-2, -1, 1, 2, 2, 1, -1, -2};
|
||||
|
||||
public Knight(int x, int y, String color) {
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/knight.png").getImage());
|
||||
}
|
||||
|
||||
public Piece copy() {
|
||||
Piece newP = new Knight(this.pos.x, this.pos.y, this.color);
|
||||
return newP;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getPseudoLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
for (int i = 0; i < xDir.length; i++) {
|
||||
Position test = new Position(pos.x + xDir[i], pos.y + yDir[i]);
|
||||
if (!Board.inBounds(test)) continue;
|
||||
if (board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
continue;
|
||||
}
|
||||
if (!board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
}
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
50
Pawn.java
Normal file
@@ -0,0 +1,50 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Pawn extends Piece {
|
||||
boolean hasMoved;
|
||||
int colorDir;
|
||||
|
||||
public Pawn(int x, int y, String color) {
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/pawn.png").getImage());
|
||||
hasMoved = false;
|
||||
colorDir = color.equals("White") ? -1 : 1;
|
||||
}
|
||||
|
||||
public Piece copy() {
|
||||
Piece newP = new Pawn(this.pos.x, this.pos.y, this.color);
|
||||
return newP;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getPseudoLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
// diagonal moves (captures)
|
||||
Position test = new Position(pos.x + 1, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
|
||||
test = new Position(pos.x - 1, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
|
||||
// one square in front: if blocked return early
|
||||
test = new Position(pos.x, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
} else return positions;
|
||||
|
||||
// two squares in front
|
||||
test = new Position(pos.x, pos.y + 2 * colorDir);
|
||||
if (!hasMoved && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
47
Piece.java
Normal file
@@ -0,0 +1,47 @@
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class Piece {
|
||||
Position pos;
|
||||
Image sprite;
|
||||
String color;
|
||||
|
||||
public Piece(int x, int y, String color, Image sprite) {
|
||||
this.pos = new Position(x, y);
|
||||
this.color = color;
|
||||
this.sprite = sprite;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
g.drawImage(sprite, pos.x * 40, pos.y * 40, null);
|
||||
}
|
||||
|
||||
public abstract ArrayList<Position> getLegalMoves(Board board);
|
||||
|
||||
public abstract ArrayList<Position> getPseudoLegalMoves(Board board);
|
||||
|
||||
public abstract Piece copy();
|
||||
|
||||
public boolean colorMatches(Piece p) {
|
||||
return this.color.equals(p.color);
|
||||
}
|
||||
|
||||
public ArrayList<Position> slide(Board board, int dx, int dy) {
|
||||
ArrayList<Position> positions = new ArrayList<>();
|
||||
int step = 1;
|
||||
while (true) {
|
||||
Position test = new Position(pos.x + step * dx, pos.y + step * dy);
|
||||
if (!Board.inBounds(test)) break;
|
||||
if (board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
} else if (board.getPiece(test).colorMatches(this)) {
|
||||
break;
|
||||
} else {
|
||||
positions.add(test);
|
||||
break;
|
||||
}
|
||||
step++;
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
}
|
||||
8
Position.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class Position {
|
||||
int x, y;
|
||||
|
||||
public Position(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||