python pygame基础1--定义窗口

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()

\
  1. # -*- coding: cp936 -*-  
  2. # 导入pygame模块  
  3. import pygame  
  4.   
  5. # 定义一个窗口  
  6. screen = pygame.display.set_mode((400,300))  
  7. # 窗口的标题  
  8. pygame.display.set_caption('first--create a window')  
  9.   
  10. # 接下来是解决怎么退出 这段代码是从pygame.org的tutorial上学的  
  11. running = True  
  12. while running:  
  13.     event = pygame.event.wait()  
  14.     if event.type == pygame.QUIT:  
  15.         running = False  
  16. pygame.quit()  

二、在窗口上输出文字

  1. # -*- coding: cp936 -*-  
  2. import sys,string,os  
  3. import pygame  
  4. from pygame.locals import *  
  5.   
  6. # 初始化窗口  
  7. pygame.init()  
  8.   
  9. # 设置窗口大小  
  10. window = pygame.display.set_mode((300,400))  
  11.   
  12. # 设置窗口标题  
  13. pygame.display.set_caption("second_show txt")  
  14.   
  15. # 创建字体对象,字体大小为20  
  16. font = pygame.font.Font(os.environ['SYSTEMROOT']+u"//Fonts//simsun.ttc",20)  
  17.   
  18. # 生成文字  
  19. text = font.render(u"pygame入门",1,(255,0,0))  
  20.   
  21. # 取得文本区域大小 我这里是将  
  22. textpos = text.get_rect()  
  23. textpos.centery = window.get_rect().centery  
  24. textpos.centerx = window.get_rect().centerx  
  25.   
  26. # 显示文字到window上  
  27. window.blit(text,textpos)  
  28.   
  29. # 事件循环  
  30. running = True  
  31. while running:  
  32.     pygame.display.flip()  
  33.     #这里用pygame.display.update()也是一样的效果  
  34.     for event in pygame.event.get():  
  35.         if event.type == pygame.QUIT:  
  36.             running = False  
  37.             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()

猜你喜欢

转载自blog.csdn.net/weixin_42162355/article/details/80307956
今日推荐