forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDinosaur.java
More file actions
64 lines (56 loc) · 1.85 KB
/
Dinosaur.java
File metadata and controls
64 lines (56 loc) · 1.85 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
package Dino_Game_java;
import java.awt.Rectangle;
// Represents the Dinosaur entity
class Dinosaur {
private int x, y; // Current position
private int initialY;
private int jumpHeight = 280; // Max height of jump from initialY
private int jumpSpeed = 20;
private boolean jumping = false;
private boolean jumpAscending = false; // True if going up, false if going down
private int width = 5 * Game.unit; // Approximate width
private int height = 10 * Game.unit; // Approximate height (can be more precise)
public Dinosaur(int x, int y) {
this.x = x;
this.y = y;
this.initialY = y;
}
// Initiates the jump sequence
public void startJump() {
if (!jumping) {
jumping = true;
jumpAscending = true;
}
}
// Updates the dinosaur's Y position during a jump
public void updateJump() {
if (jumping) {
if (jumpAscending) {
y -= jumpSpeed; // Move up
if (y <= initialY - jumpHeight) { // Reached peak
y = initialY - jumpHeight;
jumpAscending = false; // Start descending
}
} else {
y += jumpSpeed; // Move down
if (y >= initialY) { // Landed
y = initialY;
jumping = false; // Jump finished
}
}
}
}
public boolean canJump() {
return !jumping;
}
public boolean isJumping() {
return jumping;
}
public int getX() { return x; }
public int getY() { return y; }
public Rectangle getBounds() {
// More accurate bounds needed based on actual drawing
// This is a placeholder
return new Rectangle(x - 2 * Game.unit, y - 16 * Game.unit, 11 * Game.unit, 17 * Game.unit);
}
}