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

练习一:利用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
			

效果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值