We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey there. I, like several other's I've seen in my search, am attempting to program the game of Battleship in Processing 3. It's been quite a trip so far, and I think I may have actually learned more doing this than I am from my Computer Science class. Anyway, at the the start of the game, I would like the user to begin with a blank screen, in which, after a preset amount of time (only two seconds or so), an image flashes in the center, perhaps three or four times, before it stays and text appears at the bottom of the screen. Here's what I have:
def intro():
global dharma
global starttime
fill(colors['black'])
stroke(colors['black'])
image(dharma, y+y*.25,y*.25, y*.5,y*.5)
I would just call the image, do time.sleep() then call a black rectangle to cover the image and do time.sleep() again, and repeat that however many times I want the image flashed, save that: 1. That probably breaks like all programming effenciency laws, and, 2. The fact that draw() is called ever millisecond or somesuch causes one sleep() to make the entire project to run in second intervals.
Thanks for any help! As an afternote, I will almost definitely be back with more questions on this project. Should those questions go under this topic or be asked as a separate question? Thanks!
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
You're gonna need some "state" approach for that "flash": :(|)
https://forum.Processing.org/two/discussion/10674/#Item_25
https://forum.Processing.org/two/discussions/tagged?Tag=#state
I'm afraid I have near to no clue what state is. Looking at your template leads me to believe that even my guess that it is the 'state' of the program is incorrect. That and the fact that Java and I are not exactly besties. If you had to explain it to a small child, what would you say?
It's beginning to make sense, I think! In your template, at the beginning where you have the
is that Java's way of creating the variables the same as saying
in Python? In the template, the keyPressed() function is only connected to draw() in that it alters the variables that draw() is checking to determine if it is looping or not, correct? I think I'm getting it, man!
They're rather more constants than variables in Java due to
final
.Python doesn't have that specific feature. But it's not that important. :-j
Although I'd convert
static final int ORIGINAL = 0, CONTRAST = 1, NEGATIVE = 2, WIPE = 3;
As this more compact statement in Python:
ORIGINAL, CONTRAST, NEGATIVE, WIPE = range(4)
Just be aware that you're sketch's requirements are more complex than that example.
It's a game and can't use noLoop(). It's always displaying some animation.
The states determine what needs to be displayed/done for each draw() frame. :-B
ORIGINAL, CONTRAST, NEGATIVE, WIPE = range(4)
Like I said about learning more from this than from my class. I didn't even know that was possible!
I think that I have states under control, at least as much of a grasp as I need for this project. But now I'm trying to think about the best method of obtaining the flash. I can't imagine that it would be morally appropriate, as a coder, to just overlap several of the images and black rectangles. Would there be a way to remove the image after it is drawn?
Aren't relying on background() as the 1st statement at the top of draw() in order to clear the whole canvas for the next drawings? :-\"
Best way of removing any drawing is simply by not drawing it anymore! ;;)
You pick the drawings to do based on current value of variable state. That's the trick. :-$
So I need two separate states for the startup, one with the image showing and one without, and just alternate between those two states as many times as I want the image flashed? That seems even more arduous than covering the image with the black rectangle.
Seems like you haven't still figured out the idea of program states.
That's why I've asked ya to look up other examples. (:|
state = FLASH
.No, that's it. I understand the list as a whole, my issue lies within bullet 3: I've pieced together how to get there, but how do I deal with the flash itself?
That's SFX. Not my forté at all. I can't help ya w/ that, sorry! X_X
i could do it in processing but not in py
but the principle is as gotoloop said use states to determine in which state you are (pre-flash, flash(on), flash(off), after-flash) and act accordingly
you can even give the states names:
all different values of course
etc.
now in
draw()
sayetc.
now the flash itself:
since you clear (using background or so) the screen at he beginning of draw() only that is visible that you actually draw!!!
so when state is
flashOn
show the thing, if it isflashOff
don't show it (no black rectangle needed)anyway, use a timer to toggle between state=flashOn and state=flashOff
Okay, so rather than have one state with the flashing functionality within, have the flashing functionality comprised of two states. Gotcha. Now, with just the intro gimmick taking two states, is there a limit to how many states I can or should be using?
that's what I said. Alternatively you could have the states pre-flash, flash, and after-flash only and make an extra boolean var
boolean flashOn=false;
No.
typical more states would be
stateEnterUserName1
andstateEnterUserName1
orstateShowHighScore
orstateHelp
here is a typical program with states:
Thank you both so much for the help that you've granted so far. Here is my new problem: Python doesn't have a switch statement. And that wouldn't be a problem, save that Python is that language that we've been learning for four months now. I have however found what looks like a switch class that someone created to do the same purpose. Does this look like it will work the same as Switch in Java:
Code by Brian Beck http://code.activestate.com/recipes/410692/
Very cool solution there! But C/ JS / Java's
switch () / case: / default:
block is merely some regular Python'sif: / elif: / else:
block using the equality==
operator only! ;))https://Processing.org/reference/switch.html
http://py.Processing.org/reference/else.html
As an illustration, here's a Java Mode sketch which uses
switch () / case:
in both draw() & mousePressed(): *-:)https://forum.Processing.org/two/discussion/14071/converting-java-code-to-python-for-dataviz#Item_11
When converted to Python Mode, they've become
if: / elif: / else:
instead: :ar!https://forum.Processing.org/two/discussion/14071/converting-java-code-to-python-for-dataviz#Item_13
Wow! That converted code cleared up a whole lot of issues! Thanks! But, of course, here's a new one: When I was first trying to load the font that I was using for the grid, I kept having trouble because Processing said that there was a syntax error right before the PFont, which I was only using because that's what I had seen in all the sketches with text. However when I left PFont out, it worked fine. Until I wanted to use a second font. So am I using text completely wrong? How do I load another font?
I don't have much experience w/ PFont either.
But as you already know, look up Processing's reference 1st:
I am ashamed: I had the file name mispelled