Pathologically Eclectic Rubbish Lister PerlMonks

### "flashing"-like stuff

 on Sep 24, 2011 at 14:54 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

ok, I'm not talking about neon lights or something :P

I'm writing a small perl clone of PONG -- you know, the game everyone played at least once

this is the code so far:

```#!/usr/bin/perl

\$|=1;

use Term::ReadKey;

(\$w, \$h) = GetTerminalSize;

ReadMode 3;

\$x1 = 3; \$y1 = 3;
\$dx = 0.05; \$dy = 0.05;

print "\e[H";
print "\n" x (\$h-2); print " " x (\$w/2-5);
print "##########\r";
\$x=(\$w/2-5);

while (1) {

\$key = ReadKey -1;

if (\$key eq "d") {\$x++; if (\$x>(\$w-10)) {\$x=(\$w-10)};
print "\e[H"; print "\n" x (\$h-2); print "\r"; pri
+nt "\e[K"; print " " x \$x; print "##########\r";}
if (\$key eq "a") {\$x--; if (\$x<0) {\$x=0};
print "\e[H"; print "\n" x (\$h-2); print "\r"; pri
+nt "\e[K"; print " " x \$x; print "##########\r"; }

print "\e[H";
for (0..(\$h-3)) {print " " x (\$w); print "\n"}
print "\e[H\n\n\n"; print "\n" x \$y1; print "\r\e[K"; print " " x \$x1;
+ print "O\r";

\$x1 = (\$x1 + \$dx);
\$y1 = (\$y1 + \$dy);

if ( (\$y1)>=(\$h-5) || \$y1<=0) {\$dy *= -1}
if ( (\$x1)>=(\$w-1) || \$x1<=0) {\$dx *= -1}

}

ReadMode 0;
[download]```

yes - there isn't any "my" or strict or warnigns... but that's not the problem. and yes - the ball bounce in its own way, don't caring about the pad

before go ahead, I've got this problem, when you run that code, the cursor of the ball made everything "flashing" and make the game annoying

how can I avoid this annoying flashing ?

(sorry for my bad english)

Replies are listed 'Best First'.
Re: "flashing"-like stuff
by zentara (Archbishop) on Sep 24, 2011 at 16:55 UTC
how can I avoid this annoying flashing ?

I know this isn't answering your question about Term::ReadKey, but....

Have you considered using a GUI, a Graphical User Interface, like Tk, Wx, Tkx, Gtk2, etc? Just in case you weren't aware that better ways exit for Pong, than just using a terminal. See PONG with TkZinc for example.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh
oh, thanks, but I was trying to write specifically a ASCII version :)
Re: "flashing"-like stuff
by MidLifeXis (Monsignor) on Sep 25, 2011 at 18:54 UTC

I think that I am reading your code correctly, if not, then ignore as appropriate.

When you are printing your movement, you are first clearing the old location, then writing to the new location. There is a period of time where the screen does not have the "sprite" displayed. With graphics-based sprites, I believe that the correct solution is to xor at the new location, then xor at the old location. In your text based version, print to the new location first, then clear the old location. There may be some edge cases <update>especially when old and new overlap</update> that you need to consider.

It has been a number of years since I have tackled this problem, but I seem to remember it being a common one when moving blocks of stuff around the screen.

--MidLifeXis

I solved this way:
```#!/usr/bin/perl

start:
\$|=1;

use Term::ReadKey;
ReadMode 3;
(\$w, \$h) = GetTerminalSize;

\$n = 0.97; #AI sternght
\$l = 10; #difficulty

\$x1 = \$w/2; \$y1 = \$h/2;

@dxs = (-0.09, -0.08, -0.07, -0.06, -0.05, 0.05, 0.06, 0.07, 0.08, 0.0
+9);

@d = (0.05, -0.05);
\$dx = \$d[rand @d];
\$dy = \$d[rand @d];

\$x = \$w/2-5;

while (1) {

\$key = ReadKey -1;

if (\$key eq "d") {\$x++; \$x = \$w-10 if \$x > \$w-10}
if (\$key eq "a") {\$x--; \$x = 0     if \$x < 0}

\$s = " " x ((\$x1-5)*\$n) . "[========]";
\$s .= "\n" x \$y1 . " " x \$x1 . "o";
\$s .= "\n" x (\$h-\$y1-4) . " " x \$x . "[========]\n\nyou:\t\t\$you\ncomp
+uter:\t\$pc\n\r";
print \$s;

\$dx *= -1 if \$x1 >= \$w-1 || \$x1 <= 0;
\$dy *= -1 if  (\$y1 >= \$h-5 && \$x1 > \$x && \$x1 < \$x+10) || (\$y1 <= 1 &&
+ \$x1 > ((\$x1-5)*\$n) && \$x1 < ((\$x1+5)*\$n));
\$dx = \$dxs[\$x1-\$x] if  (\$y1 >= \$h-5 && \$x1 > \$x && \$x1 < \$x+10) || (\$y
+1 <= 1 && \$x1 > ((\$x1-5)*\$n) && \$x1 < ((\$x1+5)*\$n));

\$x1 += \$dx;
\$y1 += \$dy;

if (\$y1 < 0) {ReadMode 0; ++\$you; sleep 1; goto start}
if (\$y1 > \$h-4) {ReadMode 0; ++\$pc; sleep 1; goto start}

select(undef, undef, undef, 0.005/\$l);
system('clear');

}

ReadMode 0;
[download]```
saving everything inside \$s (: now it works perfectly (it has some minor bugs, but whatever)
Re: "flashing"-like stuff
by Anonymous Monk on Sep 24, 2011 at 16:18 UTC
here is an udated version, for better readability:
```#!/usr/bin/perl

\$|=1;

use Term::ReadKey;
ReadMode 3;
(\$w, \$h) = GetTerminalSize; #get size of the screen

\$x1 = 0; \$y1 = 0; #ball start coordinates
\$dx = 0.05; \$dy = 0.05; #increments to the ball coordinates

\$x = 0; #start position of the pad

while (1) {

\$key = ReadKey -1;

if (\$key eq "d") {\$x++; \$x = \$w-10 if \$x > \$w-10} #moving the pad to t
+he right
if (\$key eq "a") {\$x--; \$x = 0     if \$x < 0} #moving the pad to the l
+eft

print "\e[H"; print "\n" x (\$h-2); print "\r\e[K"; print " " x \$x; pri
+nt "##########\r\e[H"; #drawing the pad at the bottom of the screen

for (0..(\$h-3)) {print " " x (\$w); print "\n"} #cleaning the screen ab
+ove the pad
print "\e[H\n\n\n"; print "\n" x \$y1; print " " x \$x1; print "O\r"; #d
+rawing the ball

\$x1 += \$dx; #update coordinates of the ball
\$y1 += \$dy;

\$dx *= -1 if \$x1 >= \$w-1 || \$x1 <= 0; #if the ball hits the left or ri
+ght wall
\$dy *= -1 if \$y1 <= 0 || ( \$y1 >= \$h-5 && \$x1 > \$x && \$x1 < \$x+10 ); #
+if the ball hits the top wall or the pad

if (\$y1 > \$h-4) {ReadMode 0; die "\nyou lose!\n"} #if the ball doesn't
+ hit the pad

}

ReadMode 0;
[download]```
but the problem's still there, somewhere

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927652]
Approved by davies
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-05-22 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (163 votes). Check out past polls.

Notices?