Python - 类被调用一次时重复两次

时间:2016-02-17 00:30:48

标签: python pygame

我的程序运行了两次playerinf(),但是在我调用它时从不运行下一个函数。在代码的底部是我调用哪些函数运行的地方。我之前从未遇到过这个问题。我做错了什么?

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 个答案:

答案 0 :(得分:1)

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

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

在init函数结束时调用playerinf。 在实例化播放器时调用它

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

然后你最后再次

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