This site requires JavaScript, please enable it in your browser!
Greenfoot back
intotherain
intotherain wrote ...

2015/11/24

Create a maze using a one-pixel actor subclass

intotherain intotherain

2015/11/24

#
Hi! Scenario name: maze World subclass: Earth Actor subclass: Wall Using MS Paint, I have created a 1 pixel by 1 pixel image that I have filled with color. File name is wall.png and it is store in the images folder already. It is the icon of the Wall actor subclass. I need to create a maze. I have already done so using two methods. In the first method, I created different segments using MS Paint (e.g. wall1.png, wall2.png, etc) and then arranged these into a maze in the world. The second is by using for-loops to use the 1 pixel image. Now I want to create a new maze simply by placing a Wall object into the world, like, say, addObject(new Wall(), 50, 50); then changing the size of this pixel to, say, (100, 1) to create a horizontal wall that is 1 pixel thin. I would then add another part of the maze by, say, addObject(new Wall(), 200, 200); then changing the scale vertically to create a vertical wall. I have tried on my own and from the answers given here like GreenfootImage x = getImage(); x.scale(100, 1); setImage(x); does not work for me. Please help. Thank you.
schwonko schwonko

2015/11/24

#
I tried and it works. Before you have to place the one-pixel-actor on the background, as usual. (But this 1 pixel image is nearly invisible). Or you must load directly from file , by using method: public GreenfootImage(java.lang.String filename) If you prepare nothing, then : GreenfootImage x = getImage(); ... will get an empty image. Proposal: Instead scaling a one pixel image, why not use the draw methods ?
danpost danpost

2015/11/24

#
@schwonko, you cannot use collision methods or easily detect collision with walls that are drawn onto the background image. It is true that the image can be created programmatically without much difficulty; but it is not a problem using an image file as described above. The code block given should create a line out of the point. The only two things I can immediately think of that might be an issue are (1) the image from the file is not actually a (1, 1) image; you can test this with the following:
System.out.println("image width: "+x.getWidth()+"/nimage height: "+x.getHeight());
The output should be:
image width: 1
image height: 1
(2) the other thing is the placement of the code given (or the possible misplacement thereof); you would need to post the entire Wall class for checking this.
intotherain intotherain

2015/11/25

#
Thanks for the replies schwonko and danpost. @danpost, the output of the System.out is indeed 1 and 1. (by the way, the new line didn't work for me. I'm using 2.4.2) So I still haven't solved this. If in world Earth, I do this inside the Earth constructor: addObject(new Wall(), 50, 50); addObject(new Wall(), 200, 200); how do I make the pixel at (50, 50) into a horizontal segment of length 100? How do I make the pixel at (200, 200) into a vertical segment of length 100? Thanks in advance!
danpost danpost

2015/11/25

#
You need to place the Wall actors created in a variable so you can access their images and scale them:
Actor wall = new Wall();
addObject(wall, 50, 50);
wall.getImage().scale(100, 1);
wall = new Wall();
addObject(wall, 200, 200);
wall.getImage().scale(1, 100);
BTW, it was not because you were using 2.4.2 that the new line did not work. I accidentally coded a forward slash instead of a backward one. (FYI, I am still using 2.3.0)
intotherain intotherain

2015/11/25

#
Thanks so much, danpost! I really appreciate how you help noobs like me around here.
schwonko schwonko

2015/11/25

#
remark: I was not talking about drawing to the background image, but inside an actor method (e.g. as part of the constructor) to the actor itsself.
You need to login to post a reply.