2013-10-17 71 views
2

这是我在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

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

# 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() 

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

+0

当我输入all_sprites_list.draw() – user2888499

+0

时,我说我需要两个参数来绘制,我忘了添加屏幕。这个论点就是你的屏幕表面,或者任何你想要让组织闪耀的地方。例如:'all_sprites_list.draw(screen)' – ninMonkey

相关问题