这是一个简单的c语言源代码,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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "graphics.h" #include "math.h" #define LEFT 0x4b00 #define RIGHT 0x4d00 #define ESC 0x011b int x,y,x1,y1,key,t1,t2,keyx,keylong,keymove,over=0,i,tt=0,fast; int ballx[9]={53,103,263,120,236,390,410,450,600}; int bally[9]={158,239,120,113,30,313,120,263,231},ballz[9]={0}; float ls; win() { float lx,ly,ll; for (i=0;i<9;i++) { if (ballz[i]==1) /*假如球体已经消失就不判定*/ continue ; lx=( float )(ballx[i]-x); ly=( float )bally[i]-y; ll=lx*lx+ly*ly; ls= sqrt (ll); if (( int )ls<17) /*判定两球是否碰到*/ {tt++; /*记录消失几个球*/ fast-=50; /*速度加快*/ if (i%2==0) /*移动加快*/ keymove+=5; else { setcolor(0); rectangle(keyx,460,keyx+keylong,470); keylong-=10; /*长条变短*/ setcolor(YELLOW); rectangle(keyx,460,keyx+keylong,470); } ballz[i]=1; setcolor(0); circle(ballx[i],bally[i],10); if (y<bally[i]) if (x<ballx[i]) {t1=-1;t2=-1;} else {t1=1;t2=-1;} else if (x<ballx[i]) {t1=-1;t2=1;} else {t1=1;t2=1;} } } } fungo() { x=100,y=50,keyx=200,keylong=100,keymove=15,fast=5000; for (i=0;i<9;i++) { setcolor(i+1); circle(ballx[i],bally[i],10); } sleep(1); t1=t2=1; while (1) { while (!kbhit()) { setcolor(YELLOW); rectangle(keyx,460,keyx+keylong,470); x1=x; y1=y; x+=t1; y+=t2; setcolor(4); circle(x,y,6); delay(fast); setcolor(0); circle(x,y,6); win(); if (tt==9) /*胜利*/ { cleardevice(); over=1; setcolor(RED); settextstyle(0,0,4); setusercharsize(3,1,3,1); outtextxy(200,200, " YOU WIN! " ); break ; } if (y>480) /*失败*/ { cleardevice(); over=1; setcolor(RED); settextstyle(0,0,4); setusercharsize(3,1,3,1); outtextxy(200,200, "GAME OVER" ); break ; } else if (x==11&&y==11) /*判定碰到东西*/ {t1=1;t2=1;} else if (x==629&&y==11) {t1=-1;t2=1;} else if (x==11) { if (y<y1) {t1=1;t2=-1;} else {t1=1;t2=1;} } else if (y==11) { if (x<x1) {t1=-1;t2=1;} else {t1=1;t2=1;} } else if (x==629) { if (y1>y) {t1=-1;t2=-1;} else {t1=-1;t2=1;} } else if (y==449) { if (x>=keyx&&x<=keyx+keylong) { if (x<(keyx+keylong/4)) {t1=-1;t2=-1;} else if (x>=(keyx+keylong/4)&&x<(keyx+keylong/2)) {t1=-1;t2=-2;} else if (x>=(keyx+keylong/2)&&x<(keyx+keylong/4*3)) {t1=1;t2=-2;} else {t1=1;t2=-1;} } } } if (over) break ; key=bioskey(0); setcolor(0); rectangle(keyx,460,keyx+keylong,470); if (key==ESC) {cleardevice(); break ;} else if (key==LEFT) keyx-=keymove; else if (key==RIGHT) keyx+=keymove; } } main() { int gd=DETECT,gr; initgraph(&gd,&gr, "c:\\tc" ); fungo(); getch(); sleep(1); closegraph(); } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。