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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class Scrolling extends World { /** * Constructor for objects of class MyWorld. * */ GreenfootImage background; int currentX; int currentY; public Scrolling() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 900 , 300 , 1 , false ); background = new GreenfootImage( "galaxy-stars01.jpg" ); getBackground().drawImage(background, 0 , 0 ); } public void act() { drawBackground(); checkKeys(); } public void checkKeys() { if (Greenfoot.isKeyDown( "left" )) changeCurrentXY(- 1 , 0 ); if (Greenfoot.isKeyDown( "right" )) changeCurrentXY( 1 , 0 ); if (Greenfoot.isKeyDown( "up" )) changeCurrentXY( 0 , 1 ); if (Greenfoot.isKeyDown( "down" )) changeCurrentXY( 0 , - 1 ); } public void drawBackground() { getBackground().drawImage(background, -currentX, -currentY); } public void changeCurrentXY( int changeX, int changeY) { currentX += changeX; currentY -= changeY; } } |

