Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/examples/game_character/GameCharacterExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package examples.game_character;

import game_tools.Game;
import game_tools.GameCharacter;
import game_tools.GameControlScene;

import java.awt.*;
import java.awt.event.KeyEvent;

public class GameCharacterExample implements GameControlScene {

Game game = new Game();

GameCharacter character1 = new GameCharacter();

GameCharacter character2 = new GameCharacter(50, 50);

GameCharacter character3 = new GameCharacter(100, 100, Color.BLUE);

GameCharacter character4 = new GameCharacter(150, 150, 96, 214, 163);

GameCharacter character5 = new GameCharacter(200, 200, 75, 75, "/game_tools/game_character_images/cat_face/cat-face.png");

GameCharacter character6 = new GameCharacter(20, 400, GameCharacter.PremadeCharacter.ALIEN);

GameCharacter character7;

public GameCharacterExample() {
//initialize character 7
String[] animationImageLocations = {
"/examples/flappy/bird0.png",
"/examples/flappy/bird1.png",
"/examples/flappy/bird2.png",
"/examples/flappy/bird3.png",
"/examples/flappy/bird4.png",
"/examples/flappy/bird5.png",
};
character7 = new GameCharacter(275, 275, 100, 100, animationImageLocations);

//start game
game.setScene(this);
game.start();
}

public static void main(String[] args) throws InterruptedException {
GameCharacterExample example = new GameCharacterExample();
//move character6
for(int i=0; i<100; i++){
Thread.sleep(20);
example.character6.moveRight();
}
}

@Override
public void draw(Graphics g) {
//background
g.setColor(new Color(100, 150, 230));
g.fillRect(0, 0, game.screenWidth, game.screenHeight);

// draw character1
character1.draw(g);

//draw character2
character2.draw(g);

//draw character3
character3.draw(g);

//draw character4
character4.draw(g);

//draw character 5
character5.draw(g);

//draw character6
character6.draw(g);

//draw character6
character7.draw(g);
}

@Override
public void keyPressed(KeyEvent e) {

}

}
6 changes: 6 additions & 0 deletions src/game_tools/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public Animation() {
this.spritesList = new ArrayList<>(Arrays.asList(sprites));
}

Animation(String... imageFileLocations) {
this.spritesList = new ArrayList<>(Arrays.asList(
Arrays.stream(imageFileLocations).map(Sprite::new).toArray(Sprite[]::new)
));
}

public void add(Sprite sprite) {
this.spritesList.add(sprite);
}
Expand Down
176 changes: 176 additions & 0 deletions src/game_tools/GameCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package game_tools;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;

public class GameCharacter {

//position
public int x;
public int y;

//movement
public int verticalMovementSpeed = 2;
public int horizontalMovementSpeed = 2;
public int previousX = x;
public int previousY = y;

//size
public int width = 50;
public int height = 50;

//shape color
public Color color = Color.RED;

//image
public BufferedImage image;

//animation
public Animation idleAnimation;
public Animation moveRightAnimation;

//characters
public PremadeCharacter premadeCharacter;

public GameCharacter() {
}

public GameCharacter(int x, int y) {
this.x = x;
this.y = y;
}

public GameCharacter(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color=color;
}

public GameCharacter(int x, int y, int redValue, int greenValue, int blueValue) {
this(x, y, new Color(redValue, greenValue, blueValue));
}

public GameCharacter(int x, int y, Color color, int width, int height) {
this.x = x;
this.y = y;
this.color = color;
this.width = width;
this.height = height;
}

public GameCharacter(int x, int y, int redValue, int greenValue, int blueValue, int width, int height) {
this(x, y, new Color(redValue, greenValue, blueValue), width, height);
}

public GameCharacter(int x, int y, int width, int height, String... imageFileLocations) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.idleAnimation = new Animation(imageFileLocations);
}

public GameCharacter(String imageLocation){
try {
image = ImageIO.read(getClass().getResourceAsStream(imageLocation));
} catch (Exception e) {
System.err.println("Failed to load Image: " + imageLocation);
e.printStackTrace();
}
}

public GameCharacter(int x, int y, int width, int height, String imageLocation){
this.x = x;
this.y = y;
this.width = width;
this.height = height;

try {
image = ImageIO.read(getClass().getResourceAsStream(imageLocation));
} catch (Exception e) {
System.err.println("Failed to load Image: " + imageLocation);
e.printStackTrace();
}
}

public GameCharacter(PremadeCharacter premadeCharacter) {
this.premadeCharacter = premadeCharacter;
if(premadeCharacter == PremadeCharacter.ALIEN) {
//initialize idle animation
String[] idleAnimationImageLocations = {
"/game_tools/game_character_images/alien/idle/red__0000_idle_1.png",
"/game_tools/game_character_images/alien/idle/red__0001_idle_2.png",
"/game_tools/game_character_images/alien/idle/red__0002_idle_3.png"
};
this.idleAnimation = new Animation(idleAnimationImageLocations);
//initialize move right animation
String[] moveRightAnimationImageLocations = {
"/game_tools/game_character_images/alien/walk/red__0006_walk_1.png",
"/game_tools/game_character_images/alien/walk/red__0007_walk_2.png",
"/game_tools/game_character_images/alien/walk/red__0008_walk_3.png",
"/game_tools/game_character_images/alien/walk/red__0009_walk_4.png",
"/game_tools/game_character_images/alien/walk/red__0010_walk_5.png",
"/game_tools/game_character_images/alien/walk/red__0011_walk_6.png",
};
this.moveRightAnimation = new Animation(moveRightAnimationImageLocations);

//adjust framerate
idleAnimation.frameRate = 10;
moveRightAnimation.frameRate = 10;
}
}

public GameCharacter(int x, int y, PremadeCharacter premadeCharacter) {
this(premadeCharacter);
this.x = x;
this.y = y;
}

public void moveRight(){
this.x += horizontalMovementSpeed;
}

public void moveLeft(){
this.x -= horizontalMovementSpeed;
}

public void moveUp(){
this.y -= verticalMovementSpeed;
}

public void moveDown(){
this.y += verticalMovementSpeed;
}

public void draw(Graphics g) {
if(idleAnimation != null){
idleAnimation.draw(g, x, y, width, height);
if(moveRightAnimation != null && previousX < x){
moveRightAnimation.draw(g, x, y, width, height);
previousX = x;
}
}
else if(image != null){
g.drawImage(image, x, y, width, height, null);
}
else{
g.setColor(color);
g.fillRect(x, y, width, height);
}
// g.setColor(Color.YELLOW);
// moveMouth(g);
// ghost.draw(g);
// x++;
// count++;
}
//if shape == null
//draw rect


public enum PremadeCharacter {
ALIEN,
DOG,
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.