2016-02-17 117 views
0

我的程序運行playerinf()兩次,但從不運行下一個函數時,我要求它。代碼的底部是我調用哪些函數來運行的地方。我以前從來沒有遇到過這個問題。我究竟做錯了什麼?Python - 類重複一次調用兩次

import pygame 
import time 
import random 
import sys 
import hashlib 

pygame.init() 

screen = pygame.display.set_mode((1000,600)) 

pygame.display.set_caption("Parkour Legend") 

# variables 
black = (0, 0, 0) 
white = (255, 255, 255) 
red = (255, 0, 0) 
green = (0, 255, 0) 
blue = (0, 0, 255) 
clock = pygame.time.Clock() 
x = 0 

# Create Screen 

screen.fill(black) 
pygame.display.flip() 

# Default player 

name = "Bob" 
age = 15 
gender = "Boy" 

# Information Access 

class information: 
    def __init__(self, name, age, gender, enlistmentNum): 

     self.name = name 
     self.age = age 
     self.gender = gender 
     self.enlistmentNum = enlistmentNum 
     self.playerinf() 

    def playerinf(self): 

     if self.age < 12: 
      print("Sorry you are too young to play this game :(") 
      sys.exit() 
     else: 
      print("Your keyboard movements are being transmitted through one of the human-like beings of the world...") 
      time.sleep(5) 
      print("you have complete control of where he goes, but not his mind!") 
      time.sleep(3) 
      passrequested = raw_input("Enter Enlistment Number: ") 
      if passrequested == enlistmentNum: 
       print("Connecting to port: 597643") 
       time.sleep(.5) 
       print("Satellite Signal: Low") 
       time.sleep(2) 
       print("Recalibrating...") 
       time.sleep(2) 
       print("Satellite Signal: Excellent") 
       time.sleep(2) 
       data = random.randint(3872, 5195) 
       data = str(data) 
       print("Transmitting Data: "+str(data)+" GB/S") 
      else: 
       print("Password Entered was incorrect!") 
       time.sleep(1) 
       print("Shutting Down...") 
       sys.exit() 

    def startup(self): 
     screen.fill(white) 

name = raw_input("What is your name: ") 
age = int(input("What is your age: ")) 
gender = raw_input("Are you a boy or a girl: ") 

enlistmentNum = hashlib.sha256(name.encode()) 

enlistmentNum = enlistmentNum.hexdigest() 

print("-----------------------------") 
print("Enlistment forum:") 
print("Name: "+str(name)) 
print("Age: "+str(age)) 
print("Gender: "+str(gender)) 
print(" ") 
print("Your Enlistment Number is: "+str(enlistmentNum)) 
print("-----------------------------") 

player1 = information(name, age, gender, enlistmentNum) 
while x == 0: 
    x = 1 
    player1.playerinf() 
else: 
    player1.startup() 
    pygame.display.flip() 
    clock.tick(10) 

回答

1
class information: 
    def __init__(self, name, age, gender, enlistmentNum): 

     self.name = name 
     self.age = age 
     self.gender = gender 
     self.enlistmentNum = enlistmentNum 
     self.playerinf() 

playerinf被稱爲在你的初始化函數的末尾。 當你在最後

player1 = information(name, age, gender, enlistmentNum) 

然後再實例化一個玩家通過你

while x == 0: 
    x = 1 
    player1.playerinf() 
+1

哇哦這就是所謂!我怎麼錯過了?非常感謝! –

+0

不客氣 –