0

这是我使用的代码。

import pygame, sys

from pygame.locals import *

pygame.init()

def game():

width, height = 1000, 600
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('My game far now :P') #This command allows you make a title.
background=pygame.image.load('AE.jpg')
background = pygame.transform.scale(background, (width,height))
screen.blit(background, (0,0))

#Load target image and player



player = pygame.image.load('little.png')
player = pygame.transform.scale(player, (40,40))
px,py = width/2,height/2
screen.blit(player, (px,py))

movex = movey = 0

#Running of the game loop

while True:
    screen.blit(background, (0,0))
    #screen.blit(target,targetpos)
    screen.blit(player, (px,py))
    pygame.display.update()


    #keyboard an/or mouse movements
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()


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

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

                px = px + movex
                py = py + movey




#Python 's way of running the main routine

if __name__=='__main__':
   game()

当我运行程序时,一切正常,屏幕打开,背景和播放器在屏幕中间生成,但是当我尝试移动时什么也没发生,没有任何错误。

我会得到任何帮助:)

感谢您花时间帮助我。

4

3 回答 3

3

您似乎在最后两行中存在代码缩进问题,这可能会导致该错误。

如果代码块,您的当前代码等效于此:

    elif event.type == pygame.KEYUP:
        if event.key == K_RIGHT:
            movex = 0
        if event.key == K_LEFT:
            movex = 0
        if event.key == K_UP:
            movey = 0
        if event.key == K_DOWN # IF BLOCK STARTS
            movey = 0

            px = px + movex  # THIS FALLS IN THE PREVIOUS IF BLOCK
            py = py + movey

正确的代码是:

  elif event.type == pygame.KEYUP:
        if event.key == K_RIGHT:
            movex = 0
        if event.key == K_LEFT:
            movex = 0
        if event.key == K_UP:
            movey = 0
        if event.key == K_DOWN # IF BLOCK STARTS
            movey = 0          #IF BLOCK ENDS

  px = px + movex  # NOW THIS IS OUT OF THE IF BLOCK
  py = py + movey
于 2013-10-23T17:54:28.643 回答
0

拿这段代码,运动是优化的。删除了 Useles 代码。我希望你能理解它;)

import pygame, sys
from pygame.locals import *

pygame.init()
width, height = 1000, 600
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('My game far now :P')
background=pygame.image.load('AE.png')
background = pygame.transform.scale(background, (width,height))
player = pygame.image.load('little.png')
player = pygame.transform.scale(player, (40,40))
px,py = width/2,height/2
movex = movey = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]: px -= 2
    if keys[pygame.K_RIGHT]: px += 2
    if keys[pygame.K_UP]: py -= 2
    if keys[pygame.K_DOWN]: py += 2

    screen.blit(background, (0,0))    
    screen.blit(player, (px,py))
    pygame.display.update()
于 2013-10-24T13:51:10.227 回答
0

两个问题。

  1. 您不在循环中渲染
  2. 您仅在 if 语句中更新位置。

固定的:

while True:
    # input    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

        elif event.type == pygame.KEYDOWN:
            # ...snip...

    # physics
    px += movex
    py += movey

    # drawing

    screen.blit(background, (0,0))
    screen.blit(player, (px,py))
    pygame.display.update()
于 2013-10-23T23:00:46.860 回答