First, what is Pygame?
pygame is a cross-platform python module, designed for video game design, including picture, sound.
Second, install pygame
# pip3.6 install pygame
test:
Three, pygame basic framework
Code:
import sys #导入sys模块
import pygame #导入pygame模块
pygame.init() #pygame初始化
size = width,height = 800,800 #设置窗口宽、高
screen = pygame.display.set_mode(size) #显示窗口
while True: #死循环,确保程序不会停止,窗口不会关闭
for event in pygame.event.get(): #pygame.event.get()获取事件队列,依据type来判断事件类型
if event.type == pygame.QUIT: #检测到关闭pygame窗口事件
sys.exit() #退出循环
pygame.quit() #退出pygame
operation result:
The following window appears
Four, pygame common module
Load picture:
pygame.image.load("图片名称")
eg:
xiaojiejie = pygame.image.load("./data/a/o/l/t/i/p/xiaojiejie.jpeg")
# ./data/a/o/l/t/i/p/xiaojiejie.jpeg 图片寻找路径
Set Window Title:
pygame.display.set_caption('来自一个爱你的小哥哥')
Load the upper left corner of the small icon:
taoxin = pygame.image.load("./data/a/o/l/t/i/p/taoxin.jpg").convert_alpha()
pygame.display.set_icon(taoxin) #图标
Effect schematic:
Background Color:
#以下是三原色
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
#根据三原色可按比例调制出其他颜色
eg:
grey = (177,177,177)
white = (255,255,255)
black = (0,0,0)
Load mp3 Music:
pygame.mixer.music.load("./data/a/o/l/t/i/p/jingqiaoqiao.mp3")
play music:
#在主循环中
if pygame.mixer.music.get_busy() == False: #如果音乐没有播放
pygame.mixer.music.play() #播放音乐
Fill color to the window:
screen.fill((255,255,255)) #(255,255,255)为白色
After loading the picture window to refresh the specified location:
screen.blit(xiaojiejie, (width_x, height_y))
#xiaojiejie为加载图片时赋予的变量
#(width_x, height_y)为以窗口左上角为(0,0)点的坐标系中的位置
Refresh the window:
#在主循环中:
pygame.display.flip()
或 pygame.display.update()
Load text:
#在主循环中:
textFont = pygame.font.SysFont("SimHei", 30) #加载字体
wo = textFont.render("我" , True, (200,0,60)) #加载文字
screen.blit(wo, (150, 440)) #将文字刷新到窗口指定位置
pygame.display.update() #刷新窗口
Delay program:
pygame.time.wait(3000) #程序停止3秒
Five, pygame combat
Know the common module, do not know how to use equal to 0, the following teach you to make use pygame vibrato confession artifact
- Note: Copy the code, then you have to note that I write I put pictures and music path paths
import sys #导入sys模块
import pygame #导入pygame模块
import random
pygame.init() #pygame初始化
size = width,height = 480,480 #设置窗口宽、高
screen = pygame.display.set_mode(size) #显示窗口
xiaojiejie = pygame.image.load("./data/a/o/l/t/i/p/xiaojiejie.jpeg")
width_x = 120
height_y = 120
kuaile = pygame.image.load("./data/a/o/l/t/i/p/kuaile.png").convert_alpha()
kuaile_x = 100
kuaile_y = 370
bukuaile = pygame.image.load("./data/a/o/l/t/i/p/bukuaile.png").convert_alpha()
bukuaile_x = 300
bukuaile_y = 370
taoxin = pygame.image.load("./data/a/o/l/t/i/p/taoxin.jpg").convert_alpha()
pygame.display.set_icon(taoxin) #图标
pygame.display.set_caption('来自一个爱你的小哥哥')
white = (255,255,255)
pygame.mixer.music.load("./data/a/o/l/t/i/p/jingqiaoqiao.mp3")
def get_random_pos():
bukuaile_x, bukuaile_y = random.randint(0,390), random.randint(0,440) #随机赋值
return bukuaile_x, bukuaile_y
while True: #死循环,确保程序不会停止,窗口不会关闭
for event in pygame.event.get(): #pygame.event.get()获取事件队列,依据type来判断事件类型
if event.type == pygame.QUIT: #检测到关闭pygame窗口事件
sys.exit() #退出循环
elif event.type == pygame.MOUSEBUTTONDOWN: #鼠标点击事件
#如果点击到了不快乐这个区域,随机给不快乐赋予x,y坐标
if event.pos[0] >= bukuaile_x and event.pos[0] <= bukuaile_x + 93 \
and event.pos[1] >= bukuaile_y and event.pos[1] <= bukuaile_y + 52 :
bukuaile_x, bukuaile_y = get_random_pos()
#如果点击到了快乐这个区域,显示我就知道你喜欢这张图
elif event.pos[0] >= kuaile_x and event.pos[0] <= kuaile_x + 91 \
and event.pos[1] >= kuaile_y and event.pos[1] <= kuaile_y + 54 :
kuaile_x = 10000
kuaile_y = 10000
bukuaile_x = 10000
bukuaile_y = 10000
xiaojiejie = pygame.image.load("./data/a/o/l/t/i/p/wojiuzhidaonikuaile.jpeg").convert_alpha()
#以下五行使操作显示到窗口,必不可少
screen.fill(white)
screen.blit(xiaojiejie, (width_x, height_y))
screen.blit(kuaile, (kuaile_x, kuaile_y))
screen.blit(bukuaile, (bukuaile_x, bukuaile_y))
pygame.display.update()
screen.fill(white)
screen.blit(xiaojiejie, (width_x, height_y))
screen.blit(kuaile, (kuaile_x, kuaile_y))
screen.blit(bukuaile, (bukuaile_x, bukuaile_y))
pygame.display.update()
if pygame.mixer.music.get_busy() == False: # 如果音乐没有播放
pygame.mixer.music.play() # 播放音乐
pygame.quit() #退出pygame
After running renderings:
Click the unhappy:
Unhappy random run, keep clicking, continue to run randomly
click happy:
I extend this program otherwise, have made a complete confession software, originality is not easy, reproduced, please indicate the source!