forked from deR0R0/sssnac-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
146 lines (122 loc) · 4.79 KB
/
GamePanel.java
File metadata and controls
146 lines (122 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Code Written by Robert Zhao and Nihal Gorthi
// Date 5/2/2025
// Made for Computer Science Foundations Project @ TJHSST
// © 2025 Robert Zhao and Nihal Gorthi
/*
TODO:
✔️ Implement "snapping" to the grid (through the turning)
- Fix the latency of the snake turning, potential solution: queue the inputs in an array, then process them when the input unlocks
- Implement the apple spawning. OPTIONAL: make the apple float up and down
- Implement the snake growing when it eats the apple through the body use of leadership and follwership ideology
- Implement the snake dying when it runs into itself or the wall (the wall is the edge of the screen)
- Implement other modes (eat apple, go faster)
- Crazy Mode or Dual Mode
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class GamePanel extends JPanel {
private static final int FRAME = 900;
private static final int GRID = 15; // gridsize x gridsize
private static final int loopDelay = 5; // delay in milliseconds
private final BackgroundGrid BACKGROUND;
private BufferedImage myImage;
private Graphics myBuffer;
private Timer t;
private SnakeHead player;
private int tick = 0;
private boolean lockInput = false;
private long secs = System.currentTimeMillis()/1000;
// constructors, or basically the setup for the game
public GamePanel() {
// create the buffered image for a smoother game
myImage = new BufferedImage(FRAME, FRAME, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
// create the background grid
BACKGROUND = new BackgroundGrid(GRID, GRID, new Color(143, 205, 57), new Color(168, 217, 72), FRAME, FRAME);
// create the player
player = new SnakeHead(0, 0, FRAME/GRID, 1, "RIGHT", null, null);
// start the game loop without blocking the gui threads
t = new Timer(loopDelay, new GameLoop()); // 840 / the integer = fps. 840 =
t.start();
// allow mouse clicks and stuff
setFocusable(true);
// create lsiteners
addKeyListener(new Keyboard());
}
public class Keyboard extends KeyAdapter {
public void snapToGridX(String direction) {
if(player.getX() % (FRAME / GRID) > (FRAME / GRID)/2) {
player.setX(player.getX() + (FRAME / GRID) - (player.getX() % (FRAME / GRID)));
} else {
player.setX(player.getX() - (player.getX() % (FRAME / GRID)));
}
}
public void snapToGridY() {
if(player.getY() % (FRAME / GRID) > (FRAME / GRID)/2) {
player.setY(player.getY() + (FRAME / GRID) - (player.getY() % (FRAME / GRID)));
} else {
player.setY(player.getY() - (player.getY() % (FRAME / GRID)));
}
}
@Override
public void keyPressed(KeyEvent e) {
if(lockInput) {
return;
}
// check for key presses and change the direction of the player
switch (e.getKeyCode()) {
case KeyEvent.VK_UP -> {
if(!player.getDirection().equals("DOWN")) {
snapToGridX("up");
player.setDirection("UP");
lockInput = true;
}
}
case KeyEvent.VK_DOWN -> {
if(!player.getDirection().equals("UP")) {
snapToGridX("down");
player.setDirection("DOWN");
lockInput = true;
}
}
case KeyEvent.VK_LEFT -> {
if(!player.getDirection().equals("RIGHT")) {
snapToGridY();
player.setDirection("LEFT");
lockInput = true;
}
}
case KeyEvent.VK_RIGHT -> {
if(!player.getDirection().equals("LEFT")) {
snapToGridY();
player.setDirection("RIGHT");
lockInput = true;
}
}
}
}
}
// override the paintComponent method to draw the buffered image
public void paintComponent(Graphics g) {
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
// game loop
private class GameLoop implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Erase previous frame by overlaying the background
BACKGROUND.draw(myBuffer);
// move the player and draw it
player.move();
player.draw(myBuffer);
// Paint
repaint();
// ticking system to handle the speed of the game
tick++;
if(tick % 30 == 0) {
lockInput = false;
}
}
}
}