Skip to content

Commit 81c8ede

Browse files
committed
Added a jumpscare
1 parent e5fa135 commit 81c8ede

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Diff for: src/homecoming/GamePanel.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ public class GamePanel extends JPanel {
2424
*/
2525
static {
2626
BufferedImage img = null;
27+
BufferedImage jumpScare = null;
2728
BufferedImage background = null;
2829
try {
2930
img = ImageIO.read(new File("download.png"));
31+
jumpScare = ImageIO.read(new File("jumpscare.png"));
3032
background = ImageIO.read(new File("download.jpg"));
3133
} catch (IOException ioe) {
3234
System.err.println("Fatal error: Failed to load images");
3335
System.exit(1);
3436
}
3537
backgroundImg = background;
38+
jumpScareImg = jumpScare;
3639
heartImg = makeColorTransparent(img, Color.WHITE);
3740
}
3841

3942
static final Image heartImg;
43+
static final Image jumpScareImg;
4044
static final Image backgroundImg;
4145
static final Font font = new Font("Courier", Font.BOLD, 30);
4246

@@ -48,7 +52,7 @@ public void paintComponent(Graphics g) {
4852
g.setColor(Color.WHITE);
4953
g.fillRect(0, 0, getWidth(), getHeight());
5054

51-
g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
55+
g.drawImage(Main.jumpScare? jumpScareImg: backgroundImg, 0, 0, getWidth(), getHeight(), null);
5256

5357
/*
5458
* Draw the greeting in the center of the screen

Diff for: src/homecoming/GameState.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package homecoming;
22

33
import java.awt.Point;
4+
import java.awt.event.KeyAdapter;
5+
import java.awt.event.KeyEvent;
46
import java.util.ArrayList;
57

6-
public class GameState {
8+
public class GameState extends KeyAdapter{
79
static final int FALLING_HEART_SIZE = 50;
810

911
static final int SPEED = 5;
@@ -25,7 +27,7 @@ public void update() {
2527
/*
2628
* Every 25 ticks, create a new heart
2729
*/
28-
if(counter % 25 == 0) {
30+
if(counter % 25 == 0 && !Main.jumpScare) {
2931
Point position = new Point((int) (500 * Math.random()), 0);
3032
int speed = (int) (4 * Math.random()) + 2;
3133
hearts.add(new Heart(position, speed));
@@ -34,7 +36,22 @@ public void update() {
3436
for (Heart heart : hearts) {
3537
heart.pos.y += heart.speed;
3638
}
39+
}
3740

41+
@Override
42+
public void keyPressed(KeyEvent e) {
43+
char c = e.getKeyChar();
44+
45+
switch(c) {
46+
case 'y':
47+
Main.GREETING = "Yay!";
48+
break;
49+
case 'n':
50+
Main.GREETING = "</3";
51+
Main.jumpScare = true;
52+
hearts.clear();
53+
break;
54+
}
3855
}
3956

4057
public ArrayList<Heart> getHearts() {

Diff for: src/homecoming/Main.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ public class Main {
1010
/*
1111
* This is a format string, passed to String.format(GREETING, NAME);
1212
*/
13-
static final String GREETING = "Homecoming %s?";
13+
static String GREETING = "Homecoming %s?";
14+
15+
static boolean jumpScare = false;
1416

1517
static final GameState gameState = new GameState();
1618
public static void main(String args[]) throws InterruptedException {
1719
JFrame frame = new JFrame("Homecoming?");
1820

1921
GamePanel game = new GamePanel();
2022
frame.add(game);
23+
frame.addKeyListener(gameState);
2124

2225
frame.setPreferredSize(new Dimension(500, 500));
2326
frame.pack();

0 commit comments

Comments
 (0)