0

我正在尝试为我的游戏显示背景图像,但是当我对其进行 blit 时,它只会在屏幕上显示我的主要可玩角色,并带有黑色背景。谁能帮我解决这个问题。

这是代码:

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

我不知道您在粘贴时是否搞砸了,但是您的“if event.type”不是“while 1:”

无论如何试试这个:

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 回答
1

首先,您的缩进是错误的,但我认为这是一个错字。您的事件部分与您的游戏 while 循环处于同一级别,它应该在里面。bliting 和翻转也是。我还看到您错误地使用了 blitting 功能。来自 pygame 文档:

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

它将一个表面 blitsource到由Surface给出的目标处dest,可选地,area它是一个矩形,它定义了要被 blitted 的源表面的子表面。您必须分别对角色和背景图像进行 blit。所以你应该这样做:

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