2016-06-22 30 views
0

私は学生で、このゲームは私の仕事の一つです。プレイヤーが勝っているか緩んでいる場合、pressanykeytostart()は機能しません。しかし、それは最初に動作します。また、再生するたびに、このエラーメッセージ(Pygame Error:Video System not Initialized)が表示されます。 ゲームに一時停止機能を追加するにはどうすればよいですか?イベントのpygame.event.get():pygame.error:ビデオシステムが初期化されていません

は、スクリーン、背景画像を設定し、pygame.timeで試してみてください
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() 

答えて

0

音########################### .wait(時間) またはPygame.event.wait()。最初はブラケットで時間を待っており、2回目は新しいイベントが受信されるまで待機します。

+0

どこに追加すればよいですか? – Masumeh

関連する問題