2.使用Pygame

使用pygame

我们可以使用pygame在屏幕当中绘制一个文本出来

import pygame
from pygame.locals import*
import sys

pygame.init()

screen = pygame.display.set_mode((400,300))

#绘制文本
myfont = pygame.font.Font(None,40)  #创建文字对象
textimage = myfont.render("hello,world",True,(255,255,255))   #将文字渲染为图片   

screen.fill((0,0,255))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    screen.blit(textimage,(100,100))    #将文本对象绘制在屏幕中
    pygame.display.update() #对屏幕进行刷新

以下为绘制几个图形的代码

#绘制圆形
pygame.draw.circle(screen,color,position,radius,width)

#绘制矩形
pygame.draw.rect(screen,color,pos,width)

#绘制线条
pygame.draw.line(screen,color,start_position,end_position,width)

#绘制弧形
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)

PIE游戏的制作

游戏规则非常简单,屏幕上会出现1,2,3,4四个数字,

  • 按下对应的数字键则会画出1/4的圆出来,
  • 按下数字键5时,重置游戏
  • 按下键盘ESC时,退出游戏

以下为游戏的代码

import pygame
from pygame.locals import *
import sys
import math

pygame.init()   #初始化pygame

screen = pygame.display.set_mode((600,500)) #设置窗口大小
pygame.display.set_caption("The Pie Game")    #设置窗口标题

myfont = pygame.font.Font(None,60)  #创建文字对象
textcolor = (255,255,255)           #创建文字颜色
x = 300     #设置圆中心位置
y = 250
width = 5   #设置宽度

radius = 200    #设置圆的半径
screen.fill((0,0,255))  #填充屏幕为蓝色
rect = (x-radius,y-radius,2*radius,2*radius)    #设置弧形的矩形范围

#设置标志
pie1 = False
pie2 = False
pie3 = False
pie4 = False

#绘制文字
def draw_text():
    textimag1 = myfont.render("1",True,textcolor)
    screen.blit(textimag1,(x + radius/2,y-radius/2))
    textimag2 = myfont.render("2",True,textcolor)
    screen.blit(textimag2,(x - radius/2,y-radius/2))
    textimag3 = myfont.render("3",True,textcolor)
    screen.blit(textimag3,(x - radius/2,y+radius/2))    
    textimag4 = myfont.render("4",True,textcolor)
    screen.blit(textimag4,(x + radius/2,y+radius/2))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_1:
                pie1 = True
            elif event.key == pygame.K_2:
                pie2 = True
            elif event.key == pygame.K_3:
                pie3 = True
            elif event.key == pygame.K_4:
                pie4 = True
            elif event.key == pygame.K_5:   #按下5重新开始游戏
                pie1 = False
                pie2 = False
                pie3 = False
                pie4 = False
                screen.fill((0,0,255))
            elif event.key == pygame.K_ESCAPE:  #按下ESC退出游戏
                sys.exit()

    #绘制文字
    draw_text()

    if pie1:
        start_angle = math.radians(0)
        end_angle = math.radians(90)
        pygame.draw.arc(screen,textcolor,rect,start_angle,end_angle,width)  #绘制弧形
        pygame.draw.line(screen,textcolor,(x,y),(x+radius,y),width) #绘制直线
        pygame.draw.line(screen,textcolor,(x,y),(x,y-radius),width) #绘制直线
    if pie2:
        start_angle = math.radians(90)
        end_angle = math.radians(180)
        pygame.draw.arc(screen,textcolor,rect,start_angle,end_angle,width)
        pygame.draw.line(screen,textcolor,(x,y),(x-radius,y),width)
        pygame.draw.line(screen,textcolor,(x,y),(x,y-radius),width)
    if pie3:
        start_angle = math.radians(180)
        end_angle = math.radians(270)
        pygame.draw.arc(screen,textcolor,rect,start_angle,end_angle,width)
        pygame.draw.line(screen,textcolor,(x,y),(x-radius,y),width)
        pygame.draw.line(screen,textcolor,(x,y),(x,y+radius),width)
    if pie4:
        start_angle = math.radians(270)
        end_angle = math.radians(360)
        pygame.draw.arc(screen,textcolor,rect,start_angle,end_angle,width)
        pygame.draw.line(screen,textcolor,(x,y),(x+radius,y),width)
        pygame.draw.line(screen,textcolor,(x,y),(x,y+radius),width)

    pygame.display.update()
图片[1]-2.使用Pygame-四曲博客
© 版权声明
THE END
喜欢就支持以下吧
点赞0
分享
评论 抢沙发
四曲的头像-四曲博客

昵称

取消
昵称表情代码图片