Schrift
[thread]4746[/thread]

TK::Canvas: Test

Leser: 1


<< >> 6 Einträge, 1 Seite
Strat
 2004-03-31 23:39
#41856 #41856
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
Habe ich gerade mal auf die Schnelle geschrieben; viel spass damit:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#! /usr/bin/perl
use warnings;
use strict;

use Tk;
use Tk::Dialog ();

my ( $x, $y ) = ( 320, 460 );    # start ship at that position
my @bulletRecycler  = ();
my %monsters        = ();
my @monsterRecycler = ();
my $monsterCount    = 0;

my $mw = MainWindow->new();
$mw->protocol( 'WM_DELETE_WINDOW', \&ExitApplication );

my $canvas = $mw->Canvas(
   -background => '#000000',
   -height     => 480,
   -width      => 640,
   -cursor     => 'crosshair'
)->pack( -side => 'top', -fill => 'both', -expand => 1 );

my $ship = &GetNewShip($canvas);

for ( my $i = 100; $i < 600; $i += 30 ) {
   for ( my $j = 40; $j < 170; $j += 30 ) {
       &GetNewMonster( $canvas, $i, $j );
   }
}    # for

$canvas->Tk::bind( "<Motion>",   [ \&MoveShip, Ev('x'), Ev('y'), $ship ] );
$canvas->Tk::bind( "<Button-1>", [ \&Fire,     Ev('x'), Ev('y'), $ship ] );

&Tk::MainLoop;

# ------------------------------------------------------------
sub ExitApplication {
   my $dialog = $mw->Dialog(
       -text           => 'Programm wirklich beenden?',
       -bitmap         => 'question',
       -title          => 'Programm beenden',
       -default_button => 'Yes',
       -buttons        => [qw/Ja Nein/],
   );

   my $answer = $dialog->Show();    # and display dialog
   if ( lc($answer) eq 'ja' ) { exit; }

}    # ExitApplication

# ------------------------------------------------------------
sub GetNewShip {
   my $canvas = shift;

   my $ship = $canvas->createPolygon(
       $x - 10, $y,      $x - 10, $y - 10,
       $x - 2,  $y - 14, $x,      $y - 10,
       $x + 2,  $y - 14, $x,      $y - 15,
       $x + 10, $y - 10, $x + 10, $y,
       -outline => '#ffffff',
       -fill    => '#ff0000'
   );
   return $ship;
}    # GetNewShip

# ------------------------------------------------------------
sub GetBullet {
   my ( $canv, $x, $y ) = @_;

   my $bullet;
   if ( scalar @bulletRecycler ) {
       $bullet = shift(@bulletRecycler);
       $canvas->coords( $bullet, $x - 2, $y - 18, $x + 2, $y - 28 );
   }    # if
   else {    # if not possible, create new bullet
       $bullet =
         $canv->createRectangle( $x - 2, $y - 18, $x + 2, $y - 28,
           -fill => 'white' );
   }    # else

   return $bullet;
}    # GetBullet

# ------------------------------------------------------------
sub GetNewMonster {
   my ( $canvas, $x, $y ) = @_;

   my $monster = $canvas->createOval(
       $x - 10, $y - 10, $x + 10, $y + 20,
       -fill    => '#ffff00',
       -outline => '#ffffff'
   );
   $monsters{$monster} = 1;
   $monsterCount++;
   $canvas->after( 50, [ \&MoveMonster, $canvas, $monster, $x, $y, 10 ] );

   return $monster;
}    # GetNewMonster

# ------------------------------------------------------------
sub MoveShip {
   my ( $canv, $x1, $y1, $ship ) = @_;
   $x1 = $canv->canvasx($x1);

   # move ship to left or to right
   $canv->move( $ship, $x1 - $x, 0 );
   $x = $x1;
}    # MoveShip

# ------------------------------------------------------------
sub Fire {
   my ( $canv, $x1, $y1, $ship ) = @_;
   $x1 = $canv->canvasx($x1);

   # start firing bullet
   my $bullet = &GetBullet( $canvas, $x, $y );
   $canv->after( 10, [ \&FireUp, $bullet, $x1, $y - 18 ] );
}    # Fire

# ------------------------------------------------------------
sub FireUp {
   my ( $tag, $x2, $y2 ) = @_;
   $canvas->move( $tag, 0, -8 );

   my @items = $canvas->find( "overlapping", $x2 - 2, $y2, $x2 + 2, $y2 - 10 );
   local $" = "|";
   print "Found: @items\n" if scalar @items > 1;
   foreach (@items) {
       if ( exists $monsters{$_} ) {

           #    print "Hit Monster $_\n";
           $monsterCount--;

           # add monster to recycler
           push( @monsterRecycler, $_ );
           $canvas->coords( $_, 1, 1001, 21, 1021 ); # very dirty
       }    # if
   }    # foreach

   if ( $monsterCount <= 0 ) {
       &RestartDialog();
   }    # if

   if ( $y2 < 10 ) {    # if bullet out of screen

       # move bullet to recycler
       push( @bulletRecycler, $tag );
       $canvas->coords( $tag, 1, 1, 5, 11 );    # dirty, I know
   }    # if

   else {    # continue moving bullet up
       $canvas->after( 10, [ \&FireUp, $tag, $x2, $y2 - 8 ] );
   }    # else
}    # FireUp

# ------------------------------------------------------------
sub MoveMonster {
   my ( $canvas, $monster, $x, $y, $direction ) = @_;
   my $down = 0;

   if ( $direction < 0 and $x < 20 ) {
       $direction = -$direction;
       $down      = 10;
       $y += 10;
   }    # if
   elsif ( $direction > 0 and $x > 620 ) {
       $direction = -$direction;
       $down      = 10;
       $y += 10;
   }    # elsif
   else {
       $x += $direction;
   }    # else
   $canvas->move( $monster, $direction, $down );

   $canvas->after( 50,
       [ \&MoveMonster, $canvas, $monster, $x, $y, $direction ] );
}    # MoveMonster

# ------------------------------------------------------------
sub RestartDialog {
   my $dialog = $mw->Dialog(
       -text           => "Gewonnen\nNoch mal?",
       -bitmap         => 'question',
       -title          => 'Programm beenden',
       -default_button => 'Yes',
       -buttons        => [qw/Ja Nein/],
   );

   my $answer = $dialog->Show();    # and display dialog
   if ( lc($answer) eq 'ja' ) {
       exec($0);                    # dirty, i know
   }    # if
   else {
       exit;
   }    # else

}    # RestartDialog

# ------------------------------------------------------------
\n\n

<!--EDIT|Strat|1080762057-->
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/
ptk
 2004-04-01 13:39
#41857 #41857
User since
2003-11-28
3645 Artikel
ModeratorIn
[default_avatar]
Nett :-) Mir ist aufgefallen, dass das Geschoss oben links zu sehen ist, wenn es gerade nicht benoetigt wird. Eigentlich koenntest du es ausserhalb des sichtbaren Bereichs verschieben (Minus-Werte sind erlaubt) oder $c->itemconfigure(..., -state => "hidden") verwenden.
esskar
 2004-04-01 13:44
#41858 #41858
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
wenn ich dass ding unter XP, perl 5.8.2 und Tk 804 starte, sehe ich nur schwarz!
[E|B]
 2004-04-01 14:20
#41859 #41859
User since
2003-08-08
2561 Artikel
HausmeisterIn
[Homepage] [default_avatar]
[quote=esskar,01.04.2004, 11:44]wenn ich dass ding unter XP, perl 5.8.2 und Tk 804 starte, sehe ich nur schwarz![/quote]
Ich nicht! Bei mir funktioniert es.

@strat

Wie wäre es mit verschiedenen Levels? 8)
Gruß, Erik!

s))91\&\/\^z->sub{}\(\@new\)=>69\&\/\^z->sub{}\(\@new\)=>124\&\/\^z->sub{}\(\@new\)=>);
$_.=qq~66\&\/\^z->sub{}\(\@new\)=>93~;for(@_=split(/\&\/\^z->sub{}\(\@new\)=>/)){print chr;}

It's not a bug, it's a feature! - [CGI-World.de]
Crian
 2004-04-01 15:12
#41860 #41860
User since
2003-08-04
5870 Artikel
ModeratorIn
[Homepage]
user image
nett :)

Bei mir läuft es auch (Win 2000)
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite
Strat
 2004-04-01 22:38
#41861 #41861
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
ich habe keine Lust mehr, das weiter zu entwicklen; wenn ihr lust habt, tut euch keinen zwang an

@esskar: schwarz passt schon, aber eigentlich sollte da noch was rotes und gelbes dazukommen... wird wenigstens der mauszeuger zu einem kleinen kreuz?
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/
<< >> 6 Einträge, 1 Seite



View all threads created 2004-03-31 23:39.