Hello,
I'm new to perl, and am trying to write an ascii game. The game will basically be a third person view of a character, and will accept users input to move the character. Basically I need a little input designing the flow of the game. I can't wait for user input by doing a blocking read, but my current code eats up memory almost right away.
#!/usr/bin/perl
use Term::Info;
use Term::ReadKey;
use Term::Size 'chars';
($cols, $rows) = chars;
ReadMode 4; # Turn off controls keys
sub userInput {
$key = ReadKey(-1);
if (not defined($key)) {
#do nothing
}
elsif ($key eq "\e") {
$key = ReadKey(-1);
if ($key eq "[") {
$key = ReadKey(-1);
if ($key eq "A") {
print "Forward\n";
}
elsif ($key eq "B") {
print "Backward\n";
}
elsif ($key eq "C") {
print "Right\n";
}
elsif ($key eq "D") {
print "Left\n";
}
}
}
elsif ($key eq "q" or $key eq "Q") {
#Change to exit function
exit 0
}
else {
print "Get key $key\n";
}
updateScreen();
}
sub updateScreen {
#add code to refresh screen
userInput();
}
#Accept user input
userInput();
ReadMode 0;
Not much code because I just started, but any help on the most efficient design would be greatly appreciated.
Thanks,
Chris.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.