首页 > 编程语言 >Python pygame新手入门基础教程

Python pygame新手入门基础教程

时间:2022-10-21 16:36:47浏览次数:49  
标签:get Python surface 新手入门 pygame 屏幕 display 255

pygame简介   pygame可以实现python游戏的一个基础包。     pygame实现窗口   初始化pygame,init()类似于java类的初始化方法,用于pygame初始化。

pygame.init()
设置屏幕,(500,400)设置屏幕初始大小为500 * 400的大小, 0和32 是比较高级的用法。这样我们便设置了一个500*400的屏幕。
surface = pygame.display.set_mode((500, 400), 0, 32)
如果不设置pygame事件的话,窗口会一闪而逝。这里去捕捉pygame的事件,如果没有按退出,那么窗口就会一直保持着,这样方便我们去设置不同的内容展示。
pygame.display.set_caption(“我的pygame游戏”)
  pygame.display,set_caption设置窗口的标题
import pygame, sys
from pygame.locals import *

pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("我的pygame游戏")

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
设置屏幕背景色    这里设置背景颜色为 (255, 255,255) ,然后更新屏幕    
# 设置背景颜色
surface.fill((255, 255, 255))
# 更新屏幕
pygame.display.update()

 

 

 

添加文字   首先获取Font对象,渲染Font对象,然后设置文本位置即可,pygame.font.SysFont(None, 40) 获取到文字对象,然后渲染文字为surface对象,basicFont.render 方法第一个参数是文字,第二个是是否去除锯齿,第三个和第四个是文字的颜色和文字的背景颜色。然后一个屏幕的区域,使用 blit将文字渲染到屏幕上。注意这里渲染的必须在屏幕的填充颜色之后,不然会覆盖文字。  
# 获取字体对象
basicFont = pygame.font.SysFont(None, 40)
# surface对象
text = basicFont.render('秀儿', True, (255,255,255), (0,255,0))
# 设置文本位置
textRect = text.get_rect()

textRect.centerx = surface.get_rect().centerx
textRect.centery = surface.get_rect().centery
# 将渲染的surface对象更新到屏幕上
surface.blit(text,textRect)

 

 

 如上图所示,中文显示乱码,这里我们获取系统的字体,并将其中一种中文字体设置为默认字体即可。

# 获取当前系统字体
fonts = pygame.font.get_fonts()
print(fonts)
完整代码
import pygame,sys
from pygame.locals import *


pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("我的pygame游戏")
surface.fill((255, 255, 255))

# 获取字体对象
basicFont = pygame.font.SysFont("方正粗黑宋简体", 48)
# surface对象
text = basicFont.render('秀儿', True, (255,255,255), (0,255,0))
# 设置文本位置
textRect = text.get_rect()

textRect.centerx = surface.get_rect().centerx
textRect.centery = surface.get_rect().centery
# 将渲染的surface对象更新到屏幕上
surface.blit(text,textRect)

pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
绘制多边形  polygon 来绘制多边形,第一个参数是屏幕对象,第二个是颜色,第三个是用点串连的一个元组,最后一个点有和第一个是一致的  
import pygame,sys
from pygame.locals import *


pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("我的pygame游戏")
surface.fill((255, 255, 255))

pygame.draw.polygon(surface, (0, 0, 255), ((50, 40), (100, 100), (120, 80), (50, 40)))

pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

 

绘制直线 line方法,第一个参数是屏幕对象,之后是颜色和两个点,最后一个参数是线条宽度
pygame.draw.line(surface, (0, 0, 255), (50, 40), (100, 100), 10)

 

 

 

绘制圆形 circle用来绘制圆形,第一个参数和第二个参数是屏幕对象和颜色,之后是圆心和半径,最后一个表示宽度,如果设置为0,则是一个实园。  
pygame.draw.circle(surface, (0, 0, 255), (50, 40), 20, 10)

 

 

绘制椭圆   第一个参数和第二个参数同上,第三个参数分别指定x和y轴的左上角,之后是x和y的半径,最后一个是宽度    
pygame.draw.ellipse(surface, (0, 0, 255), (50, 40, 20, 10), 2)

 

 

绘制矩形

  rect来绘制矩形,第一个和第二个参数同上,第三个参数分别制定左上角和右下角
pygame.draw.rect(surface, (0, 0, 255), (50, 40, 20, 10))

 

 

标签:get,Python,surface,新手入门,pygame,屏幕,display,255
From: https://www.cnblogs.com/tuixiulaozhou/p/16813884.html

相关文章

  • python中numpy切片问题
    方式1:逗号前表示行,冒号表示从该行的第几个到第几个(包含头不包含尾)方式2:逗号在后,表示列,冒号表示从该列的第几个到第几个(包含头不包含尾)......
  • python3学习笔记【简易】
    0.注意事项与码风修正1.注意到句尾分号没影响到编译,查资料知可加可不加,最好不加。当在一行中写多句代码时需要加。2.for循环和if/else句尾冒号前不要有空格! 1.操作种......
  • python抓取Prometheus的数据(使用prometheus-api-client库)
    python抓取Prometheus的数据(使用prometheus-api-client库)0、写在前面我们要想抓取Prometheus的数据,一般想到的就是requests请求,爬虫的方式来抓取,这是可行的,当然,还有一个......
  • Python应用框架一览表——敬请期待!
    Webflask、trondo、Django、GUIEasyGui、Tkinter框架、网络爬虫Scrapy框架Scrapy框架安装步骤:pipinstallscrapy使用Scrapy框架编写爬虫共计4步。数据分析re模......
  • 基于TensorFlow和Python的机器学习(笔记2)
    基于TensorFlow和Python的机器学习(笔记2)     油耗预测项目(实战)importioimportos.pathimportmatplotlib.pyplotaspltimportkeras.utilsimportte......
  • Python教程:Python函数和Python闭包
    原文链接:https://www.codevoila.com/post/51/python-tutorial-python-function-and-python-closure PythonfunctionsbeyondbasicsandadeeplookatclosureinPy......
  • [oeasy]python0010 - python虚拟机解释执行py文件的原理
    ​ 解释运行程序......
  • Python实验(第8周)
       实验7:面对对象程序设计一、实验目的和要求1、学会类的定义和使用;2、学会创建属性;3、实现类的继承。二、实验环境软件版本:Python3.1064_bit三、实验过程1......
  • Python操作数据库及应用
     Utils.DBHelper importcx_OracleclassOracleHandler(object):def__init__(self,name):ifname=='PULSE':self.user='user'......
  • Python操作SAP
    一些基础的操作SAP的CodeOpenSAP#OpenSAPdefopen_sap():try:Application(backend="win32").start(sap_path,timeout=60)log.info("OpenS......