7

当我尝试存在游戏时,我似乎无法摆脱这个错误。游戏运行良好,但只有在我尝试存在游戏时才会出现错误。

import pygame
from pygame import *
import random
import time
import os
import sys
from pygame.locals import *


black = (0,0,0)
white = (255,255,255)

pygame.init()

def game():

 os.environ['SDL_VIDEO_CENTERED'] = '1'
 mouse.set_visible(False)

#screen
screen_width = 800
screen_height = 500
screen = pygame.display.set_mode([screen_width,screen_height])
#load images etc.
backdrop = pygame.image.load('bg.jpg').convert_alpha()
menu = pygame.image.load('green.jpg').convert_alpha()
ballpic = pygame.image.load('ball.gif').convert_alpha()
mouseball = pygame.image.load('mouseball.gif').convert_alpha()
display.set_caption('Twerk')
back = pygame.Surface(screen.get_size())

def text(text,x_pos,color,font2=28):
    tfont = pygame.font.Font(None, font2)

    text=tfont.render(text, True, color)
    textpos = text.get_rect(centerx=back.get_width()/2)
    textpos.top = x_pos
    screen.blit(text, textpos)

start = False
repeat = False
while start == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            start = True
            #falling = True
            #finish = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                start = True
    #game over screen
    screen.blit(menu,[0,0])
    pygame.display.set_caption("TWERK")

    #Text
    #"Welcome to Escape"
    #needs replacing with logo 
    text("Twerk",60,white,300)

    #"Instructions"
    text("Instructions",310,white)
    text("----------------------------------------------------------------------------------------",320,white)
    text("Avoid the the enemies",340,white)
    text("Last as long as you can!",360,white)
    text("Press space to start",420,white)
    pygame.display.flip()


while start == True:
positionx=[]
positiony=[]
positionxmove=[]
positionymove=[]
falling = False
finish = False
score=0
enemies=4
velocity=1

for i in range(enemies):
  positionx.append(random.randint(300,400)+random.randint(-300,200))
  positiony.append(random.randint(200,340)+random.randint(-200,100))
  positionxmove.append(random.randint(1,velocity))
  positionymove.append(random.randint(1,velocity))


font = pygame.font.Font(None, 28)
text = font.render('Starting Twerk... ', True, (100,100,100))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery

screen.blit(backdrop, (0,0))
screen.blit(text, textRect)
pygame.display.update()
game=time.localtime()

while start == True:
  end=time.localtime()
  score= (end[1]-game[1])*3600 + (end[4]-game[4])*60 + end[5]-game[5]
  if score > 1: break

first=True
strtTime=time.localtime()

while not finish or falling:
  screen.blit(backdrop, (0,0))
  for i in range(enemies):
    screen.blit(ballpic,(positionx[i],positiony[i]))
    (mousex,mousey)=mouse.get_pos()
    screen.blit(mouseball,(mousex,mousey))
    display.update()
    strt = time.localtime()

  if first:
    while True:
      end=time.localtime()
      score= (end[3]-strt[3])*3600 + (end[4]-strt[4])*60 + end[5]-strt[5]
      if score > 3: break
    first = False

  if falling:
    for i in range(enemies):
      positionymove[i]=1000
      positionxmove[i]=0


  for i in range(enemies): positionx[i]=positionx[i]+positionxmove[i]
  for i in range(enemies): positiony[i]=min(600,positiony[i]+positionymove[i])

  if falling:
    falling=False
    for posy in positiony:
      if posy<600: falling=True


  if not falling:
    for i in range(enemies):
      for j in range(i+1,enemies):
        if abs(positionx[i]-positionx[j])<20 and abs(positiony[i]-positiony[j])<20:
          temp=positionxmove[i]
          positionxmove[i]=positionxmove[j]
          positionxmove[j]=temp
          temp=positionymove[i]
          positionymove[i]=positionymove[j]
          positionymove[j]=temp

    for i in range(enemies):  
      if positionx[i]>600: positionxmove[i]*=-1
      if positionx[i]<0: positionxmove[i]*=-1
      if positiony[i]>440: positionymove[i]*=-1
      if positiony[i]<0: positionymove[i]*=-1

    for i in range(enemies):
      if abs(positionx[i]-mousex)<40 and abs(positiony[i]-mousey)<40:
        falling = True
        finish = True
        #start = False
        endTime=time.localtime()
        score= (endTime[3]-strtTime[3])*3600 + (endTime[4]-strtTime[4])*60 + endTime[5]-strtTime[5]
        break

      for event in pygame.event.get():
        if event.type == pygame.QUIT:
          pygame.quit()

          if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
              pygame.quit()




game()

这是我得到的错误,我已将 quit() 更改为 QUIT 但仍然没有运气。

Traceback (most recent call last):
File "C:\Users\MO\Desktop\Twerk\ballbounce_changed.py", line 171, in <module>
 game()
File "C:\Users\MO\Desktop\Twerk\ballbounce_changed.py", line 160, in game
 for event in pygame.event.get():
 pygame.error: video system not initialized

谢谢你 :)

4

5 回答 5

7

当你想退出游戏时,你应该停止你的主循环。

我的建议,无论是

  • 调用exit()pygame.quit()
  • 设置finish = Truestart = False(尽管由于您粘贴的代码存在一些缩进问题,因此无法判断这实际上会起作用)

pygame.quit()仅当您想终止 Python 会话时才应调用。

我的建议:如果您想game()多次调用,例如在交互式会话中,您应该删除对pygame.quit()inside的调用game()。这个函数取消初始化 pygame 并且自然所有调用它的功能的尝试都会失败。

于 2012-04-21T19:01:47.600 回答
2

您调用了 pygame.quit(),但我认为您还需要调用 sys.exit()。

for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      sys.exit()

这就是我在所有游戏中所做的。

于 2012-04-21T23:48:24.547 回答
1

我认为这是因为您手动调用 pygame.quit() 。当你的脚本结束时,Pygame 会自动退出。

要仔细检查 pygame 是否正常工作,您可以运行以下样板: http: //ninmonkey.googlecode.com/hg/boilerplate/pygame/1.%20blank%20screen/1%20-%20basic.%20pygame%20--nocomments .py

当你想退出时,设置 done=True。(见上例中的第 37 行和第 64 行。)

于 2012-04-21T22:36:29.580 回答
0

我在使用 Spyder 尝试一些 Kivy 测试文件时遇到了这个问题。就我而言,因为我只是在尝试演示文件,所以很快就可以重新启动内核。

我知道这是一个生硬的解决方案,但是如果您在 IDE 上并且不介意这样做,它比键入命令要快。

于 2018-10-03T19:33:25.597 回答
0

你可以做 :

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False # Here we exit the Loop and execute what after.
pygame.quit()

它工作正常!

于 2019-07-12T11:14:18.810 回答