我不知道为什么它仍然是空白,即使没有错误。

时间:2013-10-17 00:09:13

标签: python-3.x pygame

这是我在pygame中的屏幕代码,但除黑屏外没有任何内容。没有错误,但我是python的新手所以你能告诉我什么是错的吗?

import pygame, sys
from pygame.locals import*

pygame.init()

我的屏幕

DISPLAYSURF=pygame.display.set_mode((300,200), 0, 32)
pygame.display.set_caption('WorldMaker')

颜色

BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255, 0, 0)
GREEN=(0,255,0)
BLUE=(0,0,255)
YELLOW=(255,255,0)

文字和行

DISPLAYSURF.fill(WHITE)
pygame.draw.line(DISPLAYSURF, BLACK, (0,30), (300,30), 3)
pygame.draw.line(DISPLAYSURF, BLACK, (200,0), (200,200), 3)
myfont=pygame.font.SysFont('Eras Bold ITC', 20)
label = myfont.render('WorldMaker', 1, BLACK)
DISPLAYSURF.blit(label,(50,10))
label1 = myfont.render('Store', 1, BLACK)
DISPLAYSURF.blit(label1,(225,10))

我的精灵

这是我遇到的最麻烦的部分,我认为可能是原因,也可能是主循环。

class B(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('Character.png').convert()
        self.rect = self.image.get_rect()
        self.rect.topleft=[150,50]

    def update(self):
        self.rect.y +=1

B_list=pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
b=B()
B_list.add(b)

#Main Loop
while True:
    B.update(b)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            pygame.display.update()

1 个答案:

答案 0 :(得分:1)

当前屏幕未清除,精灵不会绘制。

# create a couple units of B(), and save one as the player
player = B()
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add([player, B(), B()])

while True:
    # event handling

    # movement
    all_sprites_list.update()

    # drawing
    screen.fill(Color("white"))
    all_sprites_list.draw(screen)
    pygame.display.update()

提示WHITE RED是多余的。你可以使用Color("red")来做同样的事情。

相关问题