Aus Das deutschsprachige Scratch-Wiki

Dieser Artikel beschreibt Schritt für Schritt, wie man einen Scrolling Platformer erstellt.

Coding the Player

Um die Bewegung des Players zu codieren, müssen die folgenden Skripte erstellt werden. Alle Skripte in diesem Abschnitt befinden sich im Sprite "Player".


Information icon.png Information: Variablen in Großbuchstaben (z. B. "SCROLL X" und "SCROLL Y") sind globale Variablen, und diese Kleinbuchstaben (z. B. "x" und "y") werden mit der Option "Nur für dieses Sprite" ausgewählt.


When green flag clicked
broadcast (Green Flag v) and wait
broadcast (Play Game v) and wait

When I receive [Play game v]
forever
    broadcast (Reset v) and wait
    broadcast (Set Up v) and wait
    Game On::custom
    repeat until <(EXIT)>()>
        broadcast (Tick v)
    end
    if <(EXIT)=(win)> then
        Win::custom
    else
        Die::custom
    end

define Position
go to x:((x)-(SCROLL X)) y: ((y) - (SCROLL Y)

define Game On//Run without screen refresh
point in direction (90)
set size to (100)%
clear graphic effects
set [in air v] to (0)
set rotation style [left-right v]
set [sy v] to (0)
set [x v] to (0)
set [y v] to (0)
set [EXIT v] to () // Nothing
set [SCROLLL X v] to (0)
set [SCROLLL Y v] to (0)
show

When I receive [Tick v]
broadcast (Ready v)
Tick <key (Right arrow v) pressed?> <key (Up arrow v) pressed?> <key (Left arrow v) pressed?>::custom

define Tick <right> <up> <left>
if <left> then
    Change player X by [-8]::custom
end
if <right> then
    Change player X by [8]::custom
end
if <<up> and <(in air)< (4)>> then
    set [sy v] to (16)
end
change [sy v] by (-1)
Change player Y by (sy)::custom
Test dying::custom
if <(SCROLL X)<(0)> then
    set [SCROLL X v] to (0)
end
if <(SCROLL Y)<(0)> then
    set [SCROLL Y v] to (0)
end
change [SCROLL Y v] by (round(((y)-(SCROLL Y))/(10)))
change [SCROLL X v] by (round(((x)-(SCROLL X))/(5)))
Position::custom
if <(y position) < (-180)> then
    set [EXIT v] to [die]
end

define Change player X by (sx)
if <(sx::custom)<(0)> then
    point in direction (-90)
else
    point in direction (90)
end
change [x v] by (sx::csutom)
Position::custom
if <touching (platforms v) ?> then
    repeat (12)
        change [y v] by (1)
        Position::custom
        if <not<touching (platforms v) ?>> then
            stop [this script v]
        end
    end
    change [y v] by (-12)
    repeat until <not<touching (platforms v)?>>
        if <(sx)>(0)> then
            change [x v] by (-1)
        else
            change [x v] by (1)
        end
        Position::custom
    end
end

define Change player Y by (sy)
change [y v] by (sy::custom)
change [in air v] by (1)
Position::custom
repeat until <not<touching (platforms v)?>>
    if <(sy::custom)>(0)> then
        change [y v] by (-1)
    else
        change [y v] by (1)
        set [in air v] to (0)
    end
    set [sy v] to (0)
    Position::custom
end

define Test dying
if <touching (Danger v)?> then
    set [EXIT v] to (die)
end

define Die
set [EXIT v] to ()//nothing
repeat (4)
    hide
    wait (0.1) seconds
    show
end
wait (0.5) seconds

define Win
set rotation style [don't rotate v]
repeat (50)
    point towards (Portal v)
    turn cw (65) degrees
    move ((distance to (Portal v))/(2)) steps
    change size by (-1)
    change [ghost v] effect by (2)
end
hide
change [LEVEL v] by (1)
wait (1) seconds
clear graphic effects
set rotation style [left-right v]

Coding the Platforms Sprite

Sobald der Code des Spielers abgeschlossen ist, muss Code für das Bodensprite (hier als "Plattformen" bezeichnet) hinzugefügt werden, um einen Bildlaufeffekt zu erzielen. Die folgenden Skripte befinden sich im Sprite "Plattformen".

when I receive [Tick v]
Position ((x)-(SCROLL X)) ((y) - (SCROLL Y)
when I receive [Green flag v]
show

define Position (x) (y)
go to x: (x::custom) y: (y::custom)
if <<(x::custom)= (x position)> and <(y::custom) = (y position)>> then
    show
else
    hide
end

When I receive [Setup v]
set [x v] to (0)
set [y v] to (0)
if <(LEVEL)=(1)> then
    switch costume to (Level 1 1)
    Clone (450) (0) // This clones a new level. This needs to be done as many times as the number of levels. In this case, 3 is used as an example.
    Clone (450) (0)
    Clone (450) (0)
else
    ...::grey // If there is another scene then it should be cloned here using another if else block.
end

define Clone (x) (y)
create clone of (myself v)
change [x v] by (x::custom)
change [y v] by (y::custom)
next costume

Coding the Danger Sprite

Um das Gefahrensprite zu codieren, dupliziere einfach das Sprite "Plattformen" und ersetze alle Level durch nur die gefährlichen Elemente, die den Spieler verletzen würden.


Information icon.png Information: Man muss sicherstellen, dass man das Gefahrensprite "Gefahr" nennt, da das obige Skript dem Spieler bereits Sinn für ein Sprite mit diesem Namen macht.


when I receive [Tick v]
position ((x)-(SCROLL X))((y)-(SCROLL Y))::custom

when I receive [Green flag v]
show

when I receive [Reset v]
delete this clone

define Position (x) (y)
go to x: (x::custom) y: (y::custom)
if <<(x::custom)= (x position)> and <(y::custom) = (y position)>> then
    show
else
    hide
end

when I receive [Setup v]
set [x v] to (0)
set [y v] to (0)
if <(LEVEL)=(1)> then
    switch costume to (Level 1 1)
    Clone (450) (0) // This clones a new level, do this as many times as the number of levels, in this case 3 is used as an example.
    Clone (450) (0)
    Clone (450) (0)
else
    ...::grey // If there is another scene then it should be cloned here using another if else block.
end

define Clone (x) (y)
create clone of (myself v)
change [x v] by (x::custom)
change [y v] by (y::custom)
next costume

Coding the Portal

Jetzt, da alles gemacht ist, muss ein Weg zum nächsten Level gemacht werden.

When I receive [Tick v]
Position ((x)-(SCROLL X)) (((y) - (SCROLL Y)) + ((([sin v] of (timer)) *(100))*(20)))::custom
if <<(costume [number v]) = (2)> and <touching (player v)?>> then
    set [EXIT v] to [win]
end

When I receive [Setup v]
set [x v] to (0)
set [y v] to (0)
if <(LEVEL)=(1)> then
    switch costume to (Level 1 1)
    Position Portal at (450) (180)::custom
else
    ...::grey // If there is another scene than the portal needs to be positioned here using another if else block.
end

define Position Portal at (x) (y)
set [x v] to (x::custom)
set [y v] to (y::custom)

define Position (x) (y)
go to x: (x::custom) y: (y::custom)
if <<(x::custom)= (x position)> and <(y::custom) = (y position)>> then
    show
else
    hide
end

Adding Collectibles

Sammlerstücke können dem Spiel hinzugefügt werden, um es interessanter zu machen. Sie sind Objekte, die gesammelt werden müssen, um ein bestimmtes Ziel zu erreichen. Der Code, um sie zu erstellen, ist unten gezeigt:

When I receive [Tick v]
Position ((x)-(SCROLL X)) (((y) - (SCROLL Y)) + ((([sin v] of (timer)) *(300))*(5)))::custom
if <touching (player v)?> then
    change [COLLECTED v] by (1)
    if <(COLLECTED)=(COLLECTED MAX)> then
        broadcast (Open portal v)
        start sound (All coins collected v)
    else
        start sound (Collect v)
    end
    delete this clone
end

When I receive [Green flag v]
show

when [m v] key pressed
set [MOUSE v] to (join ((mouse x) + (SCROLL X))(join [,]((mouse y) + (SCROLL Y))))

When I receive [Reset v]
delete this clone

define Position (x) (y)
go to x: (x::custom) y: (y::custom)
if <<(x::custom)= (x position)> and <(y::custom) = (y position)>> then
    show
else
    hide
end

When I receive [Setup v]
set [x v] to (0)
set [y v] to (0)
if <(LEVEL)=(1)> then
    Clone (450) (180)::custom
else
    ...::grey // If there is another scene than coins need to be cloned here using another if else block.
end

define Clone (x) (y)
set [x v] to (x::custom)
set [y v] to (y::custom)
create clone of (myself v)
change [COLLECTED MAX v] by (1)

See Also