新浦京81707con > 首页 > 葡京娱乐登录地址Python之文字转图片方法,使用

原标题:葡京娱乐登录地址Python之文字转图片方法,使用

浏览次数:59 时间:2019-10-04

运维结果

         差十分的少各类pygame程序均须求它,在它中得以做以下事件:

依赖函数式编制程序的代码

葡京娱乐登录地址 1

            a、处管事人件

游玩主类

#载入必要的模块
import pygame
#pygame初始化
pygame.init()
# 待转换文字
text = u"文字转图片"
#设置字体和字号
font = pygame.font.SysFont('Microsoft YaHei', 64)
#渲染图片,设置背景颜色和字体样式,前面的颜色是字体颜色
ftext = font.render(text, True, (65, 83, 130),(255, 255, 255))
#保存图片
pygame.image.save(ftext, "image.jpg")#图片保存地址

import sys

如上那篇使用pygame模块编写贪吃蛇的实例解说正是小编分享给我们的全部内容了,希望能给我们七个参照,也期望我们多多点拨脚本之家。

Pygame模块一览表:

            b、更新游戏状态

本人开掘本人并从未弄懂pygame的现实性画面更新机制,以及Smart的垄断(monopoly),对于贪吃蛇底部及身体的画面更新并从未做出来,还会有对人身和头的数据结构怎么着塑造并非很驾驭。毕竟第一遍,希望有大佬看见那篇博客能够指导下。

葡京娱乐登录地址 2

def hello_world():
    #别的pygame程序均须求实施此句进行模块初叶化
    pygame.init()
   
    #安装窗口的形式,(680,480)表示窗口像素,及(宽度,中度)
    #此函数重回一个Surface对象,本程序不行使它,故没封存
    pygame.display.set_mode((680, 480))
   
    #设置窗口标题
    pygame.display.set_caption('Hello World!')
   
    #巡回,直到接收到窗口关闭事件
    while True:
       
        #处总管件
        for event in pygame.event.get():
            #收下到窗口关闭事件
            if event.type == QUIT:
                #退出 www.2cto.com
                pygame.quit()
                sys.exit()
        #将Surface对象上帝绘制在荧屏上       
        pygame.display.update()

您或者感兴趣的作品:

  • 50行代码完成贪吃蛇(具体思路及代码)
  • Python编制程序pygame模块完成活动的小车示例代码
  • Python基于pygame模块播放DVD的点子亲自过问
  • python中pygame模块用法实例
  • 基于python中pygame模块的Linux下安装进程(详解)
  • python使用PyGame模块播放声音的方法

引进pygame模块 ,若本机未有请自行pip install pygame

if __name__ == "__main__":
    hello_world()
    测试:

import pygame
import time
import random
x = pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Slither')
gameDisplay.fill(white)
pygame.display.update()
block_size = 20
applethickness = 30
FPS = 10
icon = pygame.image.load('appleimage.jpg')
pygame.display.set_icon(icon)
img = pygame.image.load('snakeimage.jpg')
appleimage = pygame.image.load('appleimage.jpg')
clock = pygame.time.Clock()
smallfont = pygame.font.SysFont(None,30)
midfont = pygame.font.SysFont(None,50)
largefont = pygame.font.SysFont("comicsansms",80)
direction = "right"
def pause():
 paused = True
 message_to_screen("暂停",black,-100,"large")
 message_to_screen("空格暂停Q退出",black, 30)
 pygame.display.update()
 while paused:
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    pygame.quit()
    quit()
   if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_SPACE:
     paused = False
    elif event.key == pygame.K_q:
     pygame.quit()
     quit()
  #gameDisplay.fill(white)
 clock.tick(5)

def score(score):
 text1 = smallfont.render("Score : "   str(score), True, black)
 gameDisplay.blit(text1,(0,0))
def randApplegen():
 randAppleX = round(random.randrange(0,display_width-applethickness))#/10.0)*10.0
 randAppleY = round(random.randrange(0,display_height-applethickness))#/10.0)*10.0
 return randAppleX, randAppleY

def intro():
 intro = True
 message_to_screen("按c开始游戏",red,150)
 pygame.display.update()
 while intro:
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    pygame.quit()
    quit()
   if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_c:
     intro = False
    if event.key == pygame.K_q:
     pygame.quit()
     quit()
  clock.tick(15)
def snake(snakelist):
 for xny in snakelist[:-1]:
  pygame.draw.rect(gameDisplay,green,[xny[0],xny[1],block_size,block_size])
 if(direction == "right"):
  head = pygame.transform.rotate(img,270)
 elif(direction == "left"):
  head = pygame.transform.rotate(img,90)
 elif(direction == "up"):
  head = img
 elif(direction == "down"):
  head = pygame.transform.rotate(img,180)
 gameDisplay.blit(head,(snakelist[-1][0],snakelist[-1][1]))
def text_objects(text,color,size):
 if(size == "small"):
  textsurface = smallfont.render(text,True,color)
 elif(size == "middle"):
  textsurface = midfont.render(text,True,color)
 elif(size == "large"):
  textsurface = largefont.render(text,True,color)
 return textsurface, textsurface.get_rect()
def message_to_screen(msg,color,y_displace=0,size="small"):
 textsurf, textsurf_rect = text_objects(msg,color,size)
 textsurf_rect.center = (display_width/2), (display_height/2)   y_displace
 gameDisplay.blit(textsurf,textsurf_rect)

def gameLoop():
 global direction
 direction = "right"
 gameExit = False
 gameOver = False
 lead_x = display_width/2
 lead_y = display_height/2
 lead_x_change = 10
 lead_y_change = 0
 randAppleX, randAppleY = randApplegen()
 snakelist = []
 snakelength = 1
 while not gameExit:
  if gameOver == True:
   message_to_screen("Game Over",red,-50,"large")
   message_to_screen("Press C to play again or Q to quit the game",black,50,"small")
   pygame.display.update()
  while gameOver == True:
   #gameDisplay.fill(white)
   for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
     if event.key == pygame.K_q:
      gameExit = True
      gameOver = False
     if event.key == pygame.K_c:
      gameLoop()
      pygame.quit()
      quit()
    if event.type == pygame.QUIT:
     gameExit = True 
     gameOver = False
     # pygame.quit()
     # quit()
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    gameExit = True 
   if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT and direction != "right":
     direction = "left"
     lead_x_change = -block_size
     lead_y_change = 0
    elif event.key == pygame.K_RIGHT and direction != "left":
     direction = "right"
     lead_x_change = block_size
     lead_y_change = 0
    elif event.key == pygame.K_UP and direction != "down":
     direction = "up"
     lead_y_change = -block_size
     lead_x_change = 0
    elif event.key == pygame.K_DOWN and direction != "up":
     direction = "down"
     lead_y_change = block_size
     lead_x_change = 0
    elif event.key == pygame.K_SPACE:
     pause()
  if lead_x   block_size/2 >= display_width or lead_x < 0 or lead_y   block_size/2 >= display_height or lead_y < 0:
   gameOver = True
  lead_x  = lead_x_change
  lead_y  = lead_y_change
  gameDisplay.fill(white)
  gameDisplay.blit(appleimage,(randAppleX,randAppleY))
  #pygame.draw.rect(gameDisplay,red,[350,350,100,10])
  # gameDisplay.fill(red, rect=[200,200,50,50])  #good method
  snakehead = []
  snakehead.append(lead_x)
  snakehead.append(lead_y)
  snakelist.append(snakehead)
  if len(snakelist) > snakelength:
   del snakelist[0]
  for each in snakelist[:-1]:
   if each == snakehead:
    gameOver = True
  snake(snakelist)
  score(snakelength-1)
  pygame.display.update()
  if lead_x >= randAppleX and lead_x <= randAppleX   applethickness or lead_x   block_size >= randAppleX and lead_x   block_size <= randAppleX   applethickness:
   if lead_y >= randAppleY and lead_y <= randAppleY   applethickness or lead_y   block_size >= randAppleY and lead_y   block_size <= randAppleY   applethickness:
    randAppleX, randAppleY = randApplegen()
    snakelength  = 1;    
  clock.tick(FPS)
intro()
gameLoop()

pygame.quit()
quit()

...

至于本次demo

2、pygame.event.get()

python ### 刚学了python不久,发掘了贰个有意思的库pygame

本文由新浦京81707con发布于首页,转载请注明出处:葡京娱乐登录地址Python之文字转图片方法,使用

关键词: 新浦京81707con

上一篇:葡京娱乐:解决没有Win10推送图标问题,6提示0

下一篇:没有了