Tải bản đầy đủ (.docx) (10 trang)

Mô phỏng trò chơi bóng bàn

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (50.25 KB, 10 trang )

TRƯỜNG CAO ĐẲNG CÔNG NGHỆ HÀ NỘI
KHOA CÔNG NGHỆ THÔNG TIN
LỚP CNTT3 – K6

BÀI TẬP LỚN
Kỹ Thuật Đồ Họa Pascal
Đề tài: Mô phỏng trò chơi bóng bàn

Nhóm:
1. Phạm Đức Tiến
2. Triệu Bảo Trung
3. Đỗ Viết Hoàng


CODE CHƯƠNG TRÌNH
Program PascalPong;
Uses Crt, graph;
{ global variables }
Var
physics_counter : byte;
raq_y, cpu_raq_y : integer;
ball_x, ball_y : integer;
ball_x_dir, ball_y_dir : integer;
score, score_cpu : integer;
exit : boolean;

//reset variables

Procedure reset_variables;
Begin
physics_counter := 0;



raq_y := 8;
cpu_raq_y := 8;

ball_x := 28;
ball_y := 8;


if Random(1) = 1 then ball_x_dir := 2
else ball_x_dir := -2;
if Random(1) = 1 then ball_y_dir := Random(2) + 1
else ball_y_dir := -(Random(2) + 1);
End;

//init variables

Procedure init_variables;
Begin
Randomize; { set random seed }

score := 0;
score_cpu := 0;

reset_variables;
End;

//handle screen

Procedure handle_screen;
Begin

TextBackground(black);
ClrScr;


TextBackground(white);

GotoXY(ball_x,ball_y);
Write(' ');

TextBackground(white);

GotoXY(1,raq_y);
Writeln(' ');
Writeln(' ');
Writeln(' ');
Write(' ');

GotoXY(61,cpu_raq_y);
Write(' ');
GotoXY(61,cpu_raq_y + 1);
Write(' ');
GotoXY(61,cpu_raq_y + 2);
Write(' ');
GotoXY(61,cpu_raq_y + 3);
Write(' ');

TextBackground(black);


GotoXY(20,25);

Write('Player: ', score);
GotoXY(40,25);
Write('CPU: ', score_cpu);

GotoXY(1,26);
End;

// handle input

Procedure handle_input;
Var
key : char;
Begin
if KeyPressed then
begin
key := ReadKey;
case key of
'q' : exit := true;
'w' : if (raq_y > 1) then raq_y := raq_y - 1;
's' : if (raq_y < 17) then raq_y := raq_y + 1;
end;
end;


End;

//handle physics

Procedure handle_physics;
Begin

physics_counter := physics_counter + 1;

if physics_counter = 10 then
begin
physics_counter := 0;

//AI
if (cpu_raq_y > ball_y - 1) and (cpu_raq_y > 1) then
cpu_raq_y := cpu_raq_y - 1
else if (cpu_raq_y < ball_y - 1) and (cpu_raq_y < 17) then
cpu_raq_y := cpu_raq_y + 1;

//move ball
ball_x := ball_x + ball_x_dir;
ball_y := ball_y + ball_y_dir;

//check y
if (ball_y > 19) or (ball_y < 2) then ball_y_dir := ball_y_dir * -1;


//check x
if ball_x < 3 then //Nguoi choi
begin
if (ball_y >= raq_y) and (ball_y <= raq_y + 4) then
ball_x_dir := ball_x_dir * -1
else //cpu score + 1
begin
score_cpu := score_cpu + 1;
reset_variables;
end;

end
else if ball_x > 58 then //cpu
begin
if (ball_y >= cpu_raq_y) and (ball_y <= cpu_raq_y + 4) then
ball_x_dir := ball_x_dir * -1
else //score + 1
begin
score := score + 1;
reset_variables;
end;
end;

//set correct position for ball


if ball_x < 1 then ball_x := 1;
if ball_y < 1 then ball_y := 1;
end;
End;

//MAIN

BEGIN
ClrScr;
Writeln('');
Writeln('

+---------------------------+');

Writeln('


|

Writeln('

+---------------------------+');

Writeln('

PASCAL PONG v0.1

by Antonio Ni¤o D¡az');

Writeln('');
Writeln('');
Writeln('

Controls');

Writeln('

--------');

Writeln('

W/S : Up/Down');

Writeln('

Q : Quit game');


Writeln('');
Writeln('');
Writeln('
Writeln('');

|');

Press ENTER to start');


Writeln('');
Readln;
init_variables;
exit := false;
Repeat
handle_input;
handle_physics;
handle_screen;
Delay(20);
Until exit;
END.




×