-
Notifications
You must be signed in to change notification settings - Fork 0
/
game2.pl
160 lines (117 loc) · 2.58 KB
/
game2.pl
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
use strict;
use warnings;
use lib 'local/lib/perl5';
use SDL;
use SDLx::App;
use SDL::Event; #Where ever the event call back is processed
my $app = SDLx::App->new( width => 200, height => 200);
$app->add_event_handler( \&event );
$app->add_event_handler( \&ride );
$app->add_event_handler( \&cursor );
$app->add_show_handler ( \&show );
$app->add_move_handler ( \&move );
my $tank = {
x => 20,
y => 30,
};
my $car = {
x => 100,
y => 100,
};
$app->run();
print "BYE";
exit;
sub event {
my ($event, $app) = @_;
# print "EVENT: " .$event->type . "\n";
if( $event->type == SDL_QUIT ) {
$app->stop;
}
elsif($event->type == SDL_ACTIVEEVENT) {
if($event->active_state & SDL_APPINPUTFOCUS) {
if($event->active_gain) {
return 1;
}
else {
$app->pause(\&event);
# recursive, but only once since the window
# can't lose focus again without gaining it first
}
}
}
elsif($event->type == SDL_KEYDOWN) {
if($event->key_sym == SDLK_p) {
# We're paused, so end pause
return 1 if $app->paused;
# We're not paused, so pause
$app->pause(\&event);
}
}
}
sub show {
my ($delta, $app) = @_;
# print "DELTA: $delta\n";
$app->update;
}
sub move {
my ($step, $app, $t) = @_;
# print "MOVE: $step -- $t\n";
#calc your physics here
$app->draw_rect(
[ $tank->{ x }, $tank->{ y }, 20, 20 ],
[ 0, 0, 0, 255 ]
);
$tank->{ x } += $tank->{ dx };
$tank->{ y } += $tank->{ dy };
$tank->{ dx } = 0;
$tank->{ dy } = 0;
$app->draw_rect(
[ $tank->{ x }, $tank->{ y }, 20, 20 ],
[ 0, 255, 0, 255 ]
);
# car
$app->draw_rect(
[ $car->{ x }, $car->{ y }, 20, 20 ],
[ 0, 0, 0, 255 ]
);
$car->{ x } += 1;
$car->{ y } += 1;
$app->draw_rect(
[ $car->{ x }, $car->{ y }, 20, 20 ],
[ 0, 0, 255, 255 ]
);
}
sub ride {
my ($event, $app) = @_;
$event->type == SDL_KEYDOWN
or return;
if( $event->key_sym == SDLK_UP ) {
$tank->{ dy } -= 5;
}
if( $event->key_sym == SDLK_DOWN ) {
$tank->{ dy } += 5;
}
if( $event->key_sym == SDLK_LEFT ) {
$tank->{ dx } -= 5;
}
if( $event->key_sym == SDLK_RIGHT ) {
$tank->{ dx } += 5;
}
}
my $x;
my $y;
sub cursor {
my ($event, $app) = @_;
$event->type == SDL_MOUSEMOTION
or return;
$app->draw_rect(
[ $x-5, $y-5, 3, 3 ],
[ 0, 0, 0, 255 ]
);
$x = $event->motion_x;
$y = $event->motion_y;
$app->draw_rect(
[ $x-5, $y-5, 3, 3 ],
[ 255, 0, 0, 255 ]
);
}