简体   繁体   中英

I can't move my player in pygame, can you figure why?

I'm trying to make a little game, and I came across a problem which I can't seem to figure out. The problem is that my sprite isn't moving after creating the game loop and the if statements with the keyup 's and what not. Can you guys figure it out and tell me why it's not working?

Here's my code so far:

import pygame, sys, random
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 600
windowHeight = 400
windowSize = (600, 400)
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
display = pygame.Surface((300, 200))
black = (0, 0, 0)
white = (225, 225, 255)
green = (0, 255, 0)
moveRight = False
moveLeft = False
moveSpeed = 6

level=   [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','2','2','2','2','2','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2'],
    ['1','1','2','2','2','2','2','2','2','2','2','2','0','0','2','2','2','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1']]

grass = pygame.image.load('grass.png')
dirt = pygame.image.load('dirt.png')
player = pygame.image.load('player.png').convert()
playerRect = pygame.Rect(100, 100, 5, 13)
player.set_colorkey((255,255,255))

while True:
    display.fill((214, 42, 78))
    #display.blit(player,(playerRect.x,playerRect.y))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                moveRight = True
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                moveLeft = False
        if event.type == pygame.KEYUP:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                moveRight = False
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                moveLeft = False
    display.blit(player,(playerRect.x,playerRect.y))



    playerMovement = [0, 0]
    if moveRight == True:
        playerMovement[0] += 2
    if moveLeft == True:
        playerMovement[0] -= 2


    tileRect = []
    y = 0
    for row in level:
        x = 0
        for col in row:
            if col == '2':
                display.blit(grass, (x*16, y*16))
            if col == '1':
                display.blit(dirt, (x*16, y*16))
            if col != '0':
                tileRect.append(pygame.Rect(x*16,y*16,16,16))
            x += 1
        y += 1


    #pygame.draw.rect(windowSurface, black, player)
    windowSurface.blit(pygame.transform.scale(display, windowSize),(0,0))
    pygame.display.update()
    mainClock.tick(60)

I've restructured your main loop to fix the problems. I change the playerMovement (velocity) in the event loop now and use it to update the playerRect in the main loop with the pygame.Rect.move_ip method.

By the way, you can pass the playerRect to blit to blit the image at the topleft ( x, y ) coords.

# The player's velocity. Define it outside of the main loop.
playerMovement = [0, 0]

while True:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                playerMovement[0] = 2  # Set the x-velocity to the desired value.
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                playerMovement[0] = -2
        if event.type == pygame.KEYUP:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                playerMovement[0] = 0  # Stop the player when the button is released.
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                playerMovement[0] = 0

    # Game logic.
    playerRect.move_ip(playerMovement)

    # Draw everything.
    display.fill((214, 42, 78))

    tileRect = []
    y = 0
    for row in level:
        x = 0
        for col in row:
            if col == '2':
                display.blit(grass, (x*16, y*16))
            if col == '1':
                display.blit(dirt, (x*16, y*16))
            if col != '0':
                tileRect.append(pygame.Rect(x*16,y*16,16,16))
            x += 1
        y += 1

    display.blit(player, playerRect)
    windowSurface.blit(pygame.transform.scale(display, windowSize), (0, 0))
    pygame.draw.rect(windowSurface, black, playerRect)
    pygame.display.update()
    mainClock.tick(60)

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