-
Notifications
You must be signed in to change notification settings - Fork 0
/
g2.c
135 lines (108 loc) · 2.95 KB
/
g2.c
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
#include <drawlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
void reflect (float *vx, float *vy, int y, int bary, int barh) {
float hity = fabs((float)(y - bary) / (barh / 2));
float weight = 0.75 * hity + 0.75;
*vx *= -1.0 * weight;
*vy *= weight;
}
int main(void) {
float wait_time = 0.01;
/*playerの幅と高さ*/
int pw = 50, ph = 30;
/*computer no haba to takasa*/
int cw = 30, ch = 60;
/*playerの動く距離の幅*/
int pdy = 20;
/*playerの動く距離の幅*/
int pdx = 100;
/*player,computerの位置.,DL_WIDTH / 2 は画面中央部*/
int pix = 70, cix = DL_WIDTH - 70;
/*playerの初期位置(?)*/
int px = pix, py = DL_HEIGHT / 2;
/*赤板の入力装置*/
int pup = 'w', pdown = 's', pfront = 'e';
/*computerの初期位置*/
int cx = cix, cy = DL_HEIGHT / 2;
/*computer no sayuu no sokudo*/
float cvy = -10.0;
/*弾の大きさ*/
int br = 5;
/*弾の速度*/
float bvx = 40, bvy = 2;
/*?*/
int bx = DL_WIDTH / 2, by = DL_HEIGHT / 2;
int score1 = 0, score2 = 0;
int sx = DL_WIDTH / 2 - 120, sy = 50;
char sscore[] = " : ";
/*dl_get_event用の関数*/
int t, k, x, y;
dl_initialize(1.0);
while (1) {
/*player idou*/
while (dl_get_event(&t, &k, &x, &y)) {
if (t == DL_EVENT_KEY) {
if (k == pup)
py -= pdy;
else if (k == pdown)
py += pdy;
else if (k == pfront) {
if (px > pix)
px = pix;
else
px += pdx;
}
/*com idou*/
}
}
if (py - ph / 2 < 0)
py = ph / 2;
if (py + ph / 2 > DL_HEIGHT)
py = DL_HEIGHT - ph / 2;
if (cy - ph / 2 < 0)
cy = ph / 2;
if (cy + ph / 2 > DL_HEIGHT)
cy = DL_HEIGHT - ph / 2;
/**/
bx += bvx;
if (by - br <= 0) {
by = br + 1;
bvy *= -1;
} else if (by + br >= DL_HEIGHT) {
by = DL_HEIGHT - br - 1;
bvy *= -1;
}
/*atari hanntei*/
if (by > cy - ch / 2 && by < cy + ch / 2) {
if (bvx < 0 && bx - br <= cx + cw / 2 && bx - br >= cx - cw / 2
|| bvx > 0 && bx + br >= cx - cw / 2 && bx + br <= cx + cw / 2) {
reflect (&bvx, &bvy, by, cy, ch);
}
}
/*自動射撃*/
if (bx + br >= DL_WIDTH || bx - br <= 0) {
bx = px + pw + 1;
by = py;
bvx = 40.0;
bvy = 2.0;
}
if(score1 > 999)
score1 = 999;
if(score2 > 999)
score2 = 999;
dl_stop();
dl_clear(DL_C("black"));
/*player*/
dl_rectangle(px - pw / 2, py - ph / 2, px + pw / 2, py + ph / 2, DL_C("white"), 2, 0);
/*computer*/
dl_rectangle(cx - cw / 2, cy - ch / 2, cx + cw / 2, cy + ch / 2, DL_C("green"), 1, 1);
dl_circle(bx, by, br, DL_C("white"), 1, 1);
sprintf(sscore, "%3d:%d", score1, score2);
dl_text(sscore, sx, sy, 2.0, DL_C("white"), 2);
dl_resume();
dl_wait(wait_time);
}
return 0;
}