2

This is my code for a screen in pygame, but nothing appears except a black screen. There is no error but I'm new to python so could you tell me what's wrong?

import pygame, sys
from pygame.locals import*

pygame.init()

My Screen

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

Colors

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

Text and Lines

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

My Sprite

This is the part I am having the most trouble with and I think might be the cause or maybe the mainloop.

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

1 回答 1

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")用于同样的事情。

于 2013-10-17T00:31:01.860 回答