1+ /**
2+ * Name: Flappy Bird, try to avoid obstacles to gain points.
3+ * Author: Loris Henry
4+ * Tags: Flappy Bird, game, user interaction
5+ */
6+
7+
8+ model FlappyBird
9+
10+ global {
11+
12+
13+ float step <- 10#ms;
14+ float g <- 9.81 #m/(#s^2);
15+ float tuyau_speed <- 0.5 #m/#s;
16+ file pipe <- image_file("../includes/pipe9.png");
17+ file bird_image <- image_file("../includes/birdd.png");
18+
19+
20+ point param_size <- {3.0, 4.65}parameter:true;
21+ point off_set <- {0.035,0.16,0}parameter:true;
22+ float bird_to_size <- 5.2 parameter:true;
23+ point bird_offset <- {0.017, -0.012} parameter:true;
24+ point text_point <- {0.97, 0.18} parameter:true;
25+ float text_size <- 30.0 parameter:true;
26+ point graph_position <- {0.2, 0.2} parameter:true;
27+
28+ geometry shape <- square(2#m);
29+
30+ float speed_init <- -1.7#m/#s;
31+ int count<-0;
32+ bool game_over <- false;
33+
34+ init {
35+
36+ do reinit_model;
37+ }
38+ action reinit_model {
39+
40+ ask texts {
41+ do die;
42+ }
43+ count<-0;
44+ game_over <- false;
45+
46+ create bird {
47+ location <- point([1, 1]);
48+ }
49+ create texts {
50+ my_text <- "0";
51+ location <- text_point;
52+ f <- font("Flappy Bird Font", text_size);
53+ }
54+ }
55+
56+ action to_game_over {
57+ game_over <- true;
58+ ask bird {
59+ do die;
60+ }
61+ ask tuyau {
62+ do die;
63+ }
64+ }
65+ reflex add_tuyau when:not game_over and every(9/5#s) {
66+ create tuyau {
67+
68+ location <- {3,rnd(0.5, 1.5)};
69+
70+ }
71+ }
72+
73+ }
74+
75+ species bird frequency: game_over ? 0 : 1{
76+ float speed;
77+ bool impulsion <- false;
78+ float size <- 7.5#cm;
79+ rgb color <- #blue;
80+
81+ reflex move {
82+ speed <- speed + g*step;
83+ if impulsion {
84+ speed <- speed_init;
85+ impulsion <- false;
86+ }
87+ location <- point([location.x, location.y + speed*step]);
88+ }
89+
90+ reflex collision when: circle(size,location) intersects union(tuyau collect each.fake_shape){
91+ ask world {do to_game_over;}
92+ }
93+ reflex border when: location.y > 2.0 - size or location.y < 0.2 + size {
94+ ask world {do to_game_over;}
95+ }
96+
97+ aspect default {
98+ draw circle(size) color:color;
99+ }
100+ aspect png {
101+ draw bird_image size:bird_to_size*size at: location+bird_offset rotate:atan(speed/(4*tuyau_speed));
102+ }
103+ }
104+
105+ species tuyau {
106+ float speed <- tuyau_speed;
107+
108+ geometry fake_shape1;
109+ geometry fake_shape2;
110+ geometry fake_shape3;
111+ geometry fake_shape4;
112+ geometry fake_shape5;
113+ geometry fake_shape6;
114+ geometry fake_shape;
115+ bool has_counted <- false;
116+
117+ init {
118+ fake_shape1 <- line([{location.x, 0}, {location.x, location.y - 0.25}]);
119+ fake_shape3 <- rectangle({location.x - 0.22, location.y + 0.25}, {location.x + 0.216, location.y + 0.45});
120+ fake_shape4 <- rectangle({location.x - 0.22, location.y - 0.25}, {location.x + 0.216, location.y -0.05});
121+
122+
123+ fake_shape2 <- line([{location.x, location.y + 0.25}, {location.x, 2}]);
124+
125+ fake_shape5 <- rectangle({location.x - 0.188, location.y + 0.45}, {location.x + 0.187, location.y + 2});
126+ fake_shape6 <- rectangle({location.x - 0.188, location.y - 2}, {location.x + 0.187, location.y - 0.45});
127+ fake_shape <- union(fake_shape3, fake_shape4, fake_shape5, fake_shape6);
128+ }
129+
130+ reflex move {
131+ location <- point([location.x - speed*step, location.y]);
132+ }
133+ reflex update_shape {
134+ fake_shape1 <- line([{location.x, 0}, {location.x, location.y - 0.25}]);
135+ fake_shape2 <- line([{location.x, location.y + 0.25}, {location.x, 2}]);
136+ fake_shape3 <- rectangle({location.x - 0.22, location.y + 0.25}, {location.x + 0.216, location.y + 0.45});
137+ fake_shape4 <- rectangle({location.x - 0.22, location.y - 0.25}, {location.x + 0.216, location.y -0.45});
138+ fake_shape5 <- rectangle({location.x - 0.188, location.y + 0.45}, {location.x + 0.187, location.y + 2});
139+ fake_shape6 <- rectangle({location.x - 0.188, location.y - 2}, {location.x + 0.187, location.y - 0.45});
140+ fake_shape <- union(fake_shape3, fake_shape4, fake_shape5, fake_shape6);
141+
142+
143+
144+
145+ }
146+ reflex count when:location.x < 1.0 and not has_counted{
147+ count <- count + 1;
148+ has_counted <- true;
149+ write name;
150+ ask texts[0] {
151+ my_text <- count as string;
152+ }
153+ }
154+
155+ aspect default {
156+ draw fake_shape1 color:#lime;
157+ draw fake_shape2 color:#lime;
158+ }
159+ aspect png {
160+ draw pipe size:param_size at: location - off_set;
161+ }
162+ }
163+
164+ species texts {
165+ string my_text;
166+ font f;
167+ rgb color <- #black;
168+
169+ aspect default {
170+ draw rectangle(2, 0.2) color:#orange at:{1,0.1};
171+ draw my_text color:color font:f size:text_size;
172+ }
173+ }
174+
175+
176+
177+ experiment main {
178+
179+ bool has_started<-false;
180+ float minimum_cycle_duration <- 10#ms;
181+ output {
182+
183+
184+ layout consoles:false controls:false editors:false navigator:false parameters:false toolbars:false tray:false;
185+
186+
187+
188+
189+
190+ display main fullscreen:true{
191+
192+ image_layer "../includes/background.png";
193+ species bird aspect:png;
194+
195+ species tuyau aspect:png;
196+ species texts;
197+ event "r" {
198+ ask world {
199+ do reinit_model;
200+ }
201+ }
202+
203+ event " " {
204+ if not has_started {
205+ ask simulation {
206+ do resume;
207+ myself.has_started <- true;
208+ }
209+
210+ }
211+ ask bird {impulsion <- true;
212+
213+ }
214+ }
215+ graphics "Start" position:{0,0} size:{0.15, 0.6}{
216+ if not has_started{
217+ draw "Press space to play" font:font("FlappyBirdy", 70) color:#white;
218+
219+ }
220+ }
221+ graphics "Game Over" position:{0,0} size:{0.25, 0.7}{
222+ if game_over{
223+ draw "Game Over" font:font("FlappyBirdy",120, #bold ) color:#white;
224+ draw " Press R to reload" font:font("FlappyBirdy",70, #bold ) color:#white at: {0.9, 1.4};
225+ }
226+
227+ }
228+ }
229+ }
230+
231+ }
0 commit comments