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

バグの原因となっている可能性がある最後の 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 # 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

2 つの問題。

  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 に答える