forked from skooter500/OOP-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBugZap.java
More file actions
141 lines (116 loc) · 3.25 KB
/
BugZap.java
File metadata and controls
141 lines (116 loc) · 3.25 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
package ie.tudublin;
import processing.core.PApplet;
public class BugZap extends PApplet {
public void settings() {
size(1000, 1000);
}
public void setup() {
reset();
}
float playerX, playerY;
float playerSpeed = 5;
float playerWidth = 40;
float halfPlayerWidth = playerWidth / 2;
float bugX, bugY, bugWidth = 100;
float halfBugWidth = bugWidth / 2;
int score = 0;
void reset() {
resetBug();
playerX = width / 2;
playerY = height - 50;
}
void resetBug() {
bugX = random(halfBugWidth, width - halfBugWidth);
bugY = 50;
}
void drawBug(float x, float y) {
// Draw the bug
stroke(255);
float saucerHeight = bugWidth * 0.7f;
line(x, y - saucerHeight, x - halfBugWidth, y);
line(x, y - saucerHeight, x + halfBugWidth, y);
// line(x - halfBugWidth, y, x - halfBugWidth, y);
line(x - halfBugWidth, y, x + halfBugWidth, y);
float feet = bugWidth * 0.1f;
line(x - feet, y, x - halfBugWidth, y + halfBugWidth);
line(x + feet, y, x + halfBugWidth, y + halfBugWidth);
feet = bugWidth * 0.3f;
line(x - feet, y, x - halfBugWidth, y + halfBugWidth);
line(x + feet, y, x + halfBugWidth, y + halfBugWidth);
float eyes = bugWidth * 0.1f;
line(x - eyes, y - eyes, x - eyes, y - eyes * 2f);
line(x + eyes, y - eyes, x + eyes, y - eyes * 2f);
}
void drawPlayer(float x, float y, float w) {
stroke(255);
float playerHeight = w / 2;
line(x - halfPlayerWidth, y + playerHeight, x + halfPlayerWidth, y + playerHeight);
line(x - halfPlayerWidth, y + playerHeight, x - halfPlayerWidth, y + playerHeight * 0.5f);
line(x + halfPlayerWidth, y + playerHeight, x + halfPlayerWidth, y + playerHeight * 0.5f);
line(x - halfPlayerWidth, y + playerHeight * 0.5f, x - (halfPlayerWidth * 0.8f), y + playerHeight * 0.3f);
line(x + halfPlayerWidth, y + playerHeight * 0.5f, x + (halfPlayerWidth * 0.8f), y + playerHeight * 0.3f);
line(x - (halfPlayerWidth * 0.8f), y + playerHeight * 0.3f, x + (halfPlayerWidth * 0.8f),
y + playerHeight * 0.3f);
line(x, y, x, y + playerHeight * 0.3f);
}
public void keyPressed() {
if (keyCode == LEFT) {
if (playerX > halfPlayerWidth) {
playerX -= playerSpeed;
}
}
if (keyCode == RIGHT) {
if (playerX < width - halfPlayerWidth) {
playerX += playerSpeed;
}
}
if (keyCode == ' ')
{
if (playerX > bugX - halfBugWidth && playerX < bugX + halfBugWidth)
{
line(playerX, playerY, playerX, bugY);
score ++;
resetBug();
}
else
{
line(playerX, playerY, playerX, 0);
}
}
}
void moveBug() {
if ((frameCount % 30) == 0) {
bugX += random(-5, 5);
if (bugX < halfBugWidth) {
bugX = halfBugWidth;
}
if (bugX > width - halfBugWidth) {
bugX = width - halfBugWidth;
}
bugY += 2;
}
}
int gameMode = 0;
public void draw() {
background(0);
fill(255);
text("Score: " + score, 50, 100);
if (gameMode == 0)
{
fill(255);
drawPlayer(playerX, playerY, playerWidth);
drawBug(bugX, bugY);
moveBug();
text("Score: " + score, 20, 20);
}
else
{
textAlign(CENTER, CENTER);
text("GAME OVER!!!", width / 2, height / 2);
}
if (bugY > height - 50)
{
gameMode = 1;
}
}
}