This site requires JavaScript, please enable it in your browser!
Greenfoot_back
Back to discussions
No_avatar_thumb
Tym5 wrote ...

2011/12/15

Need help with loops!

No_avatar Tym5

2011/12/15

I have a project due soon and I am trying to figure out how once a key (2) is pressed once, lines continuously draw themselves. can anyone lend any advice? I am not looking for an answer because I enjoy problem solving, just a push in the right direction. Thanks!
Gollum dordor

2011/12/15

I don't know exactly how to do that, but I think that you need a counter which start when the key pressed, and according to the counter, the lines draw pixels in the right places. I think you need something like that:
No_avatar Tym5

2011/12/15

if (k.charAt(0) == '2'){ //This will put lines randomly onto the screen GreenfootImage b = this.getBackground(); java.awt.Color c = new java.awt.Color(Greenfoot.getRandomNumber(51),Greenfoot.getRandomNumber(102), Greenfoot.getRandomNumber(255)); b.drawLine(Greenfoot.getRandomNumber(400),Greenfoot.getRandomNumber(290), Greenfoot.getRandomNumber(300),Greenfoot.getRandomNumber(100)); int x=Greenfoot.getRandomNumber(this.getWidth()); int y=Greenfoot.getRandomNumber(this.getHeight()); int x2=Greenfoot.getRandomNumber(this.getWidth()); int y2=Greenfoot.getRandomNumber(this.getHeight()); int getAlpha; background.clear(); background.setColor(c); background.drawLine(x,y,x2,y2); } This is what it looks like so far. I need the lines to be added onto the world until another button is pressed. If I hold the button down it does this, but as soon as I let go it stops, if I can figure that out then I can finish this project
Fractal_r3800c0s800f10000t0z80x0y0_thumb danpost

2011/12/15

Check the discussion at http://www.greenfoot.org/topics/600. (Copy and paste the address as link does not want to work for me)
No_avatar Tym5

2011/12/15

I Tried to do that actually, and The lines would not appear. The code I tried out was this: public String lastKeyPressed=""; public void act() { java.lang.String k = Greenfoot.getKey(); GreenfootImage background = this.getBackground(); String lastKeyPressed=""; if (k == null) { // no key press } else{ if (k.charAt(0) == 'c'){ //This will clear the screen to the color white background.setColor(Color.white); background.fill(); } if (k.charAt(0) == 'r'){ //This will set the background to a random color GreenfootImage pic=new GreenfootImage(600,400); pic.setColor( new Color(Greenfoot.getRandomNumber(255+1),Greenfoot.getRandomNumber(255+1), Greenfoot.getRandomNumber(255+1))); pic.fill(); setBackground(pic); } String myKey = Greenfoot.getKey(); if (myKey != null) lastKeyPressed = myKey; if ("2".equals(lastKeyPressed)) { Color a; int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); int x = Greenfoot.getRandomNumber(this.getWidth()); int y = Greenfoot.getRandomNumber(this.getHeight()); int x1 = Greenfoot.getRandomNumber(this.getWidth() - x); int y2 = Greenfoot.getRandomNumber(this.getHeight() - y); a = new Color(r, g, b, 255); background.clear(); background.setColor(a); background.drawLine(x, y, x1, y2); } } } } Basically nothing happened when I did this
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2011/12/15

My, WHAT A MESS! The first two statement are OK! The statements:
with the first statement are used to keep track of the key that was pressed (and released) last. These two statements are somewhat independent of anything else within the class. The 'public String lastKeyPressed = "";' at the beginning is the variable we will use in determining whether we are drawing lines or not. You can use 'lastKeyPressed' to check for the String "c", clear the background, and set 'lastKeyPressed' to "" (so on the next act, it will not clear it again), then check it for "r", fill the background with a random color, and set 'lastKeyPressed' to "" (again, so it does not repeat the fill); check it for "l", randomly draw a line onto the background, do NOT reset 'lastKeyPressed' and each act will draw another line (until a different key is pressed).
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2011/12/15

By the way, those two statements listed above should be executed first within your act method, with the checks for different keystrokes afterwards. Put everything in order -- it should sound logical as you read through the code. Remove the code starting at the third line in act (String lastKeyPressed = "";) to the else statement.
No_avatar Tym5

2011/12/15

if ("2".equals(lastKeyPressed)) { String myKey = Greenfoot.getKey(); if (myKey != null) lastKeyPressed = myKey; else{ Color a; int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); int x = Greenfoot.getRandomNumber(this.getWidth()); int y = Greenfoot.getRandomNumber(this.getHeight()); int x1 = Greenfoot.getRandomNumber(this.getWidth() - x); int y2 = Greenfoot.getRandomNumber(this.getHeight() - y); a = new Color(r, g, b, 255); background.clear(); background.setColor(a); background.drawLine(x, y, x1, y2); } } This is what it looks like now (Sorry if I repeatedly post mistakes, I have never programmed before) and I am still lost as to where to put things.
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2011/12/16

No_avatar Tym5

2011/12/16

I actually figured this out not to long ago, thank you so much! Now I just have to figure out how to add random letters (a-z) and make the F key a "toggle" so it fills whatever shape is being produced
You need to login to post a reply