python中pygame画图_Python--写游戏pygame入门二(屏幕上画圆,矩形,椭圆)

1、建立一个窗口

import pygame

#模块初始化

pygame.init()

#创建一个窗口,窗口大小为640*480

screen=pygame.display.set_mode([640,480])

#定义窗口的标题为'Draw'

pygame.display.set_caption('Draw')

#用白色填充窗口

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

2、退出窗口

while True:

for event in pygame.event.get():

if event.type==pygame.QUIT:

sys.exit()

event.get()函数获取事件队列,即把捕获到的事件放入一个队列,然后一一执行。

3、画圆,矩形,椭圆

说明文档链接如下:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

4、当按下键盘上的按键,在频幕上随机画出圆,矩形或者椭圆,按【退出键】退出程序

#!usr/bin/env python

#coding=utf-8

import pygame

import sys

import random

screen_size = (640, 480)

backgroundcolor = (255, 127, 255)

#pygame初始化

pygame.init()

#创建一个窗口

screen = pygame.display.set_mode(screen_size, 0, 32)

pygame.display.set_caption('Draw rect and circle')

#背景填充

screen.fill(backgroundcolor)

while True:

for event in pygame.event.get():

#按下关闭按钮,退出程序

if event.type==pygame.QUIT:

sys.exit()

#按下键盘上的任意键,在屏幕上画图

elif event.type == pygame.KEYDOWN:

i = random.randint(0, 2)

drawcolor = (random.randint(0,255),random.randint(0,255),random.randint(0,255))

top = random.randint(0,400)

left = random.randint(0,500)

width = random.randint(0,5)

#画圆

if i == 0:

radiu = random.randint(width,100)

pygame.draw.circle(screen, drawcolor, [top, left], radiu, width)

#画矩形

elif i == 1:

rectwidth = random.randint(0,255)

rectheight = random.randint(0,100)

pygame.draw.rect(screen, drawcolor,[left, top, rectwidth, rectheight], width)

#画椭圆

else:

try:

rectwidth = random.randint(0,255)

rectheight = random.randint(0,100)

pygame.draw.ellipse(screen, drawcolor, [left, top, rectwidth, rectheight], width)

except ValueError:

print 'ellipse'

pass

#重画屏幕

pygame.display.flip()

5、效果图

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#模块导入 import pygame,sys from pygame.locals import* #初始化pygame pygame.init() #设置窗口大小,单位是像素 screen = pygame.display.set_mode((500,400)) #设置背景颜色 screen.fill((0,0,0)) #设置窗口标题 pygame.display.set_caption("你好,我的朋友") # 绘制一条线 pygame.draw.rect(screen, (0,0,0), [0,100,70,40]) #加载图片 img = pygame.image.load("panda.jpg") #初始化图片位置 imgx = 0 imgy = 10 #加载和播放音频 sound = pygame.mixer.Sound('Sound_Of_The_Sea.ogg') sound.play() #加载背景音乐 pygame.mixer.music.load('TEST1.mp3') #播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第个参数是设置播放的起点(单位为秒) pygame.mixer.music.play(-1, 30.0) #导入文字格式 fontt=pygame.font.Font(None,50) #配置文字 tex=fontt.render("It is boring!!!",True,(0,0,128),(0,255,0)) #显示文字及坐标 texr=tex.get_rect() texr.center=(10,250) #初始化方向 dire = "right" #设置循环 while 1: #绘制文字 screen.blit(tex,texr) screen.fill((0,0,0)) screen.blit(img,(imgx,imgy)) if dire == "right": imgx+=5 if imgx == 380: dire = 'down' elif dire == 'down': imgy += 5 if imgy == 300: dire = 'left' elif dire == 'left': imgx -= 5 if imgx == 10: dire = 'up' elif dire == 'up': imgy -= 5 if imgy == 10: dire = 'right' #获取事件 for ss in pygame.event.get(): #判断事件 if ss.type == QUIT: #退出Pygame pygame.quit() #退出系统 sys.exit() #绘制屏幕内容 pygame.display.update() #设置帧率 pygame.time.delay(10)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值