-
-
Save rors/daeff74a5904b435c594ff9d1ba96c80 to your computer and use it in GitHub Desktop.
Sasha midterm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# global img | |
circleY = 300 | |
circleDirection = 1 | |
position = 0 | |
startTime = 0 | |
rectY = 200 | |
rectDirection =1 | |
def drawLevel1(): | |
global circleY, circleDirection, startTime, position | |
# image(img,0,0) | |
fill(50,150,200) | |
rect(position,200, 50,50) | |
# move for a little while, then stop | |
if millis() > startTime and millis() < startTime + 2000: | |
position = position + 1 | |
# wait a little while, then reset the startTime variable, | |
# so the above timing starts over: | |
if millis() > startTime + 4000: | |
startTime = millis() | |
global rectY | |
fill(200,50,150) | |
rect(200,rectY,50,50) | |
global rectDirection | |
rectY= rectY + rectDirection | |
if rectY > width: | |
rectDirection = -1 | |
fill(102,51,0) | |
ellipse(300,circleY, 50,50) | |
circleY = circleY + circleDirection | |
if circleY > width: | |
circleDirection = -1 | |
if circleY < 0: | |
circleDirection = 1 | |
if keyPressed: | |
if key == 'j': | |
circleDirection = -1 | |
if key == 'l': | |
circleDirection =1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def drawLevel2(): | |
global img, circleY, circleDirection, startTime, position | |
image(img,0,0) | |
fill(50,150,200) | |
rect(position,200, 50,50) | |
# move for a little while, then stop | |
if millis() > startTime and millis() < startTime + 1000: | |
position = position + 1 | |
# wait a little while, then reset the startTime variable, | |
# so the above timing starts over: | |
if millis() > startTime + 3000: | |
startTime = millis() | |
global rectY | |
fill(150,50,200) | |
rect(200,rectY,50,50) | |
global rectDirection | |
rectY= rectY + rectDirection | |
if rectY > width: | |
rectDirection = -1 | |
fill(102,51,0) | |
ellipse(300,circleY, 50,50) | |
circleY = circleY + circleDirection | |
if circleY > width: | |
circleDirection = -1 | |
if circleY < 0: | |
circleDirection = 1 | |
if keyPressed: | |
if key == 'j': | |
circleDirection = -1 | |
if key == 'l': | |
circleDirection =1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Level_1 import* | |
from Level_2 import* | |
level = 1 | |
circleY = 300 | |
circleDirection = 1 | |
position = 0 | |
startTime = 0 | |
rectY = 200 | |
rectDirection =1 | |
def setup(): | |
global img | |
size(576,720) | |
img = loadImage("grass background.png") | |
stroke(50,50,150) | |
def draw(): | |
if level == 1: | |
drawLevel1() | |
elif level ==2: | |
drawLevel2() | |
image(img,0,0) | |
global circleY, circleDirection, startTime, position | |
fill(50,150,200) | |
rect(position,200, 50,50) | |
# move for a little while, then stop | |
if millis() > startTime and millis() < startTime + 2000: | |
position = position + 1 | |
# wait a little while, then reset the startTime variable, | |
# so the above timing starts over: | |
if millis() > startTime + 4000: | |
startTime = millis() | |
global rectY | |
fill(200,50,150) | |
rect(200,rectY,50,50) | |
global rectDirection | |
rectY= rectY + rectDirection | |
if rectY > width: | |
rectDirection = -1 | |
fill(102,51,0) | |
ellipse(300,circleY, 50,50) | |
circleY = circleY + circleDirection | |
if circleY > width: | |
circleDirection = -1 | |
if circleY < 0: | |
circleDirection = 1 | |
if keyPressed: | |
if key == 'j': | |
circleDirection = -1 | |
if key == 'l': | |
circleDirection =1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @sgodycki. Good question. So variable scope can be a little tricky across tabs.
In short:
global
variables are not accessible across tabs. So any variables that you want to use within a tab have to be defined within that tab (i.e., set to an initial value). If there is a variable that you are using in one tab that you want to use in another tab, then you need to pass that variable in to the other tab via your function call. See the week 8 class notes for an example & discussion on how to pass variables in to functions (i.e., parameters).For example, in your case, if there is a variable that you are using in
midterm_PART_2.pyde
that you want to use inLevel_1.py
, you could pass it in via thedrawLevel1()
function. You would change the function definition to look something like this:(
arg
is a variable name and you can call it whatever you'd like.) And then you could use that withindrawlLevel()
simply as you would use any other variable. For example:BUT, I don't think you need to do that here. You can simply use variables in each tab. Keep in mind that the scope of those variables will only be their own tab. I made some changes to show you this. Look at lines 3-8 of
Level_1.py
. I added variable definitions there for the variables that you'll be using in that tab. Keep in mind that now these are totally separate from the variables of the same name in your main tab.Also, in
midterm_PART_2.pyde
I movedglobal img
insidedef setup()
. Remember that you need to declare a variableglobal
in the scope where you are assigning or modifying it, but not where you are using it. So I also commented outglobal img
indef draw()
. And I commented out your use ofimg
inLevel_1.py
. If this works for you, you can repeat the pattern for level 2.It is a little unclear to me where you should drawing
img
to the screen however. My initial thought based on what it looks like you're trying to do is that indef draw()
you may only want the code on lines 18-21. The rest of the drawing code (including usingimg
) would be in the specific levels. If you go that route, then you wouldn't needcircleY
,circleDirection
, etc in your main tab. Only in each level. That may or may not work depending on the details of how you want game play to work. If you have more questions about that we could talk.Let me know if that helps.