鼠标控件无响应



我刚刚开始使用pygame。我已经创建了我的游戏,但它真的很不稳定并且崩溃(退出时出现问题)。

我创建了一个欢迎页面,但它创建了一个游戏控制错误(鼠标控制的角色已停止响应),我尝试了一些方法,但只会导致更多的游戏崩溃和错误。 你能帮我调试你可能发现的任何东西吗? 您从她的 http://dl.dropbox.com/u/47312995/Twerk.rar 下载游戏

代码:

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 = display.set_mode((800,500))
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=1
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:
        endTime=time.localtime()
        score= (endTime[3]-strtTime[3])*3600 + (endTime[4]-strtTime[4])*60 + endTime[5]-strtTime[5]
        falling = True
        finish = True
game()

提前谢谢你

问题是你没有检查输入。 每次通过游戏循环时,您应该调用pygame.event.get()。 例如,从我的模板代码中:

def quit():
    pygame.quit(); sys.exit()
def GetInput():
    key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if   event.type == QUIT: quit()
        elif event.type == KEYDOWN:
            if   event.key == K_ESCAPE: quit()
def Draw():
    pygame.display.flip()
def main():
    Clock = pygame.time.Clock()
    while True:
        GetInput()
        Draw()
        Clock.tick(60)

最新更新