0

ゲームの背景画像を表示しようとしていますが、それをブリットすると、画面に主なプレイ可能なキャラクターのみが黒の背景で表示されます。誰かがこの問題で私を喜ばせるのを手伝ってもらえますか?

コードは次のとおりです。

import sys, pygame, os
pygame.init()

size = width, height = 320, 240
xREVERSEspeed = [-2, 0]
xspeed = [2, 0]

screen = pygame.display.set_mode(size)
pygame.display.set_caption("Game")
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'

ball = pygame.image.load("turret.png").convert_alpha()
background = pygame.image.load("scoreframe.png").convert()
ballrect = ball.get_rect(center=(160, 231))
BACKGROUNDrect = background.get_rect()
clock = pygame.time.Clock()

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()    
        if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                        print "I've pressed the LEFT arrow button."
                        ballrect = ballrect.move(xspeed)
                        print ballrect
                if event.key == pygame.K_RIGHT:
                        print "I've pressed the RIGHT arrow button."
                        ballrect = ballrect.move(xREVERSEspeed)
                        print ballrect

    screen.blit(ball, ballrect, background, BACKGROUNDrect)
    pygame.display.flip()
4

2 に答える 2

1

まず、インデントが間違っていますが、これはタイプミスだと思います。イベント パーツはループ中のゲームと同じレベルにあり、内側にある必要があります。ブリッティングとフリッピングも。また、ブリッティング機能を間違って使用していることもわかります。pygame ドキュメントから:

Surface.blit(source, dest, area=None, special_flags = 0): return Rect

これは、で指定された宛先の にサーフェスをブリットsourceします。必要に応じて、ブリットするソース サーフェスのサブサーフェスを定義する長方形を指定します。キャラクターと背景画像を別々にブリットする必要があります。したがって、次のようにする必要があります。Surfacedestarea

screen.blit(background,(0,0))
screen.blit(ball,ballrect)
于 2013-03-06T16:21:23.577 に答える
1

ここに貼り付けたときに失敗したかどうかはわかりませんが、「if event.type」は「while 1:」の int ではありません。

とにかくこれを試してください:

while 1:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()


    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            print "I've pressed the LEFT arrow button."
            ballrect = ballrect.move(xspeed)
            print ballrect
        if event.key == pygame.K_RIGHT:
            print "I've pressed the RIGHT arrow button."
            ballrect = ballrect.move(xREVERSEspeed)
            print ballrect
    screen.blit(background,(BACKGROUNDrect))
    screen.blit(ball,(ballrect))
    pygame.display.flip()
于 2013-03-06T16:24:55.573 に答える