0

我使用 cx_Freeze 生成了一个 exe 文件,但是当我打开 exe 文件时,黑色的 cms 窗口瞬间自动关闭。这是为什么?我的代码在 Python IDLE 中测试良好。提前感谢任何帮助者!

这是我的代码:

# import all modules here
from tkinter import *
import random
import time

score = 0

# create a Ball class
class Ball:

    # define the class initiation function
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas # save the canvas into an object property
        self.paddle = paddle
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color) # create a ball and save it into an object property
        self.canvas.move(self.id, 245, 100) # initialize the ball to position(x, y)
        self.speed = 3 # set the initiative speed
        starts = [self.speed, -self.speed] # create a list to hold 2 ball moving speed and direction
        random.shuffle(starts) # randomize the order of the list
        self.x = starts[0]
        self.y = -self.speed # set the y axis moving speed and direction
        self.canvas_height = self.canvas.winfo_height() # self.canvas_height = 400
        self.canvas_width = self.canvas.winfo_width() # self.canvas_width = 500
        self.hit_bottom = False # record if the ball has hit bottom

    # define the function that checks if the ball hits the paddle
    def hit_paddle(self, pos):

       # get the current position of the paddle
       paddle_pos = self.canvas.coords(self.paddle.id) 

       # check if the ball hits the paddle, return True if hits, otherwise, return False.
       if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                global score
                score += 1 # add score if the ball hits the paddle
                return True # return True if hits

       # return False if not hit            
       return False

    # define the ball animation function
    def draw(self):

        # the ball moves according to the speed and direction set in __init__ function
        self.canvas.move(self.id, self.x, self.y)

        # get the current position of the ball, pos = [255.0, 100.0, 270.0, 115.0] => [top, left, bottom, right]
        pos = self.canvas.coords(self.id) 

        # check top and bottom
        if pos[1] <=0:
            self.y = self.speed
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True

        # check if the ball hits the paddle
        if self.hit_paddle(pos) == True:
            self.y = -self.speed

        # check left and right
        if pos[0] <= 0:
            self.x = self.speed
        if pos[2] >= self.canvas_width:
            self.x = -self.speed

        if score >= 3:
            self.speed = 4

    # define the text display function when game is over
    def gameover(self):
        self.canvas.create_text(250, 100, text='Game Over, Zoe!', font=('Arial', 20), fill='red')
        self.canvas.create_text(250, 65, text='Your score: ' + str(score), font=('Arial', 15), fill='red')        

# create a Paddle class
class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)

        # initialize the position of the paddle to (x, y)
        self.canvas.move(self.id, 200, 300)
        self.x = 0 # set the paddle cannot move vertically
        self.canvas_width = self.canvas.winfo_width()

        # bind function onto key press events
        self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)        

    # define the paddle animation function    
    def draw(self):

        # the paddle moves according to the speed and direction set in turn_left and turn_right function        
        self.canvas.move(self.id, self.x, 0)

        pos = self.canvas.coords(self.id) # get the current position of the paddle

        # if the paddle reach the end of left or right, it stops moving
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.canvas_width:
            self.x = 0          

    # set the speed when a left key is pressed
    def turn_left(self, evt):
        self.x = -2

    # set the speed when a right key is pressed    
    def turn_right(self, evt):
        self.x = 2


tk = Tk()  # set the Tk() object
tk.title("Dennis' Python Game") 
tk.resizable(0, 0)  # make the window a fixed size, both horizontally and vertically
""" tk.wm_attributes("-topmost", 1) # make the window appear on top of all programs """

# set the game stage
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)

# generate the stage
canvas.pack()
tk.update()

# instantize ball and paddle class
paddle = Paddle(canvas, 'blue')  # create a Paddle instance
ball = Ball(canvas, paddle, 'lightblue') # create a Ball instance

# set a loop to animate the game
while 1:
    if ball.hit_bottom == False:        
        ball.draw()
        paddle.draw()
    else:
        ball.gameover()

    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)
4

1 回答 1

0

打开命令提示符并从那里运行 EXE。然后,您将看到它产生的任何输出都显示在控制台上。

从那里你可以很容易地调试它。

于 2013-05-28T03:49:59.067 回答