赞
踩
下面定义了一个白色,字体大小为60,在x=200,y=200处开始绘制的文字
myfont = pygame.font.Font(None,60)
white = 255,255,255
textImage = myfont.render("here", True, white)
screen.blit(textImage, (200,200))
加入上一章的程序后完整程序如下
# -*- coding=utf-8 -*- import pygame pygame.init() screencaption = pygame.display.set_caption('first pygame') screen = pygame.display.set_mode((400,400)) #设置400*400窗口 screen.fill((0,0,255)) # 将界面设置为蓝色 myfont = pygame.font.Font(None,60) white = 255,255,255 textImage = myfont.render("here", True, white) screen.blit(textImage, (200,200)) pygame.display.update() # 必须调用update才能看到绘图显示 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit()
程序的效果是下面这样的
获取鼠标位置只需要用到pygame.mouse.get_pos(),代码如下
x, y = pygame.mouse.get_pos() # 获取鼠标位置
textImage = myfont.render("x=%s,y=%s"%(x,y), True, white) #将鼠标位置设置为显示内容
由于鼠标的移动位置刷新后会重复绘制文字,所以我们要将界面的底色设置部分和文字的绘制部分的代码放到主循环中,并且将pygame.display.update() 也放入主循环,修改后的完整代码如下
# -*- coding=utf-8 -*- import pygame pygame.init() screencaption = pygame.display.set_caption('first pygame') screen = pygame.display.set_mode((400,400)) #设置400*400窗口 myfont = pygame.font.Font(None,30) white = 255,255,255 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.fill((0,0,255)) # 将界面设置为蓝色 x, y = pygame.mouse.get_pos() # 获取鼠标位置 textImage = myfont.render("x=%s,y=%s"%(x,y), True, white) screen.blit(textImage, (200,200)) pygame.display.update() # 必须调用update才能看到绘图显示
现在鼠标移动时中间的文字就会跟着立即显示鼠标的坐标了,我们看到鼠标放到最左上角的时候x、y的值都是接近0的,放到最右下角是接近400的
下面定义了一个黄色、半径是30的、中心位置在100,500的圆
circle_color = [255,255,0]
circle_x = 100
circle_y = 50
circle_size = 30
line_width = 2
pygame.draw.circle(screen,circle_color,[circle_x,circle_y],circle_size,line_width)
我们把这段代码加入主循环的 pygame.display.update() 前看到界面上多了一个的圈
下面定义了一个黄色,宽80,高30,左上角顶点在300,500位置的矩形
rect_color = 255,255,0
rect_x = 300
rect_y = 50
rect_width = 80
rect_height = 30
pygame.draw.rect(screen,rect_color,[rect_x,rect_y,rect_width,rect_height],line_width)
我们把这段代码加入主循环的 pygame.display.update() 前看到界面上多了一个矩形
下面定义了一个黄色,从x=50,y=120的点连接到x=300,y=160的点的线段
line_color = 255,255,0
pygame.draw.line(screen,line_color,[50,120],[300,160], line_width)
绘制效果如下
请先将这个图片存放到程序的运行目录,并且命名为pygame.gif
下面是加载图片的代码
image = pygame.image.load('pygame.gif') # 加载图片
image = pygame.transform.scale(image,(338,100)) #图片有点大,将它缩小到宽338高100的大小
screen.blit(image, (40,250)) # 在界面上x=40,y=250的位置显示此图片
我们把前两行代码加入主循环外,第三行代码加入主循环的pygame.display.update() 前,看到的效果是这样的
结束本章前贴一下目前的完整代码
# -*- coding=utf-8 -*- import pygame pygame.init() screencaption = pygame.display.set_caption('first pygame') screen = pygame.display.set_mode((400,400)) #设置400*400窗口 myfont = pygame.font.Font(None,30) white = 255,255,255 image = pygame.image.load('pygame.gif') # 加载图片 image = pygame.transform.scale(image,(338,100)) # 图片有点大,将它缩小到宽338高100的大小 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.fill((0,0,255)) # 将界面设置为蓝色 x, y = pygame.mouse.get_pos() # 获取鼠标位置 textImage = myfont.render("x=%s,y=%s"%(x,y), True, white) screen.blit(textImage, (200,200)) circle_color = 255,255,0 circle_x = 100 circle_y = 50 circle_size = 30 line_width = 2 pygame.draw.circle(screen,circle_color,[circle_x,circle_y],circle_size,line_width) rect_color = 255,255,0 rect_x = 300 rect_y = 50 rect_width = 80 rect_height = 30 pygame.draw.rect(screen,rect_color,[rect_x,rect_y,rect_width,rect_height],line_width) line_color = 255,255,0 pygame.draw.line(screen,line_color,[50,120],[300,160], line_width) screen.blit(image, (40,250)) # 在界面上x=40,y=250的位置显示此图片 pygame.display.update() # 必须调用update才能看到绘图显示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。