A Flappy Bird clone made in Fjord
1using System;
2using System.Linq;
3using Fjord;
4using Fjord.Modules.Debug;
5using Fjord.Modules.Game;
6using Fjord.Modules.Graphics;
7using Fjord.Modules.Input;
8using Fjord.Modules.Mathf;
9using Fjord.Modules.Sound;
10using static SDL2.SDL;
11
12namespace Flappy_Bord {
13 // Scene class this is where your game is
14
15 class pipel {
16 public V2f position;
17 public bool can_add_p;
18
19 public pipel(V2f pos) {
20 can_add_p = true;
21 position = pos;
22 }
23 }
24
25 public class main : scene
26 {
27 bird player = new bird();
28
29 pipel[] p = new pipel[6];
30
31 texture pipe = new texture("pipe.png");
32 V2 pipe_size = new V2(0, 0);
33
34 short points = 0;
35 short high_score = 0;
36
37 texture background = new texture("background.png");
38
39 public main() {
40 pipe_size = pipe.get_size();
41
42 Random rand = new Random();
43
44 for(var i = 0; i < p.Length; i++) {
45 p[i] = new pipel(new V2f(500 + 60 * i, rand.Next(30, 170)));
46 }
47 }
48
49 public override void on_load()
50 {
51 game.MAX_FPS = 60;
52 draw.load_font("Arcadia");
53
54 Sound.load_sound("jump", "jump");
55 Sound.load_sound("die", "die");
56 Sound.load_sound("point", "point");
57
58 // This is where you load all your scenes
59 // The if statement is so that it doesn't trigger multiple times
60
61 game.set_render_resolution(game.renderer, 360, 203);
62
63 if(!scene_handler.get_scene("game-template")) {
64
65 // Add all scenes
66 scene_handler.add_scene("game-template", new main());
67
68 // Load the first scene this can later be called in any file as for example a win condition to switch scene.
69 scene_handler.load_scene("game-template");
70 }
71 }
72
73 // Update method
74 // This is where all your gamelogic is
75
76 public override void update()
77 {
78 player.update();
79
80 for(var i = 0; i < p.Length; i++) {
81 if(!(player.get<Transform>().position.y < p[i].position.y + 30 && player.get<Transform>().position.y > p[i].position.y - 35)) {
82 if(p[i].position.x > 92 && p[i].position.x < 102) {
83 restart();
84 Sound.play_sound("die");
85 }
86 } else {
87 if(p[i].position.x > 99 && p[i].position.x < 100) {
88 if(p[i].can_add_p) {
89 p[i].can_add_p = false;
90 points++;
91 Sound.play_sound("point");
92 }
93 }
94 }
95
96 p[i].position.x -= 4f * (float)game.delta_time;
97 if(p[i].position.x < 0 - pipe_size.x) {
98 p[i].position.x = 360;
99 p[i].can_add_p = true;
100 }
101
102 if(points > high_score)
103 high_score = points;
104 }
105
106 if(input.get_key_just_pressed(input.key_r)) {
107 restart();
108 }
109
110 if(input.get_key_just_pressed(input.key_escape)) {
111 game.stop();
112 }
113
114 base.update();
115 }
116
117 // Render method
118 // This is where all your rendering is
119
120 public override void render()
121 {
122 draw.texture(new V2(0, 0), background);
123
124 player.render();
125
126 for(var i = 0; i < p.Length; i++) {
127 pipe.set_fliptype(flip_type.none);
128 draw.texture(new V2((int)p[i].position.x, (int)p[i].position.y + 30), pipe);
129 pipe.set_fliptype(flip_type.vertical);
130 draw.texture(new V2((int)p[i].position.x, (int)p[i].position.y - 35 - pipe_size.y), pipe);
131 }
132
133 draw.text(new V2(10, 5), "Arcadia", 6, "Score: " + points.ToString());
134 draw.text(new V2(10, 11), "Arcadia", 6, "High Score: " + high_score.ToString());
135
136 base.render();
137 }
138
139 public void restart() {
140 player = new bird();
141
142 Random rand = new Random();
143
144 for(var i = 0; i < p.Length; i++) {
145 p[i] = new pipel(new V2f(500 + 60 * i, rand.Next(30, 170)));
146 }
147
148 points = 0;
149 }
150 }
151
152 // Main Class
153
154 class Program
155 {
156 public static void Main(string[] args)
157 {
158 // Function that starts game
159 // The parameter should be your start scene
160 game.set_resource_folder("resources");
161 game.set_asset_pack("main");
162 game.run(new main());
163 }
164 }
165}