通过同一窗口中的tkinter按钮启动/暂停pygame

时间:2019-03-10 10:08:35

标签: python tkinter pygame embed

对于一个学校项目,我和一个朋友用pygame做游戏。 pygame窗口嵌入在tkinter框架中。打开应用程序后,游戏立即开始。现在,我们要使用tkinter的按钮开始和暂停游戏。谢谢您,我很高兴。

import pygame, random, sys
from pygame.locals import *
from pygame.key import *
import tkinter as tk
from tkinter import *
import os

创建一个tkinter框架

root = tk.Tk()
root.title("Borderflip")
embed = tk.Frame(root, width = 500, height = 500) #Erzeugt einen 
eingebetteten Rahmen für das Pygame-Fenster
embed.grid(columnspan = (600), rowspan = 500) 
embed.pack(side = LEFT) #Fenster nach rechts
buttonwin = tk.Frame(root, width = 75, height = 500)
buttonwin.pack(side = LEFT)

这里是按钮 播放和暂停按钮没有功能

def play_button():

play_button = Button(root,  padx=8, width=18, pady=8, bd=8, font=("Arial", 
26), text="Play", command=play_button)
play_button.pack()

pause_button = Button(root,  padx=8, width=18, pady=8, bd=8, font=("Arial", 
26), text="Pause", command=root.quit)
pause_button.pack()

quit_button = Button(root, padx=8, width=18, pady=8, bd=8, font=("Arial", 
26), text="Quit", command=root.destroy)
quit_button.pack()

将pygame集成到tkinter中的代码

os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((500,500))
screen.fill(pygame.Color(255,255,255))
pygame.display.init()
pygame.display.update()
frame = Frame(root)
frame.pack()

pygame的代码

x = 500 # Breite und Höhe Fenster in Pixel
y = 500

sbreite = 100 # Breite und Höhe Schläger
shöhe = 15

sx = 200 # Definition 
sy = 450

bx = int(x/2)
by = int(y/2)

brad = 15

speed = 0

bxspeed = 1
byspeed = -2

leben = 10

def sblock():
    global speed
    if sx <= 0 or sx >= x-sbreite:
        speed = 0

def ballbewegung():
    global bx,by
    bx += bxspeed
    by += byspeed


def reset():
    global byspeed,bxspeed,leben,bx,by,sx,sy,speed
    sx = 200
    sy = 450

    bx = int(x/2)
    by = int(y/2)

speed = 0

bxspeed = random.randint(-2,2)
if bxspeed == 0:
    bxspeed = 1
byspeed = random.randint(-2,2)
if byspeed == 0:
    byspeed = 2
screen.fill((0,0,0))
pygame.draw.circle(screen, (255,255,0), (bx,by), brad, 0)
pygame.draw.rect(screen, (255,40,0), (sx,sy,sbreite,shöhe), 0)
pygame.display.flip()
pygame.time.wait(1000)
root.update()

def ballblock():
    global byspeed, bxspeed, leben
    if by-brad <= 0:
        byspeed *= -1
    if bx-brad <= 0:
        bxspeed *= -1
    if bx+brad >= x:
        bxspeed *= -1
    if by >= 435 and by <= 440:
        if bx >= sx-15 and bx <= sx + sbreite + 15:
            byspeed *= -1
        else:
            leben -= 1
            reset()
    root.update()

def sbewegung():
    global sx
    sx += speed

while leben > 0:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed = -2
            if event.key == pygame.K_RIGHT:
                speed = 2
screen.fill((0,0,0))
sbewegung()
sblock()
pygame.draw.rect(screen, (255,40,0), (sx,sy,sbreite,shöhe), 0)

ballbewegung()
ballblock()
pygame.draw.circle(screen, (255,255,0), (bx,by), brad, 0)

pygame.display.flip()
pygame.time.wait(5)

print ("haha verloren")

while True:
    pygame.display.update()
    root.update()

1 个答案:

答案 0 :(得分:0)

Tkinter + Pygame不能很好地配合使用,通常是由于它们的图形库以及一个程序在另一个程序处于活动状态时如何睡眠之间的冲突。

我个人建议从您的项目中删除tkinter并通过button建立一个pygame.sprite.Sprite()类,然后可以像tkinter按钮一样使用它。您可以阅读有关Here.

的文档