Skip to content

Commit

Permalink
- Added a mouse follower with text
Browse files Browse the repository at this point in the history
- Some comments adapted
  • Loading branch information
SteveClement committed Dec 10, 2016
1 parent 180d4f5 commit 1166423
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion 010_1stsNoMore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
try:
from colors import *
except ImportError:
raise ImportError('please use the python interpreter directly, pgzrun has issues with local module imports: python3 -m pgzero 1stText.py')
raise ImportError('please use the python interpreter directly, pgzrun has issues with local module imports: python3 -m pgzero 010_1stsNoMore.py')

BOX1 = Rect((20,20), (100,100))
BOX2 = Rect((40,40), (200,200))
Expand Down
6 changes: 2 additions & 4 deletions 018_RectFollowsMouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
try:
from colors import *
except ImportError:
raise ImportError('please use the python interpreter directly, pgzrun has issues with local module imports: python3 -m pgzero 1stText.py')
raise ImportError('please use the python interpreter directly, pgzrun has issues with local module imports: python3 -m pgzero 018_RectFollowsMouse.py')

# Define our 800x600 screen
WIDTH = 800
HEIGHT = 600

# The following will assign a tuple to the variable RED
RED = 200, 0, 0

# This will literally put a rectangle with certain properties into the variable BOX
BOX1 = Rect((0,0), (0,0))
BOX2 = Rect((20,20), (100,100))
Expand Down
66 changes: 66 additions & 0 deletions 019_RectFollowsMouseText.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# remember we have a nice colors.py file, let's use it and just not think too
# much about redefining colors ourselves, read colors.py to know what is available
# please use python3 -m pgzero mySource.py to run when import local modules
try:
from colors import *
except ImportError:
raise ImportError('please use the python interpreter directly, pgzrun has issues with local module imports: python3 -m pgzero 018_RectFollowsMouseText.py')

# Define our 800x600 screen
WIDTH = 800
HEIGHT = 600

# This will literally put a rectangle with certain properties into the variable BOX
BOX1 = Rect((0,0), (0,0))
BOX2 = Rect((20,20), (100,100))

# As we know, we need the draw function to put stuff on the screen
def draw():
# What if we do not clear the screen?
screen.clear()

# Needs to be define before we use growMin and growMax
mouseFont = fontNormalized(mousePos[1])

# We simply draw, to the screen, the variable BOX with the color in RED
screen.draw.rect(BOX1, RED)
screen.draw.rect(BOX2, BLUE)

# 2 Lines to show when we stop growing our font
screen.draw.line((0,growMin), (WIDTH, growMin),(GREEN))
screen.draw.line((0,growMax), (WIDTH, growMax),(GREEN10))

# Draw some text on the screen to give an idea where our mouse is at
screen.draw.text(
'mouseX: {0} mouseY: {1}'.format(mousePos[0], mousePos[1]),
center=((WIDTH/2), 20),
fontsize=48,
color=YELLOW,
)

screen.draw.text(
'{}'.format(mousePos),
center=mousePos,
fontsize=mouseFont,
color=PINK,
)

def on_mouse_move(pos):
global mousePos
x, y = pos
BOX1.size = (x, y)
BOX2.center = (x, y)
mousePos = pos

def fontNormalized(posY):
global growMin
global growMax
growMin = 40
growMax = 100

if posY < growMin:
return growMin
elif posY > growMax:
return growMax
else:
return posY

0 comments on commit 1166423

Please sign in to comment.