Sky
January 9, 2019, 6:29pm
1
Where I move player:
def keyPressed(self):
if keyPressed:
if key == 'a' or key == 'A':
self.state = "LEFT"
elif key == 'd' or key == 'D':
self.state = "RIGHT"
else:
self.state = "STOP"
def move(self):
if self.state =="RIGHT":
self.vec.x = constrain(self.vec.x + 10,40,width - 40)
elif self.state == "LEFT":
self.vec.x = constrain(self.vec.x - 10 ,40,width - 40)
self.state = "STOP"
def draw():
background(0)
ball.show()
paddle.show()
paddle.keyPressed()
paddle.move()
ball.move()
eg I keep going left and when I change the direction to right the paddle continues going left for 1/2 secs.
Its because keyPressed() has a delay where it cant read any key?
Inspired by my old Java Mode “Player Move”:
Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
I’ve made a Python Mode version “Paddle Move”:
"""
Paddle Move (v1.0.2)
by GoToLoop (2019-Jan-09)
https://Discourse.Processing.org/t/player-movement-laggy/7366/2
http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
"""
BG = 0350
def setup():
size(800, 600)
colorMode(RGB)
rectMode(CORNER)
fill(Paddle.INK)
stroke(Paddle.OUTLINE)
strokeWeight(Paddle.BOLD)
global p
p = Paddle()
print p
def draw():
background(BG)
p.move().display()
def keyPressed():
p.control(chr(keyCode) if key != CODED else keyCode, True)
def keyReleased():
p.control(chr(keyCode) if key != CODED else keyCode, False)
class Paddle:
W, H, V, O = 40, 10, 10, 5
INK, OUTLINE, BOLD = 0xff008000, 0, 1.5
def __init__(p):
p.x = width - p.W >> 1
p.y = height - p.H - p.O
p.l = p.r = False
def __str__(p, INFO='{ x: %d, y: %d }'):
return INFO % (p.x, p.y)
def display(p):
rect(p.x, p.y, p.W, p.H)
return p
def move(p):
p.x = constrain(p.x + p.V*(p.r - p.l), 0, width - p.W)
return p
def control(p, k, b):
if k == LEFT or k == 'A' or k == 'Z': p.l = b
elif k == RIGHT or k == 'D' or k == 'X': p.r = b
return p