本文实例为大家分享了C语言实现乒乓球比赛的具体代码,供大家参考,具体内容如下
1)、基本要求
用8个LED发光管(红黄绿)的来回滚动显示来模拟打乒乓球时乒乓球在两边球台上的来回运动。比赛双方用按钮/开关(双方各用1个按钮/开关)的方法来模拟发球与接球,即发球方按动其控制的按钮/开关/健,球从发球方一侧向对方运动(LED发光管从发球方到对方逐个点亮,滚动显示),当球运动至接球方时,接球方立即按动其控制的按钮/开关/键,“击球”使球“弹回”发球方一侧,如此周而复始,直至在规定的击球时间内有一方未能完成击球动作,从而对方得一分,然后继续比赛。比赛规则可参照一般的乒乓球比赛规则。
要求:
(1). 球以每0.5秒滚过1个LED的速度移动;
(2). 回球需在球到达后的0.5秒内进行(按下按钮),否则按失球计;
(3). 球未到达提前按下按钮按失球处理,不起回球作用;
(4). 用扩展的4个七段数码(小键盘上)为双方记分(每方2个八段码, ,按10进制计数,初始值00:00,可自行设计显示及规则)
2)、设计提示
球到达接球方后,立即读接球方的按钮状态,若未按则有效(若已按下则按失球计),然后不断读此按钮状态,直到按下代表“击球”动作。(采用中断,可以用同一个按键轮流按动识别)。
3)、进一步设计要求
(1). 在8×8点阵(虚拟大屏)同步显示球移动情况,设计显示输赢球图形状态、效果等;移动、击球等精确(中断)计时;
(2). 通过拨动开关或键盘设定改变球移动速度,即每个LED管点亮时间分别为0.5秒、0.3秒、0.2秒;通过拨动开关或键盘设定改变允许击球时间0.3秒、0.2秒、0.1秒;球移动速度取决于击球时间,即球移动速度(每个LED点亮时间)在0.2~0.5秒内可变,具体取决于球到达接球方后击球所花的时间0~0.5秒如花费的时间短则回球后球移动速度快反之回球后移动
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
#include "interface.h" void init8255( void ); const unsigned short Port8255Base=0x288; const unsigned char ControlWord8255=0x80; Input/Output.PortA:Output unsigned char gData=0x80; int flag=0; int num=0; const unsigned char ControlWord8255B = 0x82; const unsigned char ControlWord8255E = 0x88; void init8255B( void ); int scoreleft,scoreright; int speedlamp; int speed2; int speed3; void turnleft() { gData=1; while (1) { PortWriteByte(Port8255Base,gData); if (gData==128) { flag=0; break ; } gData*=2; sleep(speedlamp); } } void turnright() { gData=128; while (1) { PortWriteByte(Port8255Base,gData); if (gData==1) { flag=1; break ; } gData/=2; sleep(speedlamp); } } unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; BYTE pdata; BYTE reflect; void main( ) { init8255B(); //int speedlamp=500; BYTE controldudu; PortReadByte(Port8255Base + 2, &controldudu); printf ( "%d \n" ,controldudu); controldudu/=32; int k5=controldudu%2; controldudu/=2; int k6=controldudu%2; if (k6==0) speedlamp=500; else speedlamp=50; if (k5==0) speed2=500; else speed2=1; printf ( "%d %d\n" ,k6,k5); pdata/=16; scoreleft=0; scoreright=0; BYTE pdata=0; int k0=0; int k7=0; int score0=0; int score1=0; BYTE old=0; BYTE now; int k0old,k0new,k7old,k7new; k0old=old%2; k7old=old/8; while (1) { printf ( "\n当前分数:%d : %d\n" ,scoreleft,scoreright); int yyy=5; while (yyy--){ PortWriteByte(Port8255Base+2,0x01); PortWriteByte(Port8255Base+1,led[scoreright%10]); sleep(1); //delay 100ms PortWriteByte(Port8255Base+2,0x02); PortWriteByte(Port8255Base+1,led[scoreright/10]); sleep(1); PortWriteByte(Port8255Base+2,0x04); PortWriteByte(Port8255Base+1,led[scoreleft%10]); sleep(1); //delay 100ms PortWriteByte(Port8255Base+2,0x08); PortWriteByte(Port8255Base+1,led[scoreleft/10]); sleep(1); PortWriteByte(Port8255Base+2,0); } PortReadByte(Port8255Base + 2, &pdata); pdata/=16; sleep(500); printf ( "--%d" ,pdata); printf ( "old:%d new:%d\n" ,old,pdata); if (old==pdata) { continue ; } else { k7old=old/8; k0old=old%2; k0new=pdata%2; k7new=pdata/8; if (k7old!=k7new) { //printf("---here---\n"); again: turnright(); old=pdata; k0old=old%2; PortReadByte(Port8255Base + 2, &now); now/=16; k0old=now%2; if (speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r1; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=1000; goto r1; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=4000; goto r1; } else { sleep(100); goto r2; } } } } else sleep(speed2); r2: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r1: k0new=pdata%2; printf ( "speedlamp:%d\n" ,speedlamp); if (!(old!=pdata&&k0old!=k0new)) { printf ( "\n--right lose--\n" ); scoreleft++; sleep(500); PortReadByte(Port8255Base + 2, &pdata); pdata/=16; old=pdata; k7old=old/8; k0old=old%2; k0new=pdata%2; k7new=pdata/8; } else { printf ( "\nback\n" ); turnleft(); old=pdata; k7old=old/8; PortReadByte(Port8255Base + 2, &now); k7old=now/128; if (speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r3; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=1000; goto r3; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=4000; goto r3; } else { sleep(100); goto r4; } } } } else sleep(speed2); r4: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r3: k7new=pdata/8; if (!(old!=pdata&&k7old!=k7new)) { printf ( "\n--left lose--\n" ); scoreright++; } else { goto again; } } } else if (k0old!=k0new) { again2: turnleft(); old=pdata; k7old=old/8; PortReadByte(Port8255Base + 2, &now); k7old=now/128; if (speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r7; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=1000; goto r7; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=2000; goto r7; } else { sleep(100); goto r8; } } } } sleep(speed2); r8: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r7: k7new=pdata/8; if (!(old!=pdata&&k7old!=k7new)) { printf ( "\n--left lose--\n" ); scoreright++; sleep(500); PortReadByte(Port8255Base + 2, &pdata); pdata/=16; old=pdata; k7old=old/8; k0old=old%2; k0new=pdata%2; k7new=pdata/8; } else { printf ( "\nback\n" ); turnright(); old=pdata; k7old=old/8; PortReadByte(Port8255Base + 2, &now); now/=16; k0old=now%2; if (speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r5; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=100; goto r5; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if (!(reflect==pdata)) { pdata=reflect; speedlamp=2000; goto r5; } else { sleep(100); goto r6; } } } } sleep(speed2); r6: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r5: k7new=pdata/8; if (!(old!=pdata&&k0old!=k0new)) { printf ( "\n--right lose--\n" ); scoreleft++; } else { goto again2; } } } } } Cleanup(); } void init8255( void ) { if (!Startup()) { printf ( "\n\n ERROR: Open Device Error!请打开实验箱电源\n" ); _getch(); exit (0); } PortWriteByte(Port8255Base + 3, ControlWord8255); } void init8255B( void ) { if (!Startup()) { printf ( "\n\n ERROR: Open Device Error!请打开实验箱电源\n" ); _getch(); exit (0); // return to Windows } PortWriteByte(Port8255Base + 3, ControlWord8255E); } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wenmiao_/article/details/95328388