-
Notifications
You must be signed in to change notification settings - Fork 0
/
fortranBird.F90
221 lines (173 loc) · 5.43 KB
/
fortranBird.F90
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Module gl_f90_mod
! NB this version of GL has no shaders, I think... it some bizzare subset of ~2003 era GL.
! we are making a game in Fortran, all bets are off
Use opengl_gl
Use opengl_glu
Use opengl_glut
Use Quads, Only: Quad
Use Worlds
Use Players, Only: Player
! Google it, it's a lovely feature of Fortran...
Implicit None
Integer(GLint), Parameter :: wW = 512, wH = 512
Integer(glcint) :: window
Integer :: frame = 0, score = 0
Logical :: w_down = .false., a_down = .false.,&
s_down = .false., d_down = .false.,&
game_over = .false.
Real :: vx = 10, vy = 10, ph = wW/8.0, pw = wH/8.0, &
px = wW/8.0, py = wH/2.0, wvx = 2, wvy = 0
Real :: tic, toc
Real :: gy = 1.0, difficulty = 1.0
Type(Player) :: bird
Type(World) :: w
Contains
Subroutine draw_text(text, x, y, scale)
Character(Len=*), Intent(In ) :: text
Real, Intent(In ) :: x, y, scale
Integer :: i, p
call glscalef(scale, scale, scale)
Call glColor4f(0.0,0.0,0.0,0.0)
Call glTranslatef(x, y, 0.0_glfloat)
i=1
Do while (i <= len(text))
p = Ichar(text(i:i))
If (i < len(text)+1 .and. text(i:i+1) == '\n') Then
Call glTranslatef(-(i+1)*scale, -scale*1.5, 0.0_glfloat)
i = i+2
Cycle
End If
i = i+1
Call glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, p)
End Do
End Subroutine draw_text
Subroutine move(v)
Real, Intent(In ) :: v(1:2)
Real :: new(1:2)
new = (/px, py/) + v
new(1) = Max(Min(new(1), Real(wW)-pw/2.0), pw/2.0)
new(2) = Max(Min(new(2), Real(wH)-ph/2.0), ph/2.0)
px = new(1)
py = new(2)
End Subroutine move
Subroutine end_game()
Character(Len=16) :: s
Write (s, '(a, i0)') "Game over!", score
Call w%draw()
Call bird%draw()
Call draw_text(s, 0.0, wH*0.5, 0.25)
End Subroutine end_game
Subroutine update()
Real :: v(1:2)
v = (/0.0, -Min(vy-1,gy*difficulty)/)
If (w_down) Then
v(2) = v(2) + vy
End If
If (a_down) Then
v(1) = v(1) - vx
End If
If (s_down) Then
v(2) = v(2) - vy
End If
If (d_down) Then
v(1) = v(1) + vx
End If
If (Sum(v) /= 0.0) Then
Call move(v)
End If
Call bird%set_position((/px, py/))
Call w%update((/wvx*difficulty, wvy/))
Call w%draw()
Call bird%draw()
game_over = w%collided(bird%get_bb())
End Subroutine update
Subroutine display() bind(c)
Call glViewport(0_GLint,0_GLint,wW,wH)
Call glMatrixMode(GL_PROJECTION)
Call glLoadIdentity()
Call glOrtho(0.0_gldouble,wW+0.0_gldouble,0.0_gldouble,wH+0.0_gldouble, 0.0_gldouble, 1.0_gldouble)
Call glClearColor(1.0,1.0,1.0,1.0)
Call glClear(GL_COLOR_BUFFER_BIT)
Call glColor3f(1.0,0.0,0.0)
If (game_over) Then
Call end_game()
Else
Call update()
If (frame == 59) score = score + 1
difficulty = difficulty + 0.001
End If
Call glFlush()
Call glutPostRedisplay()
Call cpu_time(tic)
Do while (tic-toc < 1.0/60.0)
Call cpu_time(tic)
End Do
Call cpu_time(toc)
frame = Mod(frame + 1, 60)
End Subroutine display
Subroutine keyUp(ikey, x, y) bind(c)
Integer(GLbyte), VALUE :: ikey
Integer(GLint), VALUE :: x, y
Select Case(ikey)
Case (119)
! w
w_down = .false.
Case (97)
! a
a_down = .false.
Case (115)
! s
s_down = .false.
Case (100)
! d
d_down = .false.
End Select
End Subroutine
Subroutine keyDown(ikey, x, y) bind(c)
Integer(GLbyte), VALUE :: ikey
Integer(GLint), VALUE :: x, y
Select Case(ikey)
Case (119)
! w
w_down = .true.
Case (97)
! a
a_down = .true.
Case (115)
! s
s_down = .true.
Case (100)
! d
d_down = .true.
Case (27)
Stop
End Select
End Subroutine
Subroutine init()
Integer(GLenum) :: type
Real :: t
Integer, Allocatable :: seed(:)
Integer :: n
Call random_seed(size=n)
Allocate(seed(n))
Call random_seed(get=seed)
Call glutInitWindowSize(wW,wH)
Call glutInit()
type = GLUT_RGB
type = ior(type,GLUT_SINGLE)
Call glutInitDisplayMode(type)
window = glutCreateWindow("Fortran Bird!")
Call glutDisplayFunc(display)
Call glutKeyboardFunc(keyDown)
Call glutKeyboardUpFunc(keyUp)
Call glutMainLoop()
End Subroutine init
End module gl_f90_mod
Program main
Use opengl_gl
Use opengl_glut
Use gl_f90_mod
Call bird%init((/px, py/), (/pw, ph/), 0.0)
Call w%generate(0.0, pw*2, Real(wH), pw, pw*3, 0.1*Real(wH), 0.75*Real(wH))
Call init()
End Program main