import pygame
一、定义一个窗口
screen=pygame.display.set_mode((400,300))
pygame.display.set_caption('first--create a window') #窗口的标题
怎么退出
running=True
while running:
event=pygame.event.wait()
if event.type==pygame.QUIT:
running==False
pygame.quit()
-
-
- import pygame
-
-
- screen = pygame.display.set_mode((400,300))
-
- pygame.display.set_caption('first--create a window')
-
-
- running = True
- while running:
- event = pygame.event.wait()
- if event.type == pygame.QUIT:
- running = False
- pygame.quit()
二、在窗口上输出文字
-
- import sys,string,os
- import pygame
- from pygame.locals import *
-
-
- pygame.init()
-
-
- window = pygame.display.set_mode((300,400))
-
-
- pygame.display.set_caption("second_show txt")
-
-
- font = pygame.font.Font(os.environ['SYSTEMROOT']+u"//Fonts//simsun.ttc",20)
-
-
- text = font.render(u"pygame入门",1,(255,0,0))
-
-
- textpos = text.get_rect()
- textpos.centery = window.get_rect().centery
- textpos.centerx = window.get_rect().centerx
-
-
- window.blit(text,textpos)
-
-
- running = True
- while running:
- pygame.display.flip()
-
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- pygame.quit()
三、新的窗口
模块名 |
功能 |
pygame.cdrom |
访问光驱 |
pygame.cursors |
加载光标 |
pygame.display |
访问显示设备 |
pygame.draw |
绘制形状、线和点 |
pygame.event |
管理事件 |
pygame.font |
使用字体 |
pygame.image |
加载和存储图片 |
pygame.joystick |
使用游戏手柄或者 类似的东西 |
pygame.key |
读取键盘按键 |
pygame.mixer |
声音 |
pygame.mouse |
鼠标 |
pygame.movie |
播放视频 |
pygame.music |
播放音频 |
pygame.overlay |
访问高级视频叠加 |
pygame |
就是我们在学的这个东西了…… |
pygame.rect |
管理矩形区域 |
pygame.sndarray |
操作声音数据 |
pygame.sprite |
操作移动图像 |
pygame.surface |
管理图像和屏幕 |
pygame.surfarray |
管理点阵图像数据 |
pygame.time |
管理时间和帧信息 |
pygame.transform |
缩放和移动图像 |
#!/usr/bin/env python
background_image_filename = 'sushiplate.jpg'
mouse_image_filename = 'fugu.png'
#指定图像文件名称
import pygame
#导入pygame库
from pygame.locals import *
#导入一些常用的函数和常量
from sys import exit
#向sys模块借一个exit函数用来退出程序
pygame.init()
#初始化pygame,为使用硬件做准备
screen = pygame.display.set_mode((640, 480), 0, 32)
#创建了一个窗口 分辨率,标志位,色深
pygame.display.set_caption("Hello, World!")
#设置窗口标题
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
#加载并转换图像
#将图像转换为surface对象
while True:
#游戏主循环
for event in pygame.event.get():
if event.type == QUIT:
#接收到退出事件后退出程序
exit()
screen.blit(background, (0,0))
#将背景图画上去
#第一个参数是surface对象,第二个参数是左上角的位置
x, y = pygame.mouse.get_pos()
#获得鼠标位置
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
#计算光标的左上角位置
screen.blit(mouse_cursor, (x, y))
#把光标画上去
pygame.display.update()
#刷新一下画面
pygame.display.set_mode(分辨率,标志位,色深)
1.set_mode返回一个surface对象,代表了桌面上的那个大小。第一个参数(必须)表示分辨率(400*600),表示窗口的大小400*600。第二个是一个标志位,具体意思见下表,如果不用什么特性,就指定0;第三个为色深。
标志位 |
功能 |
FULLSCREEN |
创建一个全屏窗口 |
DOUBLEBUF |
创建一个“双缓冲”窗口,建议在HWSURFACE或者OPENGL时使用 |
HWSURFACE |
创建一个硬件加速的窗口,必须和FULLSCREEN同时使用 |
OPENGL |
创建一个OPENGL渲染的窗口 |
RESIZABLE |
创建一个可以改变大小的窗口 |
NOFRAME |
创建一个没有边框的窗口 |
2.convert函数是把图像转换为surface对象,如果不写python也会自动把image转换成surface
3.游戏的主循环是一个无限循环,直到用户跳出。在这个主循环里做的事情就是不停地画背景和更新光标位置,虽然背景是不动的,我们还是需要每次都画它, 否则鼠标覆盖过的位置就不能恢复正常了。
4.blit是个重要函数,第一个参数为一个Surface对象,第二个为左上角位置。画完以后一定记得用update更新一下,否则画面一片漆黑。
pygame.display.update()