I'm making a game for a school project and it is replicating Nintendo's Punch-Out!! game (a boxing game).
I use the variables left
and right
to determine which image the program uses, e.g. left = True
means that I use the left dodge image, and right = True
means that I use the right dodge image. When both of the variables are False, it uses the standing or 'idle' image.
So far I am able to make the character dodge left or right, but I can't get them to go back to neutral position and change back the image to the standing position after the button is pressed.
I've tried putting a py.time.delay() between the points where I would like a delay, but so far it hasn't worked.
This is my code:
import pygame as py
py.init()
window = py.display.set_mode((1000,600))
#Variables
x = 350
y = 250
height = 20
width =5
left = False
right = False
normIdle = py.image.load('p1idle.png')
dodgeLeft = py.image.load('p1DodgeLeft.png')
dodgeRight = py.image.load('p1DodgeRight.png')
#Dodging/ Image changing
def redrawgamewindow():
if left:
window.blit(dodgeLeft, (x,y))
elif right:
window.blit(dodgeRight, (x,y))
else:
window.blit(normIdle, (x,y))
py.display.update()
#Main Loop
run = True
while run:
#py.time.delay(300)
for event in py.event.get():
if event.type == py.QUIT:
run = False
keys = py.key.get_pressed()
if keys[py.K_LEFT]:
left = True
right = False
x -= 20
py.time.delay(300)
left = False
right = False
x += 20
if keys[py.K_RIGHT]:
left = False
right = True
x += 20
py.time.delay(300)
left = False
right = False
x -= 20
redrawgamewindow()
window.fill((0,0,0))
py.quit()
I am hoping to get the character to dodge left and stay for a couple milliseconds before automatically going back to the original position in the standing image.
I was able to get the code to work using a few small modifications that start just before the main loop:
# first, before the main loop draw the game window with Idle. This didn't show up before because you were going into the loop and redrawing idle. But now that we check to see if you are redrawing idle, it is probably a good idea to initialize things with redrawgamewindow().
redrawgamewindow()
#Main Loop
run = True
while run:
#py.time.delay(300)
for event in py.event.get():
if event.type == py.QUIT:
run = False
# this is a variable to see if the user did something that would change the graphics
redraw_my_char = False
keys = py.key.get_pressed()
if keys[py.K_LEFT]:
left = True
right = False
redraw_my_char = True
#note that this does not prevent button mashing e.g. right and left at once. You
if keys[py.K_RIGHT]:
left = False
right = True
redraw_my_char = True
if redraw_my_char: # you could say if did Left or Right, but I assume you'll be implementing punching, so it would probably be easier just to track one variable, redraw_my_char
# draw left or right
redrawgamewindow()
py.time.delay(300)
left = False
right = False
# with left or right reset, draw again
redrawgamewindow()
window.fill((0,0,0))
py.quit()
I deleted the x += / x -= commands, as they only changed before and after sending a delay. I can see ways they will be used once your game becomes more complex, but they aren't useful yet, for this issue.
Also, as a minor style thing, to get rid of one variable, you could also have if event.type == py.QUIT: break
.