0
\$\begingroup\$

As quite obviously stated in my title, why does my sprite only move each single time the key is pressed, (as opposed to moving WHILE the key is pressed.) By the way, really sorry, you won't be able to run it properly as I removed all the blocks that stop you walking into walls to allow it to run Here is my code:

import pygame
from pygame.locals import*
import pygame

pygame.init()

WHITE = (255,255,255)

start_in_bed = 0
#counts which sprite you should be on when running

#create screen
screen_width = 160
screen_height = 144
screen_multiplier = 4
screen = pygame.display.set_mode(((screen_width*screen_multiplier),(screen_height*screen_multiplier)))
pygame.display.set_caption('Pokemon Blood Red')

#Sprite stuff
sprite = pygame.image.load('player_south_still.png')
#Reform the sprite
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)

class Player(pygame.sprite.Sprite):

    def __init__(self):
        super(Player, self).__init__()
        self.player_sprite = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
        self.player_sprite.fill(WHITE)
        self.rect = self.player_sprite.get_rect()
        self.rect.x = 160/2
        self.rect.y = 144/2

class Blocks(pygame.sprite.Sprite):

    def __init__(self, width, height):
        super(Blocks, self).__init__()
        block_width = width
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.block_width = width
        self.block_height = height
#######################################################################

#Create all blockades ever

player = Player()
player.rect.x = (126*screen_multiplier)
player.rect.y = (100*screen_multiplier)
################################################################################
class Doors(pygame.sprite.Sprite):

    def __init__(self, width, height):
        super(Doors, self).__init__()
        self.player_house_door = pygame.Surface((width, height))
        self.player_house_door.fill(WHITE)
        self.rect = self.player_house_door.get_rect()
        self.rect.x = 40*screen_multiplier
        self.rect.y = 75*screen_multiplier       
player_house_door = Doors(8*screen_multiplier, 2*screen_multiplier)

player_group = pygame.sprite.Group()
player_group.add(player)

inside_house_door = Doors(22*screen_multiplier, 18*screen_multiplier)
inside_house_door.rect.x = 80*screen_multiplier
inside_house_door.rect.y = 13*screen_multiplier
screen.blit(inside_house_door.player_house_door, (inside_house_door.rect.x, inside_house_door.rect.y))

extra_door = Doors(8*screen_multiplier, 2*screen_multiplier)
################################################################################
nt_blocks = pygame.sprite.Group()
nt_blocks.add(nt_trees_left)
nt_blocks.add(nt_trees_top)
nt_blocks.add(nt_trees_right)
nt_blocks.add(nt_trees_right_bottom)
nt_blocks.add(laboratory)
nt_blocks.add(nt_trees_bottom)
nt_blocks.add(nt_player_house)
nt_blocks.add(nt_friend_house)


home_upstairs_blocks = pygame.sprite.Group()
home_upstairs_blocks.add(home_upstairs_back_wall)
home_upstairs_blocks.add(home_upstairs_back_back_wall)
home_upstairs_blocks.add(home_upstairs_tree)
home_upstairs_blocks.add(home_upstairs_left_wall)
home_upstairs_blocks.add(home_upstairs_right_wall)
home_upstairs_blocks.add(home_upstairs_bottom_wall)
home_upstairs_blocks.add(home_upstairs_banister)


home_downstairs_blocks = pygame.sprite.Group()
home_downstairs_blocks.add(home_downstairs_tree)
home_downstairs_blocks.add(home_downstairs_left_wall)
home_downstairs_blocks.add(home_downstairs_bottom_left_wall)
home_downstairs_blocks.add(home_downstairs_bottom_left_tree)
home_downstairs_blocks.add(home_downstairs_counter)
home_downstairs_blocks.add(home_downstairs_table)
home_downstairs_blocks.add(home_downstairs_sink)
home_downstairs_blocks.add(home_downstairs_tv)
home_downstairs_blocks.add(home_downstairs_fridge)
home_downstairs_blocks.add(home_downstairs_kitchen_back_wall)
home_downstairs_blocks.add(home_downstairs_banister)
home_downstairs_blocks.add(home_downstairs_bottom_right_wall)
home_downstairs_blocks.add(home_downstairs_top_wall)

lab_blocks = pygame.sprite.Group()
lab_blocks.add(lab_machine)
lab_blocks.add(lab_ball_table)
lab_blocks.add(lab_table)
lab_blocks.add(lab_stuff)
lab_blocks.add(lab_fridges)
lab_blocks.add(lab_bottom_left_fridges)

place = 3
def area_load():        
    global background

    if place == 0:
        global start_in_bed
        if start_in_bed == 0:
            player.rect.x = 126*screen_multiplier
            player.rect.y = 105*screen_multiplier
            sprite = pygame.image.load('player_south_still.png')
        else:
            player.rect.x = 110*screen_multiplier
            player.rect.y = 50*screen_multiplier
            sprite = pygame.image.load('player_west_still.png')
        inside_house_door.rect.x = 130*screen_multiplier
        inside_house_door.rect.y = 50*screen_multiplier
        sprite = pygame.image.load('player_west_still.png')
        background = pygame.image.load('player_house_upstairs.png').convert()
        background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
        screen.blit(background, (0, 0))
        screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
    if place == 1:
        start_in_bed += 1
        inside_house_door.rect.x = 110*screen_multiplier
        inside_house_door.rect.y = 24*screen_multiplier
#Player rect.x and rect.y are determined in collisions of doors so you end up at the right place
        player_house_door.rect.x = 73*screen_multiplier
        player_house_door.rect.y = 130*screen_multiplier
        door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
        if door_enter:
            while door_enter:
                player.rect.y -= 1*screen_multiplier
                door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
        background = pygame.image.load('player_house_bottom.png').convert()
        background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
        screen.blit(background, (0, 0))
    if place == 2:
        player_house_door.rect.x = 40*screen_multiplier
        player_house_door.rect.y = 75*screen_multiplier
        background = pygame.image.load('neuory_town.png').convert()
        background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
        screen.blit(background, (0, 0))
    if place == 3:
        player.rect.x = (70*screen_multiplier)
        player.rect.y = (112*screen_multiplier)
        inside_house_door.rect.x = 70*screen_multiplier
        inside_house_door.rect.y = 133*screen_multiplier
        extra_door.rect.x = 114*screen_multiplier
        extra_door.rect.y = 47*screen_multiplier
        sprite = pygame.image.load('player_north_still.png')
        background = pygame.image.load('lab.png').convert()
        background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
        screen.blit(background, (0, 0))
area_load()





#######################################################################


#Random variables for later use

pygame.init()

amount_caught = 0
catch1 = {'pokemon':'none',
          'hp':0,
          'attack':0,
          'defence':0,
          'sp_attack':0,
          'sp_defence':0,}

(x) = 160*0.45
(y) = 144*0.45
def caught():
    if amount_caught == 0:
        pass
        #catch1 values are equal to wild one's

#Mainloop
crashed = False
while not crashed:

    x_change = 0
    y_change = 0

    #Different buttons
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -3*screen_multiplier
                player.rect.x += x_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                if place == 3:
                    hits = pygame.sprite.spritecollide(player, lab_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = 1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, lab_blocks, False)
                if place == 2:
                    hits = pygame.sprite.spritecollide(player, nt_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = 1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, nt_blocks, False)
                if place == 1:
                    hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = 1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)

                if place == 0:
                    hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = 1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
                sprite = pygame.image.load('player_west_still.png')
            elif event.key == pygame.K_RIGHT:
                x_change = 3*screen_multiplier
                player.rect.x += x_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                if place == 3:
                    hits = pygame.sprite.spritecollide(player, lab_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = -1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, lab_blocks, False)
                if place == 2:
                    hits = pygame.sprite.spritecollide(player, nt_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = -1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, nt_blocks, False)
                if place == 1:
                    hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = -1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)

                if place == 0:
                    hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
                    if hits:
                        while hits:
                            print ('Collision!')
                            x_change = -1*screen_multiplier
                            player.rect.x += x_change
                            hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
                sprite = pygame.image.load('player_east_still.png')
            elif event.key == pygame.K_UP:      
                y_change = -3*screen_multiplier
                player.rect.y += y_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                if place == 3:
                    hits = pygame.sprite.spritecollide(player, lab_blocks, False)
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = 1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, lab_blocks, False)
                if place == 2:
                    hits = pygame.sprite.spritecollide(player, nt_blocks, False)
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = 1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, nt_blocks, False)
                if place == 1:
                    hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = 1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)

                if place == 0:
                    hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = 1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)

                sprite = pygame.image.load('player_north_still.png')
            elif event.key == pygame.K_DOWN:
                y_change = 3*screen_multiplier
                player.rect.y += y_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                if place == 3:
                    hits = pygame.sprite.spritecollide(player, lab_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = -1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, lab_blocks, False)
                if place == 2:
                    hits = pygame.sprite.spritecollide(player, nt_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = -1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, nt_blocks, False)
                if place == 1:
                    hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = -1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, home_downstairs_blocks, False)

                if place == 0:
                    hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)              
                    if hits:
                        while hits:
                            print ('Collision!')
                            y_change = -1*screen_multiplier
                            player.rect.y += y_change
                            hits = pygame.sprite.spritecollide(player, home_upstairs_blocks, False)
                sprite = pygame.image.load('player_south_still.png')  
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_change = 0
            elif event.key == pygame.K_RIGHT:
                x_change = 0
            elif event.key == pygame.K_UP:
                y_change = 0
            elif event.key == pygame.K_DOWN:
                y_change = 0    
    x += x_change
    y += y_change
    player.rect.x += x_change
    player.rect.y += y_change
    #Check for collisions
    hits = pygame.sprite.spritecollide(player, nt_blocks, False)
    if place == 3:
        door_enter = pygame.sprite.spritecollide(inside_house_door, player_group, False)
        if door_enter:
            print('n')
            player.rect.x = 100*screen_multiplier
            player.rect.y = 50*screen_multiplier  
            place = 2          
            area_load()    
    elif place == 2:
        door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
        if door_enter:
            print('n')
            player.rect.x = 70*screen_multiplier
            player.rect.y = 110*screen_multiplier  
            place = 1          
            area_load()
        door_enter = pygame.sprite.spritecollide(extra_door, player_group, False)
        if door_enter:
            print('n')
            player.rect.x = 70*screen_multiplier
            player.rect.y = 110*screen_multiplier  
            place = 3        
            area_load()
    elif place == 1:
        door_enter = pygame.sprite.spritecollide(inside_house_door, player_group, False)
        if door_enter:
            print('n')
            player.rect.x = (38*screen_multiplier)
            player.rect.y = (80*screen_multiplier) 
            place = 0
            sprite = pygame.image.load('player_east_still.png')
            if start_in_bed != 0:
                player.rect.x = 130*screen_multiplier
                player.rect.y = 30*screen_multiplier

            area_load()

        door_enter = pygame.sprite.spritecollide(player_house_door, player_group, False)
        if door_enter:
            print('outside door')
            player.rect.x = (38*screen_multiplier)
            player.rect.y = (80*screen_multiplier)
            place = 2
            sprite = pygame.image.load('player_north_still.png')
            area_load()
    elif place == 0:
        door_enter = pygame.sprite.spritecollide(inside_house_door, player_group, False)
        if door_enter:
            print('n')
            place = 1
            player.rect.y = 25*screen_multiplier
            player.rect.x = 132*screen_multiplier
            area_load()

    if place == 0:
        sprite = pygame.transform.scale(sprite, (10*screen_multiplier*2, 14*screen_multiplier*2))
    elif place == 1:
        sprite = pygame.transform.scale(sprite, (10*screen_multiplier*2, 14*screen_multiplier*2))
    elif place == 3:
        sprite = pygame.transform.scale(sprite, (10*screen_multiplier*2, 14*screen_multiplier*2))
    else:
        sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
    #Draw everything
    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
    screen.blit(background, (0, 0))
    #screen.blit(lab_bottom_left_fridges.image, (lab_bottom_left_fridges.rect.x, lab_bottom_left_fridges.rect.y))
    sprite.set_colorkey(WHITE)
    screen.blit(sprite,(player.rect.x,player.rect.y))
    pygame.display.flip()
pygame.quit()    

Thanks for the help!

\$\endgroup\$
  • 1
    \$\begingroup\$ Narrow this down for us. We don't want to hunt through your whole program. :( \$\endgroup\$ – Almo Mar 31 '17 at 19:25
  • \$\begingroup\$ I don't know Pygames well, but make sure you're using the correct function for checking WHILE a key is down, not just checking the event once. That's the only thing I think it could be. \$\endgroup\$ – GMR516 Mar 31 '17 at 21:30
1
\$\begingroup\$

Here is the section of relevant code from your program:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        crashed = True
        pygame.quit()
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x_change = -3*screen_multiplier
            player.rect.x += x_change
            screen.blit(player.player_sprite, (player.rect.x, player.rect.y))

When the player hits the left arrow key Pygame sends a KEYDOWN event. For each KEYDOWN event your program sees, it adjusts the player's position by 'x_change' pixels. Note that this happens ONCE per KEYDOWN event, which is why nothing happens when you hold down the key.

You have a couple basic options (there are lots more, but just as a quick overview). First, you could redesign your event logic so that the KEYDOWN event sets a velocity property of the player, and then a KEYUP event (fired on key release) sets it to 0. This will result in the player having a constant speed while the key is held. The other option, which is a bit better in my opinion, is to use Pygame's get_pressed() method to check which keys are currently pressed. This way you can adjust the player position every frame based on the keyboard state.

\$\endgroup\$
0
\$\begingroup\$

Your game loop seems malformed. Your loop looks essentially like this:

while running:
    for event in pygame.event.get():
        if keypress:
            set_velocity()
            update_position()
            render()
        elif keyrelease:
            reset_velocity()

The issue here is that you render the sprite in the event loop and that's not really a good way to do things. So, to make it simpler and clearer, you want your loop to be something like this instead:

while running:
    for event in pygame.event.get():
        if keypress:
            set_velocity()
        elif keyrelease:
            reset_velocity()

    window.fill((0, 0, 0))
    update_position()
    render()
    pygame.display.flip()

The key part that makes your sprites move continuously is the update_position() section, which will apply the velocity set in your events loop to the player sprite.

If you're wondering where collision resolution should happen in this case, it should probably be it's own function under update_position().

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.