简体   繁体   English

我无法在pygame中移动我的玩家,你知道为什么吗?

[英]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. 问题是在创建游戏循环和带有keyupif语句之后,我的精灵没有移动。 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. 我改变playerMovement事件循环现在(速度),并用它来更新playerRect与主循环pygame.Rect.move_ip方法。

By the way, you can pass the playerRect to blit to blit the image at the topleft ( x, y ) coords. 顺便说一句,你可以通过playerRectblit在做块图像topleftx, 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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