您的位置:首页 > 其它

Pygame 学习练习(三):画图?

2014-03-05 23:04 260 查看

练习一:利用randint()函数随机产生区间大小的数字,座标随机,半径随机,颜色随机

#!/usr/bin/pytho
#-*-coding:utf-8-*-
#代码目的: 利用randint()函数产生随机数,然后即可作为随机坐标,然后在画布上随机任意位置花任意大小的圆形
import pygame
from random import * # 这个主要为了生产出随机数
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,480),0,32)
pygame.display.set_caption("Testing random and draw ")

#
for i in range(0,100):
#利用randint()函数随机产生区间大小的数字,座标随机,半径随机,颜色随机
circle_postion = (randint(0,800),randint(0,480))
radius = randint(0,160)
circle_color = (randint(0,255),randint(0,255),randint(0,255))
pygame.draw.circle(screen,circle_color,circle_postion,radius)
#pygame.draw.circle()为了在画布(surface对象上画圆形
#circle(Surface, color, pos, radius, width=0) -> Rect
pygame.display.update()

while True:
for event in pygame.event.get():
#事件测试,获取事件队列里面的事件
if event.type == QUIT:
#当事件类型为退出时候,一般是按下X图标时候
over_font = pygame.font.SysFont("",32)
#产生字体Font对象
font_surface = over_font.render("Game Over!",True,(255,255,255))
#由字体产生surface对象
screen.blit(font_surface,(380,220))
#把字体对象贴到画布上,就是在屏幕上现实Game Over
pygame.display.update()
#当然,需要更新画布才看得见
else:
pass


效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: