-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdino.java
120 lines (105 loc) · 3.05 KB
/
dino.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* It's Dino class
*
* @Muhammad Ammar Nabil
* @version beta
*/
public class dino extends Actor
{
private int counter = 0;
private int counterMin = 0;
private int counterJump = 0;
private int jumpSpeed;
private boolean walk = true;
private boolean down;
private boolean jumpPress = false;
private boolean onGround = true;
private boolean pressed;
static public boolean alive = true;
public dino()
{
setImage("dino-jump.png");
setLocation(70,279);
setRotation(-90);
}
public void act()
{
if(alive){
if(onGround){
counter++;
counterJump = 0;
counterMin = 0;
if(counter>=5) {
setLocation(70, 279);
counter = 0;
running();
}
if (Greenfoot.isKeyDown("down")){
down = true;
crouch();
}else {
down = false;
if (jumpState() && !jumpPress) {
setImage("dino-jump.png");
setLocation(70, 274);
move(5);
jumpSpeed = 10;
onGround = false;
jumpPress = true;
} else {
if(!jumpState()){
jumpPress = false;
} else {
running();
}
}
}
} else {
counterMin++;
if((jumpPress && jumpState() && counterJump<=12) ||
counterMin < 8){
counterJump++;
move(10);
} else {
counterJump = 20;
jumpSpeed--;
move(jumpSpeed);
if(getY() >= 274){
setLocation(70, 279);
onGround = true;
}
}
}
if (isTouching(obstacle.class)){
alive = false;
}
}else{
getWorld().addObject(new gameOver(),450, 200);
}
}
private void running(){
if(walk) {
setImage("dino-0.png");
walk = false;
}else {
setImage("dino-1.png");
walk = true;
}
}
private void crouch(){
setLocation(86,296);
if(walk) {
setImage("dino-crouch-0.png");
}else {
setImage("dino-crouch-1.png");
}
}
private boolean jumpState() {
if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("space")){
return true;
}
if(pressed){return true;}
return false;
}
}