Some fixes
[sdlgit/SDL_perl.git] / lib / SDL / Tutorial / Pong.pm
1 use strict;
2 use warnings;
3
4 package Ball;
5 #use SDL::Game::Rect;
6
7 sub 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
18 sub update {
19     
20 }
21
22 package main;
23 use SDL;
24 use SDL::Game::Rect;
25 use SDL::App;
26
27
28 my $app = SDL::App->new(
29     -title  => 'Pong',
30         -width  => 640,
31         -height => 480,
32         -depth  => 16,
33 );
34
35 my $event = SDL::Event->new;
36 my $ball = Ball->new;
37
38 my $bg_color = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0x00 );
39 my $back = SDL::Rect->new( 0, 0, $app->width, $app->height);
40 my $player = SDL::Rect->new( 100, 30, 20, 90);
41 my $fg_color = SDL::Color->new( -r => 0xcc, -g => 0xcc, -b => 0xcc );
42
43 event_loop() while 1;
44
45
46 sub event_loop {
47         my $held_down_key = undef;
48     while ($event->poll || defined $held_down_key) {
49         my $type = $event->type;
50         exit if $type == SDL_QUIT;
51
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                                 
64         }
65                 if ($type == SDL_KEYUP)
66                 {
67                         $held_down_key = undef;
68                 }
69                  draw_screen();
70     }
71    
72
73 }
74
75 sub draw_screen {
76
77    $app->fill($back, $bg_color);
78    $app->fill($player, $fg_color);
79
80 # if I uncomment this line, the window buttons go away!!! WTF???
81   
82   $app->sync();
83 }
84
85 __END__
86
87 =head1 PONG TUTORIAL
88
89 This tutorial is intended to help you build your very own version of the Pong game and/or variations of it, using SDL Perl.
90
91 Just 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
95 In 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
104 That 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
106 Wait. Did I say... I<<screen>>?
107
108 =head2 Part 0: "The Screen"
109
110 In 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
121 That'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
125 An 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
127 For 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
131 Yay, 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
136 Ok. 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
146 Now let's query some events!
147
148 First, 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  
154 Now 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