0

以前はこのスクリプトが機能していましたが、現在は機能しません。以前は、画像は画面の周りをうまく移動していましたが、今では移動すらしません。上下または左右のキーを押しても、画像は隅にとどまり、まったく移動しません。

import pygame, sys
from pygame.locals import *
pygame.init()

bifl = 'screeing.jpg'
milf = 'char_fowed_walk1.png'

screen = pygame.display.set_mode((640, 480))
background = pygame.image.load(bifl).convert()
mouse_c = pygame.image.load(milf).convert_alpha()


x, y = 0, 0
movex, movey = 0, 0

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

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

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



    x += movex
    y += movey

    screen.blit(background, (0, 0))
    screen.blit(mouse_c, (x, y))

    pygame.display.update()

私はpython 2.7を使用しています

4

1 に答える 1

1

キーを押すたびに移動速度が 0 に設定されないように、秒KEYDOWNを に変更するだけです。KEYUP

編集:さらに、コードにいくつかの奇妙な構文があります。=-および=+Python 演算子ではありません。+=と を意味していたと思います-=。また、可能な限りelif、ステートメントの代わりにステートメントを使用することを忘れないでください。ifこれにより、コードが最適化されるだけでなく、理解とデバッグが容易になります。

import pygame, sys
from pygame.locals import *
pygame.init()

bifl = 'screeing.jpg'
milf = 'char_fowed_walk1.png'

screen = pygame.display.set_mode((640, 480))
background = pygame.image.load(bifl).convert()
mouse_c = pygame.image.load(milf).convert_alpha()


x, y = 0, 0
movex, movey = 0, 0

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

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

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



    x += movex
    y += movey

    screen.blit(background, (0, 0))
    screen.blit(mouse_c, (x, y))

    pygame.display.update()
于 2013-09-11T07:02:14.387 に答える