for pygame.event.get()中的事件:pygame.error:视频系统未初始化

时间:2016-06-22 05:35:06

标签: python-3.x

我是学生,这个游戏是我的任务之一。当玩家获胜或失败时,按下keykeytostart()并不起作用。但是一开始它有效。我每次玩的时候都会得到这个错误按摩(Pygame错误:视频系统未初始化)。 如何在游戏中添加暂停功能?

###########################设置屏幕,背景图像和声音
import random,pygame,sys,math
pygame.init()
from pygame.locals import*


WIDTH = 600
HEIGHT = 600
TEXTCOLOR = (255, 255, 255)
Yellow = (255, 255, 0)

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Pacman')
pygame.mouse.set_visible(False)
background_image = pygame.image.load('leaves.jpg')
backgroundmusic = pygame.mixer.Sound('lost.wav')
backgroundmusic.play()

##################################################Set up the score font

font_name = pygame.font.match_font('arial')
def draw_text(surf, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, Yellow)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

###################################################SET UP the images and Sound 

pacman = pygame.sprite.Sprite()
ghost = pygame.sprite.Sprite()

eatingsound = pygame.mixer.Sound('pacman_eatfruit.wav')
pacman.image = pygame.image.load('pacman1.png')
pacman.rect = pacman.image.get_rect()
pacman_group = pygame.sprite.GroupSingle(pacman)
over_image = pygame.image.load('game.jpg')
deathsound = pygame.mixer.Sound('pacman_death.wav')

##################################################press a key to start

def terminate():
    pygame.quit()
    sys.exit()

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                return

def drawText(text, font, surface, x, y):
    screen.blit(background_image, (0,0))
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    screen.blit(textobj, textrect)

font = pygame.font.SysFont(None, 48)

drawText('Pacman Game', font, screen, (WIDTH / 3), (HEIGHT / 3))
drawText('Press a key to start.', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()

######################################Adding Ghost in different position with different speed

class Ghost(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('ghost.png')
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(1, 8)

    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT + 10:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.speedy = random.randrange(1, 8)

all_sprites = pygame.sprite.Group()
ghosts = pygame.sprite.Group()
for i in range(2):
    g = Ghost()
    all_sprites.add(g)
    ghosts.add(g)

#################################################### Adding the Coins

TILE_SIZE = pacman.rect.width
NUM_TILES_WIDTH = WIDTH / TILE_SIZE
NUM_TILES_HEIGHT = HEIGHT / TILE_SIZE

candies = pygame.sprite.OrderedUpdates()

for i in range(50):
  candy = pygame.sprite.Sprite()
  candy.image = pygame.image.load('coin.png')
  candy.rect = candy.image.get_rect()
  candy.rect.left = random.uniform(0, NUM_TILES_WIDTH - 1) * TILE_SIZE
  candy.rect.top = random.uniform(0, NUM_TILES_HEIGHT - 1) * TILE_SIZE
  candies.add(candy)

###########################################Game Loop

score = 0
pause = False
gameOver = False
running = True
win = False

while running :
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
        if event.type == pygame.QUIT:
            running = False

###########################################Move the pacman

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                pacman.rect.top -= TILE_SIZE
            elif event.key == pygame.K_DOWN:
                pacman.rect.top += TILE_SIZE
            elif event.key == pygame.K_RIGHT:
                pacman.rect.right += TILE_SIZE
            elif event.key == pygame.K_LEFT:
                pacman.rect.right -= TILE_SIZE

##############################################Keep the pacman on the screen

        if pacman.rect.left < 0:
            pacman.rect.left = 0
        elif pacman.rect.right > 600:
            pacman.rect.right = 600
        elif pacman.rect.top <= 0:
            pacman.rect.top = 0
        elif pacman.rect.bottom >= 600:
            pacman.rect.bottom = 600

###############################################Able to use mouse

        if event.type == MOUSEMOTION:
            pacman.rect.move_ip(event.pos[0] - pacman.rect.centerx, event.pos[1] - pacman.rect.centery)

###############################################Adding coins randomly

        if event.type == pygame.USEREVENT:
            if win == False:
                candy = pygame.sprite.Sprite()
                candy.image = pygame.image.load('coin.png')
                candy.rect = candy.image.get_rect()
                candy.rect.left = random.uniform(0, NUM_TILES_WIDTH - 1) * TILE_SIZE
                candy.rect.top = random.uniform(0, NUM_TILES_HEIGHT - 1) * TILE_SIZE
                candies.add(candy)

################################################Collecting the coins and set the score

    collides = pygame.sprite.groupcollide(pacman_group, candies, False, True)
    if len(collides) > 0:
        eatingsound.play()
        score += 1
    if len(candies) == 0:
        win = True
    screen.blit(background_image, (0,0))

#################################################Wining the game

    if win:
        drawText('You Win!', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50) 
        pygame.display.update()
        winingsound = pygame.mixer.Sound('applause3.wav')
        winingsound.play()
        backgroundmusic.stop()
        waitForPlayerToPressKey()

##################################################################### Game Over screen     

    candies.draw(screen)
    pacman_group.draw(screen)
    all_sprites.draw(screen)
    draw_text(screen, str(score), 18, WIDTH / 2, 10)
    pygame.display.flip()
    all_sprites.update()

    hits = pygame.sprite.spritecollide(pacman, ghosts, False)
    if hits:
        gameOver = True

    if gameOver == True:
        drawText('Game Over!', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
        drawText('Press a key to start again.', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
        pygame.display.update()
        deathsound.play()
        backgroundmusic.stop()
        waitForPlayerToPressKey()

#############################################Drwing everything on the screen


pygame.quit()

1 个答案:

答案 0 :(得分:0)

尝试使用pygame.time.wait(时间) 或Pygame.event.wait()。第一个等待在行李中的时间量,第二个等待直到收到新的事件。