Add sprites
|
@ -0,0 +1,124 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 2.0 KiB |
|
@ -9,6 +9,10 @@ Implements Runnable interface to use "threading" - let the game do two things at
|
||||||
*/
|
*/
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
@ -20,18 +24,36 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
public Thread gameThread;
|
public Thread gameThread;
|
||||||
public Image image;
|
public Image image;
|
||||||
public Graphics graphics;
|
public Graphics graphics;
|
||||||
public PlayerBall ball;
|
public Player player;
|
||||||
|
public int frame;
|
||||||
|
// keeps track of how many ticks has elapsed since last frame change
|
||||||
|
public int frameCounter = 0;
|
||||||
|
|
||||||
|
public BufferedImage[][] spriteArray = new BufferedImage[4][11];
|
||||||
|
|
||||||
|
|
||||||
public GamePanel(){
|
public GamePanel(){
|
||||||
ball = new PlayerBall(GAME_WIDTH/2, GAME_HEIGHT/2); //create a player controlled ball, set start location to middle of screen
|
for (int i = 0; i < 11; i++) {
|
||||||
|
try {
|
||||||
|
BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));
|
||||||
|
|
||||||
|
spriteArray[1][i] = sprite;
|
||||||
|
spriteArray[3][i] = sprite;
|
||||||
|
spriteArray[0][i] = sprite;
|
||||||
|
spriteArray[2][i] = sprite;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screen
|
||||||
this.setFocusable(true); //make everything in this class appear on the screen
|
this.setFocusable(true); //make everything in this class appear on the screen
|
||||||
this.addKeyListener(this); //start listening for keyboard input
|
this.addKeyListener(this); //start listening for keyboard input
|
||||||
|
|
||||||
//add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods
|
//add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
ball.mousePressed(e);
|
player.mousePressed(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
|
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
|
||||||
|
@ -46,37 +68,37 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
//we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
|
//we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
|
||||||
image = createImage(GAME_WIDTH, GAME_HEIGHT); //draw off screen
|
image = createImage(GAME_WIDTH, GAME_HEIGHT); //draw off screen
|
||||||
graphics = image.getGraphics();
|
graphics = image.getGraphics();
|
||||||
draw(graphics);//update the positions of everything on the screen
|
draw(graphics, frame);//update the positions of everything on the screen
|
||||||
g.drawImage(image, 0, 0, this); //move the image on the screen
|
g.drawImage(image, 0, 0, this); //move the image on the screen
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//call the draw methods in each class to update positions as things move
|
//call the draw methods in each class to update positions as things move
|
||||||
public void draw(Graphics g){
|
public void draw(Graphics g, int frame){
|
||||||
ball.draw(g);
|
frameCounter += player.draw(g, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
//call the move methods in other classes to update positions
|
//call the move methods in other classes to update positions
|
||||||
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
|
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
|
||||||
public void move(){
|
public void move(){
|
||||||
ball.move();
|
player.move();
|
||||||
}
|
}
|
||||||
|
|
||||||
//handles all collision detection and responds accordingly
|
//handles all collision detection and responds accordingly
|
||||||
public void checkCollision(){
|
public void checkCollision(){
|
||||||
|
|
||||||
//force player to remain on screen
|
//force player to remain on screen
|
||||||
if(ball.y<= 0){
|
if(player.y<= 0){
|
||||||
ball.y = 0;
|
player.y = 0;
|
||||||
}
|
}
|
||||||
if(ball.y >= GAME_HEIGHT - PlayerBall.BALL_DIAMETER){
|
if(player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT){
|
||||||
ball.y = GAME_HEIGHT-PlayerBall.BALL_DIAMETER;
|
player.y = GAME_HEIGHT- Player.PLAYER_HEIGHT;
|
||||||
}
|
}
|
||||||
if(ball.x <= 0){
|
if(player.x <= 0){
|
||||||
ball.x = 0;
|
player.x = 0;
|
||||||
}
|
}
|
||||||
if(ball.x + PlayerBall.BALL_DIAMETER >= GAME_WIDTH){
|
if(player.x + Player.PLAYER_WIDTH >= GAME_WIDTH){
|
||||||
ball.x = GAME_WIDTH-PlayerBall.BALL_DIAMETER;
|
player.x = GAME_WIDTH- Player.PLAYER_WIDTH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,23 +121,34 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
move();
|
move();
|
||||||
checkCollision();
|
checkCollision();
|
||||||
repaint();
|
repaint();
|
||||||
|
if (frameCounter > 10) {
|
||||||
|
System.out.println(frameCounter);
|
||||||
|
// increment sprite image to be used and keeps it below 12
|
||||||
|
frame = (frame + 1) % 11;
|
||||||
|
frameCounter -= 10;
|
||||||
|
}
|
||||||
delta--;
|
delta--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if a key is pressed, we'll send it over to the PlayerBall class for processing
|
//if a key is pressed, we'll send it over to the Player class for processing
|
||||||
public void keyPressed(KeyEvent e){
|
public void keyPressed(KeyEvent e){
|
||||||
ball.keyPressed(e);
|
player.keyPressed(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
//if a key is released, we'll send it over to the PlayerBall class for processing
|
//if a key is released, we'll send it over to the Player class for processing
|
||||||
public void keyReleased(KeyEvent e){
|
public void keyReleased(KeyEvent e){
|
||||||
ball.keyReleased(e);
|
player.keyReleased(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
//left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface
|
//left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface
|
||||||
public void keyTyped(KeyEvent e){
|
public void keyTyped(KeyEvent e){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BufferedImage getImage(String imageLocation) throws IOException {
|
||||||
|
return ImageIO.read(new File(imageLocation));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ In 2D GUI, basically everything is a rectangle even if it doesn't look like it!
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
public class PlayerBall extends Rectangle{
|
public class GenericSprite extends Rectangle{
|
||||||
|
|
||||||
public int yVelocity;
|
public int yVelocity;
|
||||||
public int xVelocity;
|
public int xVelocity;
|
||||||
|
@ -15,8 +15,8 @@ public class PlayerBall extends Rectangle{
|
||||||
public static final int BALL_DIAMETER = 20; //size of ball
|
public static final int BALL_DIAMETER = 20; //size of ball
|
||||||
|
|
||||||
//constructor creates ball at given location with given dimensions
|
//constructor creates ball at given location with given dimensions
|
||||||
public PlayerBall(int x, int y){
|
public GenericSprite(int x, int y, int height, int width){
|
||||||
super(x, y, BALL_DIAMETER, BALL_DIAMETER);
|
super(x, y, height, width);
|
||||||
}
|
}
|
||||||
|
|
||||||
//called from GamePanel when any keyboard input is detected
|
//called from GamePanel when any keyboard input is detected
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* Eric Li, ICS4U, Completed 5/29/2022
|
||||||
|
|
||||||
|
Paddle class defines behaviours for the left and right player-controlled paddles */
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.ImageObserver;
|
||||||
|
|
||||||
|
public class Player extends GenericSprite {
|
||||||
|
public final int SPEED = 5;
|
||||||
|
public static final int PLAYER_WIDTH = 8;
|
||||||
|
public static final int PLAYER_HEIGHT = 80;
|
||||||
|
public int upKey, downKey, rightKey, leftKey;
|
||||||
|
// sA[0] is -x, -y
|
||||||
|
// sA[1] is x, -y
|
||||||
|
// sA[2] is -x, y
|
||||||
|
// sA[3] is x, y
|
||||||
|
public BufferedImage[][] spriteArray;
|
||||||
|
public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][] sprites) {
|
||||||
|
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
|
||||||
|
|
||||||
|
this.upKey = upKey;
|
||||||
|
this.downKey = downKey;
|
||||||
|
this.leftKey = leftKey;
|
||||||
|
this.rightKey = rightKey;
|
||||||
|
spriteArray = sprites;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// moves paddle when key is pressed
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
super.keyPressed(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// stops moving paddle when key is released
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
super.keyReleased(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// calls parent
|
||||||
|
public void move() {
|
||||||
|
super.move();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int draw(Graphics g, int frame) {
|
||||||
|
// g.setColor(Color.WHITE);
|
||||||
|
if (xVelocity == 0 && yVelocity == 0) {
|
||||||
|
g.drawImage(spriteArray[1][0], x, y, null);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
g.drawImage(spriteArray[(int)(Math.signum(xVelocity)+Math.signum(yVelocity)*2+3)/2][frame], x, y, null);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|