| 1 | package Games::PangZero; | ||
| 2 | |||
| 3 | $Cheat = 0; | ||
| 4 | $VERSION = '1.4.1'; | ||
| 5 | $DataDir = ''; # Set it to a path to avoid autodetection (e.g. /opt/pangzero/data) | ||
| 6 | |||
| 7 | =comment | ||
| 8 | |||
| 9 | ########################################################################## | ||
| 10 | # | ||
| 11 | # PANG ZERO | ||
| 12 | # Copyright (C) 2006 by UPi <upi at sourceforge.net> | ||
| 13 | # | ||
| 14 | ########################################################################## | ||
| 15 | |||
| 16 | This program is free software; you can redistribute it and//or modify | ||
| 17 | it under the terms of the GNU General Public License version 2, as | ||
| 18 | published by the Free Software Foundation. | ||
| 19 | |||
| 20 | This program is distributed in the hope that it will be useful, | ||
| 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | GNU General Public License for more details. | ||
| 24 | |||
| 25 | You should have received a copy of the GNU General Public License | ||
| 26 | along with this program; if not, write to the Free Software | ||
| 27 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 28 | |||
| 29 | |||
| 30 | ########################################################################## | ||
| 31 | # TODO: | ||
| 32 | ########################################################################## | ||
| 33 | * P4 Bonus probability is balldesc based. | ||
| 34 | * P3 Tour mode..? | ||
| 35 | * P5 Graphics and help for machine gun and power wire | ||
| 36 | * P5 Demo of beating the game at 'normal' difficulty | ||
| 37 | * P4 Even more forgiving collision detection (?) | ||
| 38 | * P4 Smooth numbers in the scoreboard | ||
| 39 | * P3 RotoZoomer smooth parameter to eliminate warning.. | ||
| 40 | * P3 Set DataDir with command line parameter. | ||
| 41 | * P2 Roll your own game | ||
| 42 | * P4 Reorg menu: MenuItem->Update(), MenuItem->Left(), MenuItem->Right(), ... | ||
| 43 | |||
| 44 | Next release: | ||
| 45 | * Sound effect for matrix effect... | ||
| 46 | * Handle possible HST corruption if game exits while merging scores | ||
| 47 | |||
| 48 | |||
| 49 | ########################################################################## | ||
| 50 | # QUICK GUIDE FOR WOULD-BE DEVELOPERS | ||
| 51 | ########################################################################## | ||
| 52 | |||
| 53 | ' | ||
| 54 | This file contains the entire source code of Pang Zero. I know that this is | ||
| 55 | an odd design, but it works for me. You can split the file easily if you | ||
| 56 | want to. | ||
| 57 | |||
| 58 | The parts of the file are organized like this: | ||
| 59 | |||
| 60 | 1. INITIALIZATION OF GLOBAL OBJECTS (configuration, balls, levels, etc) | ||
| 61 | 2. HIGH SCORE TABLE | ||
| 62 | 3. GAME OBJECT PACKAGES | ||
| 63 | 4. UTILITY PACKAGES AND METHODS | ||
| 64 | 5. GAMEBASE AND DESCENDENT PACKAGES (includes the menu) | ||
| 65 | 6. "MAIN" PROGRAM LOOP | ||
| 66 | ' | ||
| 67 | =cut | ||
| 68 | |||
| 69 | 0 | ▶ | use SDL; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 70 | use SDL::Surface; | ||
| 71 | use SDL::Palette; | ||
| 72 | use SDL::PixelFormat; | ||
| 73 | use SDL::Video; | ||
| 74 | use SDL::Event; | ||
| 75 | use SDL::Events; | ||
| 76 | use SDL::Color; | ||
| 77 | use SDL::Config; | ||
| 78 | use SDL::Cursor; | ||
| 79 | use SDL::GFX::Rotozoom; | ||
| 80 | use SDL::Joystick; | ||
| 81 | use SDL::Mouse; | ||
| 82 | use SDL::Image; | ||
| 83 | use SDLx::App; | ||
| 84 | use SDLx::SFont; | ||
| 85 | |||
| 86 | use Carp; | ||
| 87 | |||
| 88 | # SDL objects | ||
| 89 | |||
| 90 | use vars qw ( | ||
| 91 | $App $VERSION $RotoZoomer $Background $ScoreFont $MenuFont $GlossyFont | ||
| 92 | %BallSurfaces | ||
| 93 | $BorderSurface $WhiteBorderSurface $RedBorderSurface $BonusSurface $LevelIndicatorSurface $LevelIndicatorSurface2 | ||
| 94 | $WhiteHarpoonSurface | ||
| 95 | %Sounds $Mixer | ||
| 96 | ); | ||
| 97 | |||
| 98 | # Pang Zero variables and objects | ||
| 99 | |||
| 100 | use vars qw ( | ||
| 101 | $DataDir $ScreenHeight $ScreenWidth $PhysicalScreenWidth $PhysicalScreenHeight $ScreenMargin | ||
| 102 | $SoundEnabled $MusicEnabled $FullScreen $ShowWebsite | ||
| 103 | $DeathBallsEnabled $EarthquakeBallsEnabled $WaterBallsEnabled $SeekerBallsEnabled $Slippery | ||
| 104 | @DifficultyLevels $DifficultyLevelIndex $DifficultyLevel | ||
| 105 | @WeaponDurations $WeaponDuration $WeaponDurationIndex | ||
| 106 | @GameObjects %GameEvents $GameSpeed $GamePause $Game | ||
| 107 | @Players @GuyImageFiles @GuyColors $NumGuys | ||
| 108 | @BallDesc %BallDesc @ChallengeLevels @PanicLevels | ||
| 109 | $UnicodeMode $LastUnicodeKey %Keys %Events %MenuEvents ); | ||
| 110 | |||
| 111 | |||
| 112 | use Games::PangZero::Globals; | ||
| 113 | use Games::PangZero::GameObject; | ||
| 114 | use Games::PangZero::Ball; | ||
| 115 | use Games::PangZero::BonusDrop; | ||
| 116 | use Games::PangZero::GameBase; | ||
| 117 | use Games::PangZero::PlayableGameBase; | ||
| 118 | use Games::PangZero::ChallengeGame; | ||
| 119 | use Games::PangZero::Config; | ||
| 120 | use Games::PangZero::DeadGuy; | ||
| 121 | use Games::PangZero::DeathBall; | ||
| 122 | use Games::PangZero::PanicGame; | ||
| 123 | use Games::PangZero::DemoGame; | ||
| 124 | use Games::PangZero::PlaybackGame; | ||
| 125 | use Games::PangZero::DemoPlaybackGame; | ||
| 126 | use Games::PangZero::RecordGame; | ||
| 127 | use Games::PangZero::DemoRecordGame; | ||
| 128 | use Games::PangZero::EarthquakeBall; | ||
| 129 | use Games::PangZero::FpsIndicator; | ||
| 130 | use Games::PangZero::FragileBall; | ||
| 131 | use Games::PangZero::GamePause; | ||
| 132 | use Games::PangZero::GameTimer; | ||
| 133 | use Games::PangZero::Graphics; | ||
| 134 | use Games::PangZero::Guy; | ||
| 135 | use Games::PangZero::Harpoon; | ||
| 136 | use Games::PangZero::HalfCutter; | ||
| 137 | use Games::PangZero::Hexa; | ||
| 138 | use Games::PangZero::Highscore; | ||
| 139 | use Games::PangZero::Joystick; | ||
| 140 | use Games::PangZero::MachineGun; | ||
| 141 | use Games::PangZero::Meltdown; | ||
| 142 | use Games::PangZero::Menu; | ||
| 143 | use Games::PangZero::MenuItem; | ||
| 144 | use Games::PangZero::Music; | ||
| 145 | use Games::PangZero::Palette; | ||
| 146 | use Games::PangZero::Pop; | ||
| 147 | use Games::PangZero::PowerWire; | ||
| 148 | use Games::PangZero::SeekerBall; | ||
| 149 | use Games::PangZero::SlowEffect; | ||
| 150 | use Games::PangZero::SuperBall; | ||
| 151 | use Games::PangZero::TutorialGame; | ||
| 152 | use Games::PangZero::UpsideDownBall; | ||
| 153 | use Games::PangZero::WaterBall; | ||
| 154 | use Games::PangZero::XmasBall; | ||
| 155 | |||
| 156 | sub SaveScreenshot { | ||
| 157 | my $i = 0; | ||
| 158 | my $filename; | ||
| 159 | do { $filename = sprintf("screenshot%03d.bmp", $i); ++$i } while (-f $filename); | ||
| 160 | SDL::Video::save_BMP($App, $filename); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub Pause { | ||
| 164 | my $pausedSurface = SDL::Image::load("$DataDir/paused.png"); | ||
| 165 | my $event = SDL::Event->new(); | ||
| 166 | |||
| 167 | SDL::Video::blit_surface($pausedSurface, SDL::Rect->new(0, 0, $pausedSurface->w, $pausedSurface->h), | ||
| 168 | $App, SDL::Rect->new(($PhysicalScreenWidth - $pausedSurface->w) / 2, $PhysicalScreenHeight / 2 - 100, 0, 0)); | ||
| 169 | $App->sync(); | ||
| 170 | $Keys = (); | ||
| 171 | $Events = (); | ||
| 172 | while (1) { # Paused, wait for keypress | ||
| 173 | SDL::Events::pump_events(); | ||
| 174 | SDL::Events::wait_event($event); | ||
| 175 | last if $event->type() == SDL_KEYDOWN and $event->key_sym == SDLK_p; | ||
| 176 | if ($event->type() == SDL_KEYDOWN and $event->key_sym == SDLK_ESCAPE) { | ||
| 177 | $Game->{abortgame} = 1; | ||
| 178 | last; | ||
| 179 | } | ||
| 180 | $Game->Exit() if $event->type() == SDL_QUIT; | ||
| 181 | } | ||
| 182 | SDL::Video::blit_surface($Background, SDL::Rect->new(0, 0, $App->w, $App->h), $App, SDL::Rect->new(0, 0, $App->w, $App->h)); | ||
| 183 | Games::PangZero::GameTimer::ResetTimer(); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub HandleEvents { | ||
| 187 | my ($readBothJoystickAxes) = @_; | ||
| 188 | my ($event, $type); | ||
| 189 | |||
| 190 | $event = SDL::Event->new; | ||
| 191 | while (1) { | ||
| 192 | SDL::Events::pump_events(); | ||
| 193 | last unless SDL::Events::poll_event($event); | ||
| 194 | $type = $event->type(); | ||
| 195 | |||
| 196 | if ($type == SDL_QUIT) { | ||
| 197 | $Game->Exit(); | ||
| 198 | } | ||
| 199 | elsif ($type == SDL_KEYDOWN) { | ||
| 200 | my $keypressed = $event->key_sym; | ||
| 201 | if ($keypressed == SDLK_ESCAPE) { | ||
| 202 | $Game->{abortgame} = 1; | ||
| 203 | } elsif ($keypressed == SDLK_F1) { | ||
| 204 | SaveScreenshot(); | ||
| 205 | } elsif ($keypressed == SDLK_p and not $UnicodeMode) { | ||
| 206 | Pause(); | ||
| 207 | } else { | ||
| 208 | $Keys{$keypressed} = 1; | ||
| 209 | $Events{$keypressed} = 1; | ||
| 210 | $MenuEvents{UP} = 1 if $keypressed == SDLK_UP(); | ||
| 211 | $MenuEvents{DOWN} = 1 if $keypressed == SDLK_DOWN(); | ||
| 212 | $MenuEvents{LEFT} = 1 if $keypressed == SDLK_LEFT(); | ||
| 213 | $MenuEvents{RIGHT} = 1 if $keypressed == SDLK_RIGHT(); | ||
| 214 | $MenuEvents{BUTTON} = 1 if $keypressed == SDLK_RETURN(); | ||
| 215 | $LastUnicodeKey = $event->key_unicode() if $UnicodeMode; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | elsif ($type == SDL_KEYUP) { | ||
| 219 | my $keypressed = $event->key_sym; | ||
| 220 | $Keys{$keypressed} = 0; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | Games::PangZero::Joystick::ReadJoystick($readBothJoystickAxes); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub DoMenu { | ||
| 228 | my $oldScreenHeight = $ScreenHeight; | ||
| 229 | my $oldScreenWidth = $ScreenWidth; | ||
| 230 | $ScreenWidth = $PhysicalScreenWidth - $ScreenMargin * 2; | ||
| 231 | $ScreenWidth = int($ScreenWidth / 32) * 32; | ||
| 232 | $Game = Games::PangZero::Menu->new(); | ||
| 233 | my $retval = $Game->Run(); | ||
| 234 | Games::PangZero::Config::SaveConfig(); | ||
| 235 | |||
| 236 | $ScreenWidth = $oldScreenWidth; | ||
| 237 | $ScreenHeight = $oldScreenHeight; | ||
| 238 | |||
| 239 | return $retval; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub DoDemo { | ||
| 243 | my $messages = $Game->{messages} = { | ||
| 244 | 1 => "Use harpoons to pop the balloons", | ||
| 245 | 160 => "Pop them, and they split in two", | ||
| 246 | 300 => "Pop them again and again", | ||
| 247 | 530 => "Popping the smallest ballons makes them disappear", | ||
| 248 | 630 => "The green Super Ball gives you a lot of free time", | ||
| 249 | 720 => "Use this time wisely!", | ||
| 250 | 1150 => "Making a lot of small balls is dangerous! Observe...", | ||
| 251 | 1600 => "Don't let the balloons touch you!", | ||
| 252 | 1708 => "Dying gives you some free time.", | ||
| 253 | 1900 => "So does shooting the flashing balloons.", | ||
| 254 | 2370 => "The yellow Super Ball destroys every balloon", | ||
| 255 | 2650 => "And now... THE SPECIAL BALL DEMO!", | ||
| 256 | 2950 => "The Bouncy Ball bounces twice as high as normal balls.", | ||
| 257 | 3620 => "See?", | ||
| 258 | 4222 => "The Hexa Ball is weightless and travels in a straight line.", | ||
| 259 | 4500 => "So does its offspring.", | ||
| 260 | 5210 => "The blue Water Ball splits every time it bounces.", | ||
| 261 | 5900 => "This can cause a tide of small balls!", | ||
| 262 | 6630 => "The Earthquake Ball will really shake you up.", | ||
| 263 | 7100 => "Its offspring is not as dangerous, but still annoying.", | ||
| 264 | 7800 => "Behold, the Death Ball. It cannot be killed!!!", | ||
| 265 | 8120 => "No, really, it can't! In fact, shooting it makes it multiply.", | ||
| 266 | 8220 => "If you avoid it for 20 secs, Deathballs will get bored and go away.", | ||
| 267 | 8320 => "Also, the yellow Super Ball will destroy the Deathballs for you.", | ||
| 268 | 8800 => "Shooting it too much will lead to the Deathball Meltdown.", | ||
| 269 | 9550 => "Last but not least: here's the Seeker Ball!", | ||
| 270 | 9900 => "This ball will stalk you forever.", | ||
| 271 | 10100 => "Whew! This concludes the Special Ball Demo. Have fun playing!", | ||
| 272 | }; | ||
| 273 | my $record = 0 x 23 . 1 x 18 . 0 x 19 . 2 x 7 . 0 x 31 . 4 x 1 . 0 x 44 . 2 x 43 . 0 x 7 . 4 x 1 . 0 x 22 . 1 x 10 . 0 x 17 . 2 x 38 . 0 x 16 . 2 x 22 . 0 x 42 . 4 x 1 . 0 x 54 . 1 x 43 . 0 x 2 . 4 x 1 . 0 x 28 . 1 x 27 . 0 x 8 . 4 x 1 . 0 x 98 . 2 x 19 . 0 x 11 . 4 x 1 . 0 x 27 . 1 x 24 . 5 x 1 . 1 x 1 . 0 x 17 . 1 x 9 . 0 x 2 . 4 x 1 . 0 x 51 . 2 x 19 . 0 x 14 . 4 x 1 . 0 x 48 . 1 x 14 . 0 x 2 . 4 x 1 . 0 x 51 . 1 x 8 . 0 x 25 . 4 x 1 . 0 x 49 . 2 x 25 . 0 x 3 . 4 x 1 . 0 x 53 . 1 x 12 . 0 x 9 . 4 x 1 . 0 x 101 . 1 x 9 . 0 x 4 . 4 x 1 . 0 x 68 . 1 x 7 . 5 x 1 . 0 x 75 . 2 x 14 . 0 x 2 . 4 x 1 . 0 x 64 . 2 x 38 . 0 x 3 . 4 x 1 . 0 x 13 . 2 x 13 . 0 x 25 . 2 x 25 . 0 x 5 . 4 x 1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1 . 0 x 5 . 1 x 1 . 5 x 1 . 1 x 2 . 0 x 3 . 1 x 1 . 5 x 1 . 1 x 6 . 5 x 1 . 1 x 7 . 5 x 1 . 1 x 6 . 5 x 1 . 1 x 6 . 5 x 1 . 1 x 7 . 0 x 12 . 2 x 7 . 0 x 2 . 4 x 1 . 2 x 2 . 0 x 4 . 4 x 1 . 0 x 135 . 8 x 1 . 0 x 252 . 1 x 57 . 0 x 199 . 2 x 37 . 0 x 3 . 1 x 1 . 5 x 1 . 1 x 29 . 0 x 21 . 1 x 30 . 0 x 37 . 4 x 1 . 0 x 77 . 1 x 17 . 0 x 4 . 2 x 126 . 3 x 1 . 1 x 52 . 5 x 1 . 1 x 64 . 0 x 39 . 8 x 1 . 0 x 140; | ||
| 274 | my $rand = [2199.02,1.12,0.11,1.24,0.11,1.21,0.33,0.19,0.16,0.12,0.07,0.28,0.68]; | ||
| 275 | |||
| 276 | Games::PangZero::Config::SaveConfig(); | ||
| 277 | $Game = Games::PangZero::DemoPlaybackGame->new( 1, 3, $record, $rand, $messages ); | ||
| 278 | $Game->Run(); | ||
| 279 | Games::PangZero::Config::LoadConfig(); | ||
| 280 | $Game->RestoreGameSettings(); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub DoRecordDemo { | ||
| 284 | my ($numguys, $difficulty) = ($NumGuys, $DifficultyLevelIndex); | ||
| 285 | |||
| 286 | $NumGuys = 1; | ||
| 287 | Games::PangZero::Config::SetDifficultyLevel(3); | ||
| 288 | $Game = Games::PangZero::DemoRecordGame->new(); | ||
| 289 | $Game->Run(); | ||
| 290 | print "\n\$record = '", $Game->{record}, "';\n"; | ||
| 291 | print "\$rand = [", join( ', ', @{$Game->{rand}} ), "];\n\n"; | ||
| 292 | $NumGuys = $numguys; | ||
| 293 | Games::PangZero::Config::SetDifficultyLevel($difficulty); | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | ########################################################################## | ||
| 298 | # MAIN PROGRAM STARTS HERE | ||
| 299 | ########################################################################## | ||
| 300 | |||
| 301 | sub Initialize { | ||
| 302 | |||
| 303 | eval { SDL::init(SDL_INIT_EVERYTHING()); }; | ||
| 304 | die "Unable to initialize SDL: $@" if $@; | ||
| 305 | |||
| 306 | Games::PangZero::Config::FindDataDir(); | ||
| 307 | Games::PangZero::Config::LoadConfig(); | ||
| 308 | print "Data directory is at '$DataDir'\n"; | ||
| 309 | my $sdlFlags; | ||
| 310 | if (Games::PangZero::Config::IsMicrosoftWindows()) { | ||
| 311 | $sdlFlags = SDL_ANYFORMAT; | ||
| 312 | } else { | ||
| 313 | $sdlFlags = SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF | SDL_ANYFORMAT | SDL_FULLSCREEN; | ||
| 314 | } | ||
| 315 | |||
| 316 | ($PhysicalScreenWidth, $PhysicalScreenHeight) = Games::PangZero::Graphics::FindVideoMode(); | ||
| 317 | |||
| 318 | $App = SDLx::App->new( | ||
| 319 | flags => $sdlFlags, | ||
| 320 | title => "Pang Zero $VERSION", | ||
| 321 | icon => "$DataDir/icon.png", | ||
| 322 | width => $PhysicalScreenWidth, | ||
| 323 | height => $PhysicalScreenHeight, | ||
| 324 | fullscreen => $FullScreen, | ||
| 325 | delay => 20 | ||
| 326 | ); | ||
| 327 | |||
| 328 | SDL::Mouse::show_cursor(0); | ||
| 329 | |||
| 330 | $Background = SDL::Surface->new( Games::PangZero::Config::IsMicrosoftWindows() ? SDL_SWSURFACE() : SDL_HWSURFACE(), $App->w, $App->h, 16); | ||
| 331 | $Background = SDL::Video::display_format($Background); | ||
| 332 | $ScoreFont = SDLx::SFont->new("$DataDir/brandybun3.png"); | ||
| 333 | $MenuFont = SDLx::SFont->new("$DataDir/font2.png"); | ||
| 334 | $GlossyFont = SDLx::SFont->new("$DataDir/glossyfont.png"); | ||
| 335 | |||
| 336 | Games::PangZero::Graphics::LoadSurfaces(); | ||
| 337 | Games::PangZero::Music::LoadSounds(); | ||
| 338 | Games::PangZero::Joystick::InitJoystick(); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub MainLoop { | ||
| 342 | my $menuResult = DoMenu(); | ||
| 343 | if ($menuResult eq 'demo') { | ||
| 344 | DoDemo(); | ||
| 345 | } | ||
| 346 | |||
| 347 | #$Game = Games::PangZero::DemoRecordGame->new(); | ||
| 348 | $Game = ($menuResult eq 'challenge') | ||
| 349 | ? Games::PangZero::ChallengeGame->new() | ||
| 350 | : Games::PangZero::PanicGame->new(); | ||
| 351 | @Games::PangZero::Highscore::UnsavedHighScores = (); | ||
| 352 | $Game->Run(); | ||
| 353 | |||
| 354 | bless $Game, 'Menu'; | ||
| 355 | $Game->{abortgame} = 0; | ||
| 356 | { | ||
| 357 | my @gameObjects = @GameObjects; | ||
| 358 | foreach (@gameObjects) { | ||
| 359 | $_->Delete() if ref $_ eq 'Games::PangZero::Guy'; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | SDL::Video::blit_surface($Background, SDL::Rect->new(0, 0, $App->w, $App->h), $App, SDL::Rect->new(0, 0, $App->w, $App->h)); | ||
| 364 | $Games::PangZero::MenuFont->use(); | ||
| 365 | Games::PangZero::Highscore::MergeUnsavedHighScores($menuResult eq 'challenge' ? 'Cha' : 'Pan'); | ||
| 366 | |||
| 367 | return; | ||
| 368 | |||
| 369 | my ($filename, $i) = ('', 1); | ||
| 370 | do { $filename = sprintf("record%03d.txt", $i); ++$i } while (-f $filename); | ||
| 371 | open RECORD, ">$filename"; | ||
| 372 | print RECORD "NumGuys = $NumGuys;\nDifficultyLevelIndex = $DifficultyLevelIndex;\nrecord = '$Game->{record}';\n", | ||
| 373 | "DeathBallsEnabled = $DeathBallsEnabled;\nEarthquakeBallsEnabled = $EarthquakeBallsEnabled;\n", | ||
| 374 | "WaterBallsEnabled = $WaterBallsEnabled;\nSeekerBallsEnabled = $SeekerBallsEnabled;\n", | ||
| 375 | 'rand = [', join(',', @{$Game->{rand}}), "];\n\n"; | ||
| 376 | close RECORD; | ||
| 377 | |||
| 378 | $Game = Games::PangZero::DemoPlaybackGame->new($NumGuys, $DifficultyLevelIndex, $Game->{record}, $Game->{rand}, {}); | ||
| 379 | $Game->Run(); | ||
| 380 | $Game->RestoreGameSettings(); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub ShowErrorMessage { | ||
| 384 | my ($message) = @_; | ||
| 385 | |||
| 386 | return if $ENV{PANGZERO_TEST}; | ||
| 387 | |||
| 388 | $message = "Pang Zero $VERSION died:\n$message"; | ||
| 389 | if (Games::PangZero::Config::IsMicrosoftWindows()) { | ||
| 390 | eval( ' | ||
| 391 | use Win32; | ||
| 392 | Win32::MsgBox($message, MB_ICONEXCLAMATION, "Pang Zero error"); | ||
| 393 | ' ); | ||
| 394 | return; | ||
| 395 | } elsif ($ENV{'DISPLAY'}) { | ||
| 396 | $message =~ s/\"/\\"/g; | ||
| 397 | my @tryCommands = ( | ||
| 398 | "kdialog --msgbox \"$message\"", | ||
| 399 | "gmessage -center \"$message\"", | ||
| 400 | "xmessage -center \"$message\"", | ||
| 401 | ); | ||
| 402 | foreach (@tryCommands) { | ||
| 403 | `$_`; | ||
| 404 | return if $? == 0; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | sub ShowWebPage { | ||
| 410 | my ($url) = @_; | ||
| 411 | |||
| 412 | return if $ENV{PANGZERO_TEST}; | ||
| 413 | |||
| 414 | if (Games::PangZero::Config::IsMicrosoftWindows()) { | ||
| 415 | my $ws = "$DataDir/website.html"; | ||
| 416 | $ws =~ s/\//\\\\/g; | ||
| 417 | exec 'cmd', '/c', $ws; | ||
| 418 | exit; | ||
| 419 | } elsif ($ENV{'DISPLAY'}) { | ||
| 420 | my @tryCommands = ( | ||
| 421 | "gnome-open $url", | ||
| 422 | "mozilla-firefox $url", | ||
| 423 | "firefox $url", | ||
| 424 | "mozilla $url", | ||
| 425 | "konqueror $url", | ||
| 426 | ); | ||
| 427 | foreach (@tryCommands) { | ||
| 428 | `$_`; | ||
| 429 | return if $? == 0; | ||
| 430 | } | ||
| 431 | } else { | ||
| 432 | print "Visit $url for more info about Pang Zero $Games::PangZero::VERSION\n"; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | 1; | ||