Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This is preliminary code for game hackers, it is based on the current SDL and includes NPCs, Enemies, a map and a player character. There is also a collision detection system :

unit module PaganVisions; class PaganVisions::AdventurersGuild is PaganVisions::Map { method BUILD() { $!bg_image = SDL_LoadBMP_RW("./pics"/bg-adventurersguild-1.png +"); } } class PaganVisions::Enemy is PaganVisions::Entity { } unit module PaganVisions; class PaganVisions::Entity is PaganVisions::GameObject { has $!direction; has $!moving; has $!dx; ### move x + dx has $!dy; has $!leftstaticimagelib ### StateImagelibrary.pm6 has $!righttstaticimagelib has $!upstaticimagelib has $!downstaticimagelib has $!leftimagelib has $!rightimagelib has $!upimagelib has $!downimagelib method getImage() { if ($direction == "1direction" and $moving) return $leftimagelib.getImage(); if ($direction == "2direction" and $moving) return $rightimagelib.getImage(); if ($direction == "3direction" and $moving) return $upimagelib.getImage(); if ($direction == "4direction" and $moving) return $downimagelib.getImage(); if ($direction == "1direction") return $leftstaticimagelib.getImage(); if ($direction == "2direction") return $rightstaticimagelib.getImage(); if ($direction == "3direction") return $upstaticimagelib.getImage(); if ($direction == "4direction") return $downstaticimagelib.getImage(); return $leftstaticimagelib.getImage(); } method move_left() { $direction = "1direction"; $x -= $dx; } method move_right() { $direction = "2direction"; $x += $dx; } method move_up() { $direction = "3direction"; $y -= $dy; } method move_down() { $direction = "4direction"; $y += $dy; } } class PaganVisions::GameObject { has $!x; has $!y; has $!w; has $!h; method collide($go) { my $r = 0; if ($!x > $go.x) { $r += 1; } elsif ($!x <= $go.x) { $r += 0; } elsif ($!x < $go.x + $go.w) { $r += 1; } elsif ($!x >= $go.x + $go.w) { $r += 0; } if ($!y > $go.y) { $r += 1; } elsif ($!y <= $go.y) { $r += 0; } elsif ($!y < $go.y + $go.h) { $r += 1; } elsif ($!y >= $go.y + $go.h) { $r += 0; } if ($r == 4) { return 1; } else { return 0; } } } class PaganVisions::Map { has $!x; has $!y; has $!dx; ### move x + dx has $!dy; has $!bg_image; method BUILD($filename) { $!bg_image = SDL_LoadBMP_RW($filename); } method move_left() { $!x -= $!dx; } method move_right() { $!x += $!dx; } method move_up() { $!y -= $!dy; } method move_down() { $!y += $!dy; } } class PaganVisions::NPC is PaganVisions::Entity { } class PaganVisions::PlayerEntity is PaganVisions::Entity { } class PaganVisions::Player is PaganVisions::PlayerEntity { submethod BUILD() { //imagelibs addImage($filename) } } class PaganVisions::StateImageLibrary { has @!images; has $index; method addImage($filename) { my $image = SDL_LoadBMP_RW($filename); push($images, $image); } method getImage() { if ($index >= length(@images)) $index = 0; return @images[$index++]; } method getImageWithIndex($ind) { return @images[$ind++]; } } #!perl6 use SDL; SDL::init($SDL_INIT_EVERYTHING) { die "Failed to initialize libSDL with reason: "' ~ SDL::get_error( +) ~ "'"; } my $screen = SDL:SDL_SetVideoMode(640,480,32,undef); my $player = new Player(x => 100, y => 200, dx = 1, dy = 1, direction => "2direction"); my $map = new Map(x => 0, y =>0, dx => 1, dy => 1);


In reply to Perl 6 RPG base code (SDL) by holyghost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found