Use Tkinter to create GUI development tools (38) PyGame components in Tkinter

Use Tkinter to create GUI development tools (38) PyGame component
in Tkinter In the previous article, we introduced the turtle component in Tkinter, that is, run the code of the turtle library in the Tkinter window and display it on the Tkinter window.
See the previous article <Use Tkinter Create GUI development tools (32) Turtle component in Tkinter >
https://blog.csdn.net/hepu8/article/details/106322590
Since Tkinter can run turtle programs, then Tkinter also supports PyGame library to run PyGame games on Tkinter window .
this module is included in HP_pygame module.
direct gives us the demo code below:

import tkinter as tk
import os
import pygame as pg
from pygame.locals import *
import HP_pygame as hpg


root = tk.Tk()
root.title('HP_Pygame演示') 
root.geometry('{}x{}+{}+{}'.format(800, 600, 100, 200))
text = tk.Button(root, text='演示')
text.pack()

pp=hpg.pygameview(root)
pp.pack(fill=tk.BOTH, expand=tk.YES)
pp.update()  #必须先写,不然不显示控件
pp.set_mode(600, 500 )

def game(fr):
    pos = 0
    while 1:
        fr.screen.fill(pg.Color(0,0,0))
        pos = (pos + 1) % fr.screen.get_width()
        pg.draw.circle(fr.screen, pg.Color(255,255,255), (pos,100), 30)
        pg.display.flip()
        
        pg.display.update()

        #fr.update()

        # 从消息队列中循环取
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                fr.master.destray()

            if (event.type == pg.KEYDOWN or event.type == pg.MOUSEBUTTONDOWN):
                pg.quit()
                root.destray()

import pygame
def game2(fr):
    size = width, height = 600,400
    speed = [-2,1]
    #背景设置,全白
    bg = (255,255,255)
    #创建指定大小的窗口 Surface对象
    screen = pygame.display.set_mode(size)
    #设置窗口标题
    pygame.display.set_caption("弹弹弹,小游戏!")
    #加载图片
    gamemaster = pygame.image.load("img1/png1042.jpg")
    #获得图像的位置矩形
    position = gamemaster.get_rect()
    l_head = gamemaster
    r_head = pygame.transform.flip(gamemaster,True,False)
     
    #事件,终止事件
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == KEYDOWN:
                if event.key == K_LEFT:
                    gamemaster = l_head
                    speed = [-2,1]
                        
                if event.key == K_RIGHT:
                    gamemaster = r_head
                    speed = [2,-1]
                         
                if event.key == K_UP:
                     speed = [1,-2]
                         
                if event.key == K_DOWN:
                     speed = [-1,2]
                          
            elif event.type == KEYUP:
                    #speed =[-2,1]
                pass
            
        
        #移动图像
        position = position.move(speed)
     
        if position.left <0 or position.right > width:
            #图像翻转 gamemaster,True,False 左右翻转 上下不翻转
            gamemaster = pygame.transform.flip(gamemaster,True,False)
            #反方向移动
            speed[0] = -speed[0]
     
        if position.top <0 or position.bottom >height:
            #反方向移动
            speed[1] = -speed[1]
     
     
        #填充背景
        screen.fill(bg)
        #更新图像
        screen.blit(gamemaster,position)
        #更新界面
        pygame.display.flip()
        #延时10ms
        pygame.time.delay(10)


hpg.thread_it(game2(pp))
root.mainloop()

The results of the program.
Insert picture description here
Now, do you think Tkinter is very powerful?

#独狼荷蒲qq:2775205
#通通小白python Quantitative Group: 524949939
#电话微信:18578755056 #WeChat
public account: Lonewolf stock analysis

Guess you like

Origin blog.csdn.net/hepu8/article/details/106522565