From 9e35cc9ebbdbe868bd1082e564ab541d74694bbb Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 15 Sep 2026 09:57:01 +0200 Subject: [PATCH] Player Movement: A - links D - rechts Leertaste - Springen --- SMIMS-Game/Player.class | Bin 1101 -> 1563 bytes SMIMS-Game/Player.ctxt | 9 ++-- SMIMS-Game/Player.java | 90 +++++++++++++++++++++---------- SMIMS-Game/PlayerTestWorld.class | Bin 0 -> 574 bytes SMIMS-Game/PlayerTestWorld.ctxt | 10 ++++ SMIMS-Game/PlayerTestWorld.java | 33 ++++++++++++ SMIMS-Game/project.greenfoot | 45 ++++++++++------ 7 files changed, 140 insertions(+), 47 deletions(-) create mode 100644 SMIMS-Game/PlayerTestWorld.class create mode 100644 SMIMS-Game/PlayerTestWorld.ctxt create mode 100644 SMIMS-Game/PlayerTestWorld.java diff --git a/SMIMS-Game/Player.class b/SMIMS-Game/Player.class index 53822cc5902166bc4c25a83757c43afe637e31e7..ff9a79d8785e72a9b61bb93f2b512732c93c11e8 100644 GIT binary patch literal 1563 zcmZWpTT|Oc7(Ma@8M6*xZYBhhv^3ZX1sZZ`F_3a;z>NuMpcFcj8HBwUfsq(l!0=Nt z{Q-UM47i;>^f3?pPwn)4vH`apjdoZ2?Vj_U^X=--zkmM&zyfxXh#+brrVvM)fzFDr zwY%?m!Q?_I@O%SpvySTo^9G{X+*UghXg85m=zwDEZPmWeTokXJXk9j76lBF6g-!{p z!_(uFjlkD#B{+~kw?bM1X2rM9oZv#{dKLO)E_J5;z$w|)4S|x#fIvDktz&Xr*1!Ci1kmAzOCO zDa_-gfecyJU0rYk?bmcEU>@sxha4CfZB22hYS(J{T=7;|SkAnZdrF!`g(WQ0Tmlx3 z?TR*VFWb8Q#)vS=jjym`VpX9avNE^8|6WMjh`6e+%dtpjmb^0IJByC1-<%%rX@Aq+ zt8ykSK2*1C-;wX;WGpyvYRs$HI9s$KYKy$oMqrnY*6fq!y7o$VCN{kj)~|G6`&^~{ zRlB<1ASbV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 -1,10 +1,11 @@ #BlueJ class context comment0.target=Player -comment0.text=\r\n\ Write\ a\ description\ of\ class\ Player\ here.\r\n\ \r\n\ @author\ (your\ name)\ \r\n\ @version\ (a\ version\ number\ or\ a\ date)\r\n comment1.params= comment1.target=void\ act() comment2.params= -comment2.target=void\ player_movement() +comment2.target=void\ moveLeftAndRight() comment3.params= -comment3.target=boolean\ onGround() -numComments=4 +comment3.target=void\ jump() +comment4.params= +comment4.target=void\ fall() +numComments=5 diff --git a/SMIMS-Game/Player.java b/SMIMS-Game/Player.java index f3a9eaf..dbe19ac 100644 --- a/SMIMS-Game/Player.java +++ b/SMIMS-Game/Player.java @@ -1,38 +1,74 @@ -import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) +import greenfoot.*; -/** - * Write a description of class Player here. - * - * @author (your name) - * @version (a version number or a date) - */ public class Player extends Actor { - int velocityY = 0; - int speed = 5; + private int speed = 4; + private int jumpStrength = 12; + private int gravity = 1; + + private int verticalSpeed = 0; + private boolean onGround = false; + public void act() { - player_movement(); + moveLeftAndRight(); + jump(); + fall(); } - - public void player_movement(){ - if (Greenfoot.isKeyDown("D")){ - setLocation(getX()+speed, getY()); + + // Bewegung nach links und rechts + private void moveLeftAndRight() + { + if (Greenfoot.isKeyDown("a")) + { + setLocation(getX() - speed, getY()); } - if (Greenfoot.isKeyDown("A")){ - setLocation(getX()-speed, getY()); + + if (Greenfoot.isKeyDown("d")) + { + setLocation(getX() + speed, getY()); } } - - public boolean onGround(){ - int height = getImage().getHeight(); - - Actor floor = getOneObjectAtOffset( - 0, - height / 2 + 1, - Ground.class - ); - - return floor != null; + + // Springen + private void jump() + { + if (Greenfoot.isKeyDown("space") && onGround) + { + verticalSpeed = -jumpStrength; + onGround = false; + } + } + + // Schwerkraft und Boden-Kollision + private void fall() + { + verticalSpeed += gravity; + + int newY = getY() + verticalSpeed; + + // Prüfen, ob wir auf Ground landen + setLocation(getX(), newY); + + Actor ground = getOneIntersectingObject(Ground.class); + + if (ground != null) + { + // Wenn wir fallen, stehen wir auf dem Ground + if (verticalSpeed >= 0) + { + int groundTop = ground.getY() - ground.getImage().getHeight() / 2; + int characterHalfHeight = getImage().getHeight() / 2; + + setLocation(getX(), groundTop - characterHalfHeight); + + verticalSpeed = 0; + onGround = true; + } + } + else + { + onGround = false; + } } } diff --git a/SMIMS-Game/PlayerTestWorld.class b/SMIMS-Game/PlayerTestWorld.class new file mode 100644 index 0000000000000000000000000000000000000000..99be99adc32fd8c1bb24f698083891ffd1526545 GIT binary patch literal 574 zcmZ8d+b#o96kVqsW{ja7t-8~aP$hf-ksu*W#w8)eO8kXi@j^o41N@6$ z5bI2vNWARXd(Bywy}y3mKLPAw$wmrk6NZBU3<_j!l$5ROD2g{PBGn882Digj81Jwt zSF6?4nhgs>CTs`8a7Z|9`cG1w%TC-Um~AE7zLEmz@@mb*D5-}k>b3$KBgkqaM~jR( z$RRH<U`VM`u&@9T`<*PBGf zyr|(fYrYEg+y{;LHtYzv-oLb4l)Xu^30Ph)rN-LHQX1mc!SECD-2gWzQX;$IH8H7%DO8o7vF^bYzhno%2}AB**v8e du#942SL{jl?JWM8;|syrVP8yEDkVV{e*uiSXxRV& literal 0 HcmV?d00001 diff --git a/SMIMS-Game/PlayerTestWorld.ctxt b/SMIMS-Game/PlayerTestWorld.ctxt new file mode 100644 index 0000000..641bfe3 --- /dev/null +++ b/SMIMS-Game/PlayerTestWorld.ctxt @@ -0,0 +1,10 @@ +#BlueJ class context +comment0.target=PlayerTestWorld +comment0.text=\r\n\ Write\ a\ description\ of\ class\ PlayerTestWorld\ here.\r\n\ \r\n\ @author\ (your\ name)\ \r\n\ @version\ (a\ version\ number\ or\ a\ date)\r\n +comment1.params= +comment1.target=PlayerTestWorld() +comment1.text=\r\n\ Constructor\ for\ objects\ of\ class\ PlayerTestWorld.\r\n\ \r\n +comment2.params= +comment2.target=void\ prepare() +comment2.text=\r\n\ Prepare\ the\ world\ for\ the\ start\ of\ the\ program.\r\n\ That\ is\:\ create\ the\ initial\ objects\ and\ add\ them\ to\ the\ world.\r\n +numComments=3 diff --git a/SMIMS-Game/PlayerTestWorld.java b/SMIMS-Game/PlayerTestWorld.java new file mode 100644 index 0000000..abc6190 --- /dev/null +++ b/SMIMS-Game/PlayerTestWorld.java @@ -0,0 +1,33 @@ +import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) + +/** + * Write a description of class PlayerTestWorld here. + * + * @author (your name) + * @version (a version number or a date) + */ +public class PlayerTestWorld extends World +{ + + /** + * Constructor for objects of class PlayerTestWorld. + * + */ + public PlayerTestWorld() + { + // Create a new world with 600x400 cells with a cell size of 1x1 pixels. + super(600, 400, 1); + prepare(); + } + /** + * Prepare the world for the start of the program. + * That is: create the initial objects and add them to the world. + */ + private void prepare() + { + Ground ground = new Ground(); + addObject(ground,260,375); + Player player = new Player(); + addObject(player,264,237); + } +} diff --git a/SMIMS-Game/project.greenfoot b/SMIMS-Game/project.greenfoot index 73e93c0..f079710 100644 --- a/SMIMS-Game/project.greenfoot +++ b/SMIMS-Game/project.greenfoot @@ -1,19 +1,25 @@ #Greenfoot project file class.Ground.image=weave.jpg class.Player.image=ppl2.png -dependency1.from=MyWorld -dependency1.to=Player +dependency1.from=PlayerTestWorld +dependency1.to=Ground dependency1.type=UsesDependency -dependency2.from=Player -dependency2.to=Ground +dependency2.from=PlayerTestWorld +dependency2.to=Player dependency2.type=UsesDependency -editor.fx.0.height=0 -editor.fx.0.width=0 -editor.fx.0.x=0 -editor.fx.0.y=0 -height=736 -package.numDependencies=2 -package.numTargets=3 +dependency3.from=MyWorld +dependency3.to=Player +dependency3.type=UsesDependency +dependency4.from=Player +dependency4.to=Ground +dependency4.type=UsesDependency +editor.fx.0.height=737 +editor.fx.0.width=814 +editor.fx.0.x=360 +editor.fx.0.y=26 +height=830 +package.numDependencies=4 +package.numTargets=4 project.charset=UTF-8 publish.hasSource=false publish.locked=true @@ -43,14 +49,21 @@ target2.width=120 target2.x=0 target2.y=0 target3.height=70 -target3.name=MyWorld +target3.name=PlayerTestWorld target3.showInterface=false target3.type=ClassTarget target3.width=120 target3.x=0 target3.y=0 +target4.height=70 +target4.name=MyWorld +target4.showInterface=false +target4.type=ClassTarget +target4.width=120 +target4.x=0 +target4.y=0 version=3.1.0 -width=856 -world.lastInstantiated=MyWorld -xPosition=652 -yPosition=76 +width=1550 +world.lastInstantiated=PlayerTestWorld +xPosition=0 +yPosition=0