Reputation:
I'm new to pygame so my code is a bit messy but I hope you can help me. When I make a sprite move it works fine, but if I switch keys really fast, or if I press the jump button and let go, my sprite stops moving.
import shelve
import sys, pygame, pygame.mixer
import time
from pygame.locals import *
import random
pygame.init()
shelfFile = shelve.open('save_game')
#screen
sise = width, hight = 700, 600
red = 30,30,30
screen = pygame.display.set_mode(sise)
pygame.display.set_caption('Thing')
#varuables
background = pygame.image.load('background.png')
player = pygame.image.load('you.png')
enemy1 = pygame.image.load('enemy1.png')
clock = pygame.time.Clock()
px = shelfFile['px']
py = shelfFile['py']
health = shelfFile['health']
x2 = 0
x = 0
y = 0
u = 0
d = 0
t = 0
r = 0
ex = 0
ey = 0
cutseane = shelfFile ['cutseane']
black = 255,255,255
Punch = False
color = 255,0,0
radius = 5
room = shelfFile['room']
ehealth = shelfFile['ehealth']
while True:
pygame.event.get()
if jump == 1:
y = -20
if py <= 200:
jump = 2
if jump == 2:
y = +20
if py >= 480:
py = 480
#The Wall
if px <= 0:
px = 0
if px >= 630:
px = 630
pygame.display.flip()
screen.blit(background,(1,1))
screen.blit(player,(px,py))
#if ehealth >= 1:
#screen.blit(enemy1,(ex,ey))
#else:
#ex = 0
#ey = 0
px = px + x
py = py + y
clock.tick(30)
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if event.type == KEYDOWN and event.key == pygame.K_ESCAPE:
shelfFile['px'] = px
shelfFile['py'] = py
shelfFile['health'] = health
shelfFile['ehealth'] = ehealth
shelfFile['cutseane'] = cutseane
shelfFile['room'] = room
shelfFile.close()
pygame.quit(); sys.exit();
#Player Movement
elif event.type == pygame.KEYDOWN:
if keys [pygame.K_0]:
health -= 1
text = 1
shelfFile['text'] = text
elif event.key == pygame.K_RIGHT:
x = -15
elif event.key == pygame.K_LEFT:
x = +15
elif event.key == pygame.K_UP:
y = +15
elif event.key == pygame.K_DOWN:
y = -15
elif event.type == KEYUP:
if event.key == pygame.K_LEFT and x > 0:
x = 0
elif event.key == pygame.K_RIGHT and x < 0:
x = 0
elif event.key == pygame.K_UP and x > 0:
y = 0
elif event.key == pygame.K_DOWN and x < 0:
y = 0
Upvotes: 1
Views: 706
Reputation: 137
I think the problem is that in some places, you wrote y = +20
instead of y += 20
. If you want y
to increase by 20 units, do y += 20
, if you want y
to be set to the value 20, do y = 20
(does the same thing as y = +20
). I don't know what you are trying to do because I don't have your png files and I can't run the program to see what happens. Also, if it even is an unintentional mistake, you did the same thing for negatives and in other parts of the code.
This isn't really a mistake, it will work if not corrected but you also spelled some things wrong (if it was intentional, ignore this.)
sise should be size, hight should be height, cutseane should be cutscene...
There are other mistakes. If all of this works and they are not just typos, then you might be using some version of python that I am not aware of.
Upvotes: 1
Reputation: 20468
Your event loop is really messed up and should be restructured. keys = pygame.key.get_pressed()
shouldn't be called in the event loop, but in the outer while loop. I've got a minimal example to show you one way to do the movement and event handling (only with the event loop and without key.get_pressed
).
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((700, 600))
clock = pygame.time.Clock()
player = pygame.Surface((30, 30))
player.fill((120, 240, 90))
px = 100
py = 200
x_speed = 0
y_speed = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
# If player is on the ground, jump (in more advanced
# games you need collision detection instead >= 480).
if event.key == pygame.K_UP and py >= 480:
y_speed = -20
elif event.key == pygame.K_RIGHT:
x_speed = 10
elif event.key == pygame.K_LEFT:
x_speed = -10
elif event.type == pygame.KEYUP:
# If player is moving right, stop him.
if event.key == pygame.K_RIGHT and x_speed > 0:
x_speed = 0
# If player is moving left, stop him.
elif event.key == pygame.K_LEFT and x_speed < 0:
x_speed = 0
# Don't jump too high.
if py <= 200:
y_speed = 20
# Don't go below y = 480.
if py >= 480:
py = 480
# Move the player.
px += x_speed
py += y_speed
# The Wall.
if px <= 0:
px = 0
if px >= 630:
px = 630
screen.fill((50, 50, 50))
screen.blit(player, (px, py))
pygame.display.flip()
clock.tick(30)
Upvotes: 0