Some fixes
[sdlgit/SDL_perl.git] / lib / SDL / Tutorial / Pong.pm
CommitLineData
fdcacfa9 1use strict;
2use warnings;
3
4package Ball;
5#use SDL::Game::Rect;
6
7sub new {
8 my $class = shift;
9 my $self = {
921b4c7b 10 'rect' => SDL::Game::Rect->new(20, 20, 10, 10),
fdcacfa9 11 'speed' => 4,
12 'direction' => 5,
13 'color' => SDL::Color->new(-r => 0x00, -g => 0xcc, -b => 0x00),
14 };
15 bless $self, $class;
16}
17
18sub update {
19
20}
21
22package main;
23use SDL;
921b4c7b 24use SDL::Game::Rect;
fdcacfa9 25use SDL::App;
26
27
28my $app = SDL::App->new(
29 -title => 'Pong',
30 -width => 640,
31 -height => 480,
32 -depth => 16,
33);
34
35my $event = SDL::Event->new;
36my $ball = Ball->new;
37
921b4c7b 38my $bg_color = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0x00 );
39my $back = SDL::Rect->new( 0, 0, $app->width, $app->height);
40my $player = SDL::Rect->new( 100, 30, 20, 90);
fdcacfa9 41my $fg_color = SDL::Color->new( -r => 0xcc, -g => 0xcc, -b => 0xcc );
42
43event_loop() while 1;
44
921b4c7b 45
fdcacfa9 46sub event_loop {
921b4c7b 47 my $held_down_key = undef;
48 while ($event->poll || defined $held_down_key) {
fdcacfa9 49 my $type = $event->type;
50 exit if $type == SDL_QUIT;
51
921b4c7b 52 if ($type == SDL_KEYDOWN || defined $held_down_key) {
53 if($event->key_name eq 'down' || $held_down_key eq 'down')
54 {
55 $player->y($player->y + 2) ;
56 $held_down_key = 'down'
57 }
58 if($event->key_name eq 'up' || $held_down_key eq 'up')
59 {
60 $player->y($player->y - 2) ;
61 $held_down_key = 'up'
62 }
63
fdcacfa9 64 }
921b4c7b 65 if ($type == SDL_KEYUP)
66 {
67 $held_down_key = undef;
68 }
69 draw_screen();
fdcacfa9 70 }
921b4c7b 71
72
fdcacfa9 73}
74
75sub draw_screen {
921b4c7b 76
77 $app->fill($back, $bg_color);
78 $app->fill($player, $fg_color);
fdcacfa9 79
80# if I uncomment this line, the window buttons go away!!! WTF???
921b4c7b 81
82 $app->sync();
fdcacfa9 83}
84
85__END__
86
87=head1 PONG TUTORIAL
88
89This tutorial is intended to help you build your very own version of the Pong game and/or variations of it, using SDL Perl.
90
91Just in case you live under a rock, Pong is one of the earliest arcade games, a true classic by Atari Inc. The game has two simple rectangles scrolling up and down trying to hit a (square) ball that bounces around, and could be thought of as a table tennis simulation.
92
93=head2 Part 1: We start with a Rect
94
95In Pong, the player controls a rectangle that moves up and down, so creating the rectangle looks like a good place to start:
96
97 my $player = SDL::Game::Rect->new({
98 -top => 10,
99 -left => 20,
100 -width => 6,
101 -height => 32,
102 });
103
104That creates a new L<< SDL::Game::Rect >> object, a rectangle, with the given width/height dimensions and in the given top/left position of the screen.
105
106Wait. Did I say... I<<screen>>?
107
108=head2 Part 0: "The Screen"
109
110In SDL Perl, creating a window screen is very easy and straightforward:
111
112 use SDL;
113 use SDL::App;
114
115 my $app = SDL::App->new(
116 -title => 'Pong', # set window title
117 -width => 640, # window width
118 -height => 480, # window height
119 );
120
121That's it. If you run this code, you'll see a window appear and disappear almost instantly. Why doesn't it stay up? Well, the code is processed linearly, like usual programs are, and with no hidden magic. So, you basically said "create a window" and then the program ended - destroying the window. In order to keep it up and running, listening for events, you need an event loop.
122
123=head3 Creating an (empty) event loop
124
125An event loop is a simple infinite loop that captures events (like a key pressed or released from the keyboard, mouse movement, etc) and either does something about it or dispatches it to any object that might.
126
127For this simple game we don't need a very sofisticated event loop, so let's create a simple one.
128
129 event_loop() while 1;
130
131Yay, an infinite loop! Now we are free to define our very own event loop any way we want. Let's make it an empty sub for starters:
132
133 sub event_loop {
134 }
135
136Ok. If you run it, you'll see your C<< $app >> window displayed until you force to shutdown the program by typing C<< Ctrl-C >> or something. Other than that, our event loop doesn't do anything,
137
138=head2 Part 1 (cont.) - Drawing our Rect on the screen
139
140# TODO
141
142=head2 Part 2 - Our first event: tracking user movement
143
144# TODO
145
146Now let's query some events!
147
148First, we need to use the L<< SDL::Event >> module. Add this to the beginning of our code:
149
150 use SDL::Event;
151 my $event = SDL::Event->new;
152
153
154Now let's rewrite the C<< event_loop >> subroutine to take advantage of our event object. The new subroutine should look like this:
155
156 sub event_loop {
157 # first we poll if an event occurred...
158 while ($event->poll) {
159
160 # if there is an event, we check its type
161 my $type = $event->type
162
163 # handle window closing
164 exit if $type == SDL_QUIT;
165 }
166 }
167
168
169#TODO
170
171=head3 Hey, don't move away from the court! Our first collision detection.
172
173=head2 Part 3 - Enter "the Ball"
174
175#TODO
176
177=head3 Some vetorial background
178
179#TODO
180
181=head2 Part 4 - Collision Detection
182
183#TODO
184
185=head2 Part 5 - Our hero's nemesis appears
186
187#TODO
188
189=head3 (really) basic IA
190
191#TODO
192
193=head2 Part 6 - Counting (and showing) the score
194
195#TODO