【PyGame/PyQt5/PySDL2】get-start 快速入门

1.windows、linux,mac 安装python3

2.PC系统安装pygame, 指令:pip install pygame

3. 保存如下文件: test_game.py

4.PC上运行 python test_game.py 

import pygame
 
# 初始化Pygame
pygame.init()
 
# 创建游戏窗口
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame测试")

font = pygame.font.SysFont("Arial", 36)


 
# 游戏循环
running = True
count = 0
index = 0
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
 
    # 绘制背景
    count+=1
    if(count>1000):
        index = (index+1)%3
        count = 0

    if(index==0):
        window.fill((255,0,0))

    if(index==1):
        window.fill((0,255,0))

    if(index==2):
        window.fill((0,0,255))                
    
    print("loop_count=",count)
    text = font.render('hello pygame:'+str(count), True, (0, 0, 0), (255, 255, 255))
    window.blit(text, (0, 0))
    # 刷新窗口
    pygame.display.update()
 
# 退出游戏
pygame.quit()
print('=====pygame is quit!=====')

5.安装 pip3 install PyQt5==5.15.2

6. 保存如下文件[可用qtcreator辅助布局]: test_qt5.py

7.PC上运行 python test_qt5.py 

import sys
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton,QLineEdit,QComboBox,QCheckBox
import datetime


def btn0_clicked():
    print("#button_0 clicked!")
    print('list index=',combox.currentIndex())
    print("list text=",combox.currentText())
    print("checkbox state=",checkbox.isChecked())
    
def btn1_clicked():
    print("#button_1 clicked!")
    w.setStyleSheet("background-color:red;")
    print("Account=>",edit1.text())
    checkbox.setChecked(True)

def btn2_clicked():
    print("#button_2 clicked!")
    w.setStyleSheet("background-color:yellow;")
    print("Password=>",edit2.text())
    checkbox.setChecked(False)

if __name__ == "__main__":
    # 创建QApplication类的实例,并传入命令行参数
    app = QApplication(sys.argv)
    # 创建QWidget类的实例,相当于创建一个窗口
    w = QWidget()
    # 调整窗口的大小(宽,高)
    w.resize(320, 240)
    # 设置widget窗口背景颜色
    w.setStyleSheet("background-color:#00ee66;")
    
    current_time = datetime.datetime.now()
    print('start main test,current_time:',str(current_time))    

    # 设置窗口位置
    w.move(100, 200)
    # 设置窗口的标题
    w.setWindowTitle("pyqt5 title")
    #创建按键
    btn0 = QPushButton("OK", w)
    btn0.move(80,0)
    btn0.clicked.connect(btn0_clicked)    
    
    btn1 = QPushButton("Fist_Button", w)
    btn1.setToolTip("this Btn1 tips") #button tips text
    btn1.move(20, 50)
    btn1.clicked.connect(btn1_clicked)

    btn2 = QPushButton("Second_Button", w)
    btn2.setToolTip("this Btn2 tips") #button tips text
    btn2.move(120, 50)    
    btn2.clicked.connect(btn2_clicked)
    
    edit1 = QLineEdit(w)
    edit1.setPlaceholderText("Account:")
    edit1.move(20, 100) 
    
    edit2 = QLineEdit(w)
    edit2.setPlaceholderText("Password:")
    edit2.move(20, 130)     
    edit2.setEchoMode(QLineEdit.Password)
    
    combox = QComboBox(w)
    combox.addItem("Item0")
    combox.addItem("Item1")
    combox.addItem("Item2")
    combox.move(20, 0) 
    
    checkbox = QCheckBox(w)
    
      
    # 显示窗口
    w.show()
    # 进入循环的主循环,并通过exit函数确保主循环安全结束
    sys.exit(app.exec_())

8.在线安装 pip install -U pysdl2

【或离线下载pySDL2-master资源-CSDN文库,python setup.py install

9.PC运行如下代码

"""Simple example for using sdl2 directly."""
import os
import sys
import ctypes
import sdl2


def run():
    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO)
    window = sdl2.SDL_CreateWindow(b"Hello World",
                                   sdl2.SDL_WINDOWPOS_CENTERED,
                                   sdl2.SDL_WINDOWPOS_CENTERED,
                                   592, 460, sdl2.SDL_WINDOW_SHOWN)
    fname = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         "resources", "hello.bmp")
    image = sdl2.SDL_LoadBMP(fname.encode("utf-8"))
    windowsurface = sdl2.SDL_GetWindowSurface(window)
    sdl2.SDL_BlitSurface(image, None, windowsurface, None)
    sdl2.SDL_UpdateWindowSurface(window)
    sdl2.SDL_FreeSurface(image)

    running = True
    event = sdl2.SDL_Event()
    while running:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            if event.type == sdl2.SDL_QUIT:
                running = False
                break
        sdl2.SDL_Delay(10)

    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()
    return 0


if __name__ == "__main__":
    sys.exit(run())

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值