简体   繁体   中英

INDEPENDENT sprite movement in “x” and “y” in pygame

Ok so here is my current program:

bif="bg.jpg"
mif="pkmn.png"


import pygame
import sys

from pygame.locals import *

pygame.init()

screen=pygame.display.set_mode ((600,375),0,32)
background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()

x,y=0,0
movex, movey=0,0

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                movex=-1
            elif event.key==K_RIGHT:
                movex=+1
            elif event.key==K_UP:
                movey=-1
            elif event.key==K_DOWN:
                movey=+1

        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex=0
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_UP:
                movey=0
            elif event.key==K_DOWN:
                movey=0

    x+=movex
    y+=movey

    screen.blit(background,(0,0))
    screen.blit(mouse_c,(x,y))

    pygame.display.update()

The problem is, even tho I can move in all directions, when I press the right arrow key for example, it moves to the right, then if i keep holding right arrow key and hold down left arrow key, it will move left, the problem is this: when I let go of the right key, even tho I was going to the left, my sprite stops.

I know the problem is that when either right or left is released, x goes to 0

I wanna be able to hold right and go right, then hold left and go left instead, but then release left and go right because I was holding right

I hope I explained myself, I have tried everything and searched everywhere, please help me, if anyone can modify my code and just show it to me so i can analyze it, that would be great.

Thanks

In your event loop, check the state of the keys with pygame.key.get_pressed :

    while True:
        pressed = pygame.key.get_pressed()
        print(pressed[pygame.K_LEFT], pressed[pygame.K_RIGHT])

See this code , for example.

Try this:

import pygame
import sys

from pygame.locals import *


bif="bg.jpg"
mif="pkmn.png"


pygame.init()
FPS = 30  # changed for FPS
FPSCLOCK = pygame.time.Clock()  # changed for FPS

screen = pygame.display.set_mode ((600,375),0,32)
background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(mif).convert_alpha()

move_speed = 5  # changed for FPS
x, y = 0, 0

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pressed = pygame.key.get_pressed()
    if pressed[K_LEFT]:
        x -= move_speed
    if pressed[K_RIGHT]:
        x += move_speed
    if pressed[K_UP]:
        y -= move_speed
    if pressed[K_DOWN]:
        y += move_speed

    screen.blit(background,(0,0))
    screen.blit(mouse_c,(x,y))

    pygame.display.update()
    FPSCLOCK.tick(FPS)  # changed for FPS

Edit 1: I was running your code and noticed that you don't have any FPS monitoring- you're just running the code at the maximum rate the CPU can handle it. That'll lead to problems later, so I added code to make sure your FPS never goes above 30.

Edit 2: My old code was way overcomplicated; now that you agree that holding left and right should do nothing; this will work. (and it will be much more readable too, which helps protect you from bugs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM