【问题标题】:Why can't I blit background image whilst character image is blited?为什么我不能在字符图像被 blit 时对背景图像进行 blit?
【发布时间】:2013-03-06 16:05:09
【问题描述】:

我正在尝试为我的游戏显示背景图像,但是当我对它进行 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()

【问题讨论】:

    标签: python image screen pygame blit


    【解决方案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()
    

    【讨论】:

    • 这个答案有何不同?
    • 我想我们俩同时回复
    • 我已经提前 10 分钟回复了。
    • 真的...我的回答 16:24 你的 16:53...但谁在乎至少我们都是对的。
    • 最后一次编辑是 16:53。下次看看有没有答案。
    【解决方案2】:

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

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

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

    screen.blit(background,(0,0))
    screen.blit(ball,ballrect)
    

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2020-11-07
      • 2019-06-28
      • 1970-01-01
      相关资源
      最近更新 更多