Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial tester code that I worked on yesterday #1

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/FormatFix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: FormatFix

on:
push:
branches: [ format-check ]
branches: [ release-candidate ]

jobs:

Expand Down
18 changes: 18 additions & 0 deletions Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Block here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Block extends Tile
{
/**
* Act - do whatever the Block wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
}
145 changes: 145 additions & 0 deletions ImgScroll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import greenfoot.*;
/**
* CLASS: ImgScroll (subclass of Object)<br>
* AUTHOR: danpost (greenfoot.org username)<br>
* VERSION DATE: April 1, 2015<br>
* <br>
* DESCRIPTION: a scrolling engine that scrolls the world given to the limits of the scrolling
* background image given or to the dimensions given where the given background image is tiled,
* if needed, to fill the scrolling area; keep in mind that the smaller the world window is, the
* less lag you are likely to get (increasing the world size will cause an exponential increase
* in possible lag); this class is designed to keep lag at a minimum by breaking the scrolling
* background image into smaller parts so that the entire scrolling background image is not drawn
* every time scrolling occurs;<br>
* <br>
* There are two ways to create a scroller with this class:<br>
* (1) Use the two-parameter constructor to limit scrolling to the given background image dimensions;<br>
* (2) Use the four-parameter one to give the dimensions of the scrolling area and have the given
* image tiled, if needed, to fill that area;
*/

public class ImgScroll
{
private World scrollWorld; // the world the background image scrolls in
private GreenfootImage[][] scrollImages; // the drawing panels (background image broken down)
private int scrollWidth, scrollHeight; // the dimensions of the scrolling area
private int worldWidth, worldHeight; // the dimensions of the world window
private int xScrAmt, yScrAmt; // the overall horizontal and vertical scrolled amounts

/**
* calls the main constructor of the class using the dimensions of the given image for
* the dimensions of the scrolling area
*
* @param scrWorld the world the given background image is to scroll in
* @param scrImage the scrolling background image to be used in the given world
*/
public ImgScroll(World scrWorld, GreenfootImage scrImage)
{
this(scrWorld, scrImage, scrImage.getWidth(), scrImage.getHeight());
}

/**
* this main constructor sets field values and initial world background image
*
* @param scrWorld the world the given background image is to scroll in
* @param repImage the image that is tiled to create the scrolling background image of the given world
* @param wide the horizontal dimension of the scrolling area
* @param high the vertical dimension of the scrolling area
*/
public ImgScroll(World scrWorld, GreenfootImage repImage, int wide, int high)
{
// the field values
scrollWorld = scrWorld;
worldWidth = scrWorld.getWidth();
worldHeight = scrWorld.getHeight();
scrollWidth = wide;
scrollHeight = high;
// the scrolling image
if (repImage == null) repImage = scrWorld.getBackground();
GreenfootImage scrImage = new GreenfootImage(wide, high);
scrImage.setColor(Color.WHITE);
scrImage.fill(); // ensures no transparent pixels in background
for (int i=0; i<wide; i+=repImage.getWidth())
for (int j=0; j<high; j+=repImage.getHeight())
scrImage.drawImage(repImage, i, j);
// the drawing panels (to help reduce lag by reducing the number of pixels drawn when scrolling)
int x = 1+scrollWidth/worldWidth; // number of panels across
int y = 1+scrollHeight/worldHeight; // number of panels down
scrollImages = new GreenfootImage[y][x]; // creates the array
for (int j=0; j<y; j++) for (int i=0; i<x; i++) // fills the array
{
scrollImages[j][i] = new GreenfootImage(worldWidth, worldHeight);
scrollImages[j][i].drawImage(scrImage, -i*worldWidth, -j*worldHeight);
}
scrollBackground(); // sets initial world background image
}

/** sets the world background image determined by the current scroll values */
private void scrollBackground()
{
int x = (-xScrAmt)/worldWidth; // panel x index
int y = (-yScrAmt)/worldHeight; // panel y index
int dx = -((-xScrAmt)%worldWidth); // drawing x offset
int dy = -((-yScrAmt)%worldHeight); // drawing y offset
GreenfootImage bg = scrollWorld.getBackground(); // gets local reference to world background
bg.drawImage(scrollImages[y][x], dx, dy); // draw top-left image
if (dx != 0) // draw top-right image if needed
bg.drawImage(scrollImages[y][x+1], dx+worldWidth, dy);
if (dy != 0) // draw bottom-left image if needed
bg.drawImage(scrollImages[y+1][x], dx, dy+worldHeight);
if (dx != 0 && dy != 0) // draw bottom-right image if needed
bg.drawImage(scrollImages[y+1][x+1], dx+worldWidth, dy+worldHeight);
}

/**
* performs limited scrolling using the given changes in scroll values
*
* @param dx the amount to scroll horizontally (positive values scroll left moving view toward the right)
* @param dy the amount to scroll vertically (positive values scroll up moving view toward the bottom)
*/
public void scroll(int dx, int dy)
{
// limit change values
if (dx > 0 && xScrAmt+dx > 0) dx = -xScrAmt;
if (dx < 0 && xScrAmt+dx <= worldWidth-scrollWidth)
dx = (worldWidth-scrollWidth)-xScrAmt;
if (dy > 0 && yScrAmt+dy > 0) dy = -yScrAmt;
if (dy < 0 && yScrAmt+dy <= worldHeight-scrollHeight)
dy = (worldHeight-scrollHeight)-yScrAmt;
// change scrolling values
xScrAmt += dx;
yScrAmt += dy;
// update world background
scrollBackground();
// keep actors in place with relation to background image
for (Object obj : scrollWorld.getObjects(null))
{
Actor actor = (Actor) obj;
actor.setLocation(actor.getX()+dx, actor.getY()+dy);
}
}

/** returns the overall amount scrolled horizontally */
public int getScrolledX()
{
return xScrAmt;
}

/** returns the overall amount scrolled vertically */
public int getScrolledY()
{
return yScrAmt;
}

/** returns the width of the scrolling area */
public int getScrollWidth()
{
return scrollWidth;
}

/** returns the height of the scrolling area */
public int getScrollHeight()
{
return scrollHeight;
}
}
43 changes: 43 additions & 0 deletions MyWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 MyWorld extends World
{
private ImgScroll scroll;
private Player player;

/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1280, 720, 1, false);
addObject(player = new Player(), 100, 622);
scroll = new ImgScroll(this, new GreenfootImage("2dPixelForestBackground.png"), 2560, 720);
for (int j=0; j<scroll.getScrollHeight()-100; j+=300){
for (int i=0; i<scroll.getScrollWidth(); i+=106){
addObject(new Block(), 0+i, 700);
}
}
}


public void act(){
scroll.scroll(getWidth()/2-player.getX(), getHeight()/2-player.getY());
}

public void spawnTerrain(int[][] identifier){
for (int i = 0; i < identifier.length; i++){
for (int j = 0; j < identifier[i].length; j++){

}
}
}
}
44 changes: 44 additions & 0 deletions Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
private static int speed;
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("D")){
setLocation(getX()+2, getY());
speed = 2;
} if (Greenfoot.isKeyDown("A")){
setLocation(getX()-2, getY());
speed = -2;
} if (Greenfoot.isKeyDown("Space") && isTouching(Block.class)){
for (int i = 0; i < 20; i+=2){
setLocation(getX(), getY()-i);
}
} if (!isTouching(Block.class)){
setLocation(getX(), getY()+2);
} if (getX() < 0){
setLocation(0,getY());
} if (getX() > 2560){
setLocation(2560, getY());
} if (getY() < 0){
setLocation(getX(), 0);
} if (getY() > 720){
setLocation(getX(), 720);
}
}

public static int getSpeed(){
return speed;
}
}
12 changes: 12 additions & 0 deletions README.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
------------------------------------------------------------------------
This is the project README file. Here, you should describe your project.
Tell the reader (someone who does not know anything about this project)
all they need to know. The comments should usually include at least:
------------------------------------------------------------------------

PROJECT TITLE:
PURPOSE OF PROJECT:
VERSION or DATE:
HOW TO START THIS PROJECT:
AUTHORS:
USER INSTRUCTIONS:
19 changes: 19 additions & 0 deletions Tile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Tile here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Tile extends Actor
{
/**
* Act - do whatever the Tile wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
}
Loading