When I move my character they have complete freedom. I want the game to control similarly to Mario, I don’t know how to make it so when you hit the up arrow key, the character moves up once instead of constantly moving up until the key is released.
import math
import random
global cWidth
global cHeight
cWidth = 900
cHeight = 700
gravity = 1
class player(object):
def __init__(self):
self.x = 0
self.y = 0
self.dx = 0
self.dy = 0
self.up = 0
self.down = 0
self.left = 0
self.right = 0
self.speed = 7
self.h = 20
self.w = 20
def show(self):
fill(255)
rect(self.x,self.y,self.w,self.h)
def update(self):
self.x = self.x + (self.right - self.left)*self.speed
self.y += self.dy
self.dy += gravity
if not (self.x >= 0):
self.x = 0
if not (self.x <= (cWidth - self.w)):
self.x = (cWidth - self.w)
if not (self.y >= 0):
self.y = 0
if not (self.y <= (cHeight - 200)):
self.y = (cHeight - 200)
show = "Welcome"
def setup():
size(cWidth,cHeight)
global p
frameRate(60)
p = player()
def draw():
noStroke()
background(0)
if (show=='Welcome'):
textSize(40)
fill("#FF0852")
# Sets the button at 50,250 -- width and height
rect(50,250, 200, 50)
fill(255)
text('Start',65,290)
fill("#FF0852")
rect(50,400, 200, 50)
fill(255)
text('Quit',65,440)
elif (show=='Start'):
p.show()
p.update()
fill(155)
rect(0,520,900,200)
fill("#FF0852")
rect(50,600, 200, 50)
fill(255)
text('Menu',65,640)
def mouseClicked():
global show, shots #this in
#Handles interaction of user with the various user interface buttons
if (mouseX>50 and mouseX<50+200 and mouseY>250 and mouseY<250+50 and show=='Welcome'):
show='Start'
elif (mouseX>50 and mouseX<50+200 and mouseY>400 and mouseY<400+50 and show=='Welcome'):
exit()
elif (mouseX>50 and mouseX<50+200 and mouseY>600 and mouseY<600+50 and show=='Start'):
show='Welcome'
s = second()
def keyPressed():
if key == 'w':
p.up =1
if key == 's':
p.down=1
if key == 'a':
p.left=1
if key == 'd':
p.right=1
def keyReleased():
if key == 'w':
p.up=0
if key == 's':
p.down=0
if key == 'a':
p.left=0
if key == 'd':
p.right=0