Merge git://github.com/garu/SDL_perl into experimental
[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 = {
10# 'rect' => SDL::Game::Rect->new(20, 20, 10, 10),
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;
24use SDL::App;
25
26
27my $app = SDL::App->new(
28 -title => 'Pong',
29 -width => 640,
30 -height => 480,
31 -depth => 16,
32);
33
34my $event = SDL::Event->new;
35my $ball = Ball->new;
36
37my $player = SDL::Rect->new( -top => 200, -left => 30, -w => 20, -h => 90);
38my $fg_color = SDL::Color->new( -r => 0xcc, -g => 0xcc, -b => 0xcc );
39
40event_loop() while 1;
41
42sub event_loop {
43 while ($event->poll) {
44 my $type = $event->type;
45 exit if $type == SDL_QUIT;
46
47 if ($type == SDL_KEYDOWN && $event->key_name eq 'down') {
48 $player->y($player->y + 2);
49 }
50 }
51 draw_screen();
52}
53
54sub draw_screen {
55 $app->fill($player, $fg_color);
56
57# if I uncomment this line, the window buttons go away!!! WTF???
58 $app->update($player);
59}
60
61__END__
62
63=head1 PONG TUTORIAL
64
65This tutorial is intended to help you build your very own version of the Pong game and/or variations of it, using SDL Perl.
66
67Just 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.
68
69=head2 Part 1: We start with a Rect
70
71In Pong, the player controls a rectangle that moves up and down, so creating the rectangle looks like a good place to start:
72
73 my $player = SDL::Game::Rect->new({
74 -top => 10,
75 -left => 20,
76 -width => 6,
77 -height => 32,
78 });
79
80That 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.
81
82Wait. Did I say... I<<screen>>?
83
84=head2 Part 0: "The Screen"
85
86In SDL Perl, creating a window screen is very easy and straightforward:
87
88 use SDL;
89 use SDL::App;
90
91 my $app = SDL::App->new(
92 -title => 'Pong', # set window title
93 -width => 640, # window width
94 -height => 480, # window height
95 );
96
97That'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.
98
99=head3 Creating an (empty) event loop
100
101An 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.
102
103For this simple game we don't need a very sofisticated event loop, so let's create a simple one.
104
105 event_loop() while 1;
106
107Yay, 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:
108
109 sub event_loop {
110 }
111
112Ok. 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,
113
114=head2 Part 1 (cont.) - Drawing our Rect on the screen
115
116# TODO
117
118=head2 Part 2 - Our first event: tracking user movement
119
120# TODO
121
122Now let's query some events!
123
124First, we need to use the L<< SDL::Event >> module. Add this to the beginning of our code:
125
126 use SDL::Event;
127 my $event = SDL::Event->new;
128
129
130Now let's rewrite the C<< event_loop >> subroutine to take advantage of our event object. The new subroutine should look like this:
131
132 sub event_loop {
133 # first we poll if an event occurred...
134 while ($event->poll) {
135
136 # if there is an event, we check its type
137 my $type = $event->type
138
139 # handle window closing
140 exit if $type == SDL_QUIT;
141 }
142 }
143
144
145#TODO
146
147=head3 Hey, don't move away from the court! Our first collision detection.
148
149=head2 Part 3 - Enter "the Ball"
150
151#TODO
152
153=head3 Some vetorial background
154
155#TODO
156
157=head2 Part 4 - Collision Detection
158
159#TODO
160
161=head2 Part 5 - Our hero's nemesis appears
162
163#TODO
164
165=head3 (really) basic IA
166
167#TODO
168
169=head2 Part 6 - Counting (and showing) the score
170
171#TODO