在“顶层”窗口处于活动状态时(直到其销毁),暂停动画循环吗?

sw123456

我有一个乒乓球(已改编)。每次球员的桨叶碰到球时,我都希望出现一个带有问号的窗口。如果他回答正确,则Toplevel窗口应该被破坏并且用户可以继续游戏,失败将导致更多问题,直到用户正确为止-然后他们可以继续游戏。

如何在Toplevel窗口处于活动状态时暂停动画循环,以及如何在Toplevel销毁窗口时取消暂停动画循环

要处理的代码的主要焦点是第一类(questions_window),game_flow函数和Paddle类的paddle_collisions方法。

这是所有代码:

from tkinter import *
import time

#starting velocity for the ball
x_speed = 25
y_speed = 25

class questions_window():
    def __init__(self, master):
        self.master = master
        self.master.geometry("300x300")
        self.master.title("Questions")

    def focus(self):
        self.master.attributes("-topmost", 1)
        self.master.grab_set()

class table():
    '''This is the table class - the background for the game and it's drawings'''
    def __init__ (self, window, colour = 'green', width=600, height = 400, score1 = 0, score2 = 0):
        self. colour = colour
        self.width = width
        self.height = height
        self.canvas = Canvas(window, bg=self.colour, height=self.height, width=self.width)
        self.canvas.pack()
        self.canvas.create_line(300, 0, 300, 400, fill="red")
        self.score1 = score1
        self.score2 = score2

    #this receives the coordinates of the ball and draws an oval based on these coordinates
    #the instance of this item is returned to the ball class' 'circle' attribute
    def draw_oval(self, oval):
        x1 = oval.x_posn
        x2 = oval.x_posn + oval.width
        y1 = oval.y_posn
        y2 = oval.y_posn + oval.height
        c = oval.colour
        return self.canvas.create_oval(x1, y1, x2, y2, fill=c)

    #this recieves the coordinates of the paddle and draws a rectangle based on these coordinates
    #the instance of this item is returned to the paddle class' 'rectangle' attribute
    def draw_rectangle(self, rectangle):
        x1 = rectangle.x_posn
        x2 = rectangle.x_posn + rectangle.width
        y1 = rectangle.y_posn
        y2 = rectangle.y_posn + rectangle.height
        c = rectangle.colour
        return self.canvas.create_rectangle(x1, y1, x2, y2, fill=c)

    #this method creates text on the canvas (the scores separated with a dash)
    def reset_scorecard(self):
        scores = str(self.score1) + " - " + str(self.score2)
        self.scorecard = self.canvas.create_text(300, 50, font=("Purisa",40), text=scores)

    #this finds out who has won a point and updates the scores on the canvas
    def update_scorecard(self, player):
        if player == 1:
            self.score1 = self.score1 + 1
        elif player == 2:
            self.score2 = self.score2 + 1
        scores = str(self.score1) + " - " + str(self.score2)
        self.canvas.itemconfig(self.scorecard, text=scores)

    #this method, when called, recieves a drawing object and updates its position on the canvas
    def move_item(self, item, x1, y1, x2, y2):
        self.canvas.coords(item, x1, y1, x2, y2)

class ball():
    '''This is the ball class'''
    def __init__(self, table, colour = 'red', width = 25, height = 25, x_speed = 15,
                                                 y_speed = 15, x_start = 5, y_start = 5):
        self.colour = colour
        self.width = width
        self.height = height
        self.x_posn = x_start
        self.y_posn = y_start
        self.table = table
        self.x_start = x_start
        self.y_start = y_start
        self.x_speed = x_speed
        self.y_speed = y_speed
        self.circle = self.table.draw_oval(self)

    #this method updates the ball's x and y coordinates by the speed values
    #it then checks if the ball has hit any walls - if it has it
    #reverses the x or y speeds depending on the wall it has hit
    #it then moves the item to the new coordinates
    def move_next(self):
        self.x_posn = self.x_posn + self.x_speed
        self.y_posn = self.y_posn + self.y_speed
        if (self.x_posn <=3):
            self.x_posn = 3
            self.x_speed = -self.x_speed
        if(self.x_posn >= (self.table.width - (self.width - 3))):
            self.x_posn = (self.table.width - (self.width - 3))
            self.x_speed = -self.x_speed
        if (self.y_posn <=3):
            self.y_posn = 3
            self.y_speed = -self.y_speed
        if(self.y_posn >= (self.table.height - (self.height - 3))):
            self.y_posn = (self.table.height - (self.height - 3))
            self.y_speed = -self.y_speed
        x1 = self.x_posn
        x2 = self.x_posn + self.width
        y1 = self.y_posn
        y2 = self.y_posn + self.height
        self.table.move_item(self.circle, x1, y1, x2, y2)

class paddle():
    '''This is the ball class'''
    def __init__(self, master, table, colour = 'blue', width = 10, height = 110,
                                                  x_start = 0, y_start = 20):
        self.colour = colour
        self.width = width
        self.height = height
        self.x_posn = x_start
        self.y_posn = y_start
        self.table = table
        self.master = master
        self.x_start = x_start
        self.y_start = y_start
        self.rectangle = self.table.draw_rectangle(self)

    #this method updates the paddles position on the screen
    #it recieves the mouse' x and y positions and updates the y position
    #of the paddle to match the y position of the mouse
    #it then moves the paddle to the new coordinates
    def move(self, event):
        self.y_posn = event.y
        x1 = self.x_posn
        x2 = self.x_posn + self.width
        y1 = self.y_posn
        y2 = self.y_posn + self.height
        self.table.move_item(self.rectangle, x1, y1, x2, y2)

    #this method checks if the ball has moved passed the paddle
    #if it has it will update the scorecard passing in the value of 2
    #if it has BUT has done so between the top and bottom ends of the paddle
    #it takes this as a paddle hit and updates score with 1 passed to the method
    def paddle_collision(self, ball, master):
        global switch_value
        if ((self.x_posn + self.width) > ball.x_posn) and (self.y_posn < ball.y_posn < (self.y_posn + self.height)):
            ball.x_speed = abs(ball.x_speed)
            print("Yes!")
            self.table.update_scorecard(1)
        elif (self.x_posn+4) > ball.x_posn:
            print("Collision")
            self.table.update_scorecard(2)
            question_win = Toplevel(self.master)
            question_window = questions_window(question_win)
            question_window.focus()

#create the window object
window = Tk()
window.title("Tennis")

#create the table, ball and paddle objects
myTable = table(window)
myTable.reset_scorecard()
myBall = ball(table=myTable, x_speed = x_speed, y_speed = y_speed)
paddle1 = paddle(table=myTable, master = window)

#this is the animation loop which calls itself after every 40ms
#each call calls the ball objects move_next method and the paddle's collision method
def game_flow():
    myBall.move_next()
    paddle1.paddle_collision(myBall,window)
    window.after(40, game_flow)

#first call of game_flow to start game
game_flow()

#binds the mouse events to the padde's move method
window.bind("<Motion>", paddle1.move)

window.mainloop()
布莱恩·奥克利(Bryan Oakley)

在希望动画停止时设置一个标志,然后在需要时重新启动动画。

def game_flow():
    if not paused:
        myBall.move_next()
        paddle1.paddle_collision(myBall,window)
        window.after(40, game_flow)

def pause():
    global paused
    paused = True

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

蓝牙处于活动状态时如何暂停/睡眠?

来自分类Dev

处于暂停状态时活动会发生什么?

来自分类Dev

NavLink:悬停时处于活动状态的CSS动画

来自分类Dev

哪个窗口处于活动状态

来自分类Dev

弹出窗口处于活动状态时禁用滚动

来自分类Dev

窗口处于非活动状态时刷新Office Ribbon UI

来自分类Dev

当adblock处于活动状态时显示弹出窗口

来自分类Dev

C#如何在Form2处于活动状态时暂停Form1?

来自分类Dev

如何在 ttk.Button 状态处于活动状态时更改其前景色?

来自分类Dev

处于暂停状态时,Android的MediaPlayer setSurface

来自分类Dev

处于暂停状态时,Android的MediaPlayer setSurface

来自分类Dev

当子窗口在 WPF 中处于活动状态时如何防止关闭父窗口

来自分类Dev

检查窗口是否处于活动状态

来自分类Dev

UISearchController在表标题视图中显示的UISearchBar处于活动状态时动画太远

来自分类Dev

防止计时器在窗口处于非活动状态时停止计时

来自分类Dev

设备的(android)键盘处于活动状态时,jQuery移动弹出窗口小部件不会移动

来自分类Dev

当一个元素已经处于活动状态时,关闭一个jQuery弹出窗口

来自分类Dev

RDP会话处于活动状态时如何隐藏最上面的窗口

来自分类Dev

仅在诺言处于活动状态时如何显示和隐藏弹出窗口?反应

来自分类Dev

设备的(android)键盘处于活动状态时,jQuery移动弹出窗口小部件不会移动

来自分类Dev

自动热键。每当某个窗口处于活动状态时,如何使脚本触发?

来自分类Dev

切换回时,激活工作区切换之前处于活动状态的窗口

来自分类Dev

当一个元素已经处于活动状态时,关闭一个jQuery弹出窗口

来自分类Dev

在UILongPressGestureRecognizer处于活动状态时增长UIImageView

来自分类Dev

崩溃处于活动状态时更改班级

来自分类Dev

在函数处于活动状态时终止脚本

来自分类Dev

iOS-如何在应用程序处于活动状态和非活动状态时通过自定义声音发出警报并暂停

来自分类Dev

为什么我的请求处于循环状态而被暂停?

来自分类Dev

循环处于活动状态时,计时器是否正在运行?

Related 相关文章

  1. 1

    蓝牙处于活动状态时如何暂停/睡眠?

  2. 2

    处于暂停状态时活动会发生什么?

  3. 3

    NavLink:悬停时处于活动状态的CSS动画

  4. 4

    哪个窗口处于活动状态

  5. 5

    弹出窗口处于活动状态时禁用滚动

  6. 6

    窗口处于非活动状态时刷新Office Ribbon UI

  7. 7

    当adblock处于活动状态时显示弹出窗口

  8. 8

    C#如何在Form2处于活动状态时暂停Form1?

  9. 9

    如何在 ttk.Button 状态处于活动状态时更改其前景色?

  10. 10

    处于暂停状态时,Android的MediaPlayer setSurface

  11. 11

    处于暂停状态时,Android的MediaPlayer setSurface

  12. 12

    当子窗口在 WPF 中处于活动状态时如何防止关闭父窗口

  13. 13

    检查窗口是否处于活动状态

  14. 14

    UISearchController在表标题视图中显示的UISearchBar处于活动状态时动画太远

  15. 15

    防止计时器在窗口处于非活动状态时停止计时

  16. 16

    设备的(android)键盘处于活动状态时,jQuery移动弹出窗口小部件不会移动

  17. 17

    当一个元素已经处于活动状态时,关闭一个jQuery弹出窗口

  18. 18

    RDP会话处于活动状态时如何隐藏最上面的窗口

  19. 19

    仅在诺言处于活动状态时如何显示和隐藏弹出窗口?反应

  20. 20

    设备的(android)键盘处于活动状态时,jQuery移动弹出窗口小部件不会移动

  21. 21

    自动热键。每当某个窗口处于活动状态时,如何使脚本触发?

  22. 22

    切换回时,激活工作区切换之前处于活动状态的窗口

  23. 23

    当一个元素已经处于活动状态时,关闭一个jQuery弹出窗口

  24. 24

    在UILongPressGestureRecognizer处于活动状态时增长UIImageView

  25. 25

    崩溃处于活动状态时更改班级

  26. 26

    在函数处于活动状态时终止脚本

  27. 27

    iOS-如何在应用程序处于活动状态和非活动状态时通过自定义声音发出警报并暂停

  28. 28

    为什么我的请求处于循环状态而被暂停?

  29. 29

    循环处于活动状态时,计时器是否正在运行?

热门标签

归档