Repository navigation
Expand file tree
/
Copy pathplayFunctions.cpp
More file actions
148 lines (123 loc) · 3.78 KB
/
Copy pathplayFunctions.cpp
File metadata and controls
148 lines (123 loc) · 3.78 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
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
#include <stdio.h>
#include <sstream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_native_dialog.h>
#include "functions.h"
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
extern ALLEGRO_DISPLAY *display;
extern ALLEGRO_EVENT_QUEUE *event_queue;
extern ALLEGRO_TIMER *timer;
extern Image background1[7], background2[6];
extern Image character;
extern Image sprite;
extern Image block[350];
extern Image jump_bitmap;
extern Image run[8];
extern ALLEGRO_SAMPLE *sample;
extern ALLEGRO_SAMPLE *walks;
extern int posx;
extern int posy;
extern int spriteDirection;
void play(ALLEGRO_FONT *font){
// move ball across screen using keyboard.
int dx = 0;
int dy = 0;
int i;
float time = 0;
bool jump;
bool walk = false;
int frames = 0;
int mouse_x;
int mouse_y;
int show_bbox = false;
bool doexit = false;
if (!font){
al_show_native_message_box(display, "Error", "Error", "Could not load comic.ttf", nullptr, ALLEGRO_MESSAGEBOX_ERROR);
doexit = false;
}
while (!doexit) {
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
doexit = true;
}
// set direction if key pressed
if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
if (ev.keyboard.keycode == ALLEGRO_KEY_SPACE){
dy = -9;
}
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_LEFT:
case ALLEGRO_KEY_A:
dx = 3;
spriteDirection = 1;
walk = true;
break;
case ALLEGRO_KEY_RIGHT:
case ALLEGRO_KEY_D:
dx = -3;
spriteDirection = 0;
walk = true;
break;
}
}
// display background and blocks
if (ev.type == ALLEGRO_EVENT_TIMER) {
posy += dy;
switch (collision_x()) {
case 'l':
jump = false;
if (dx < 0) {
posx += dx;
}
break;
case 'r':
jump = false;
if (dx > 0) {
posx += dx;
}
break;
default:
jump = true;
posx += dx;al_play_sample(walks, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
break;
}
switch (collision_y(dy, time + 1)) {
case 't':
jump = false;
dy = 0;
time = 0;
break;
case 'b':
jump = true;
dy = 0;
time++;
posy += time * 0.22;
break;
default:
jump = true;
time++;
posy += time * 0.22;
break;
}
if (posy >= 690) {
printf("\nDeath by falling!");
break;
}
frames++;
if (frames >= 28) {
frames = 0;
}
display_background(2); // Prints background to screen
display_structure(3); // Prints blocks to screen
display_character(jump, walk, frames / 4); // Prints character to screen
display_timer(font); // Displays timer
al_flip_display(); // Updates screen
}
}
highscores(display_timer(font), font);
}