《Hello World》python学习之pygame画图模块

python学习之pygame画图模块

1 安装

cmd中执行命令:pip install pygame
20170217-1.JPG
如果已经安装,则会告知“Requirement already satisified”。

2 功能

2.1 新建窗口

代码

import pygame
pygame.init()
screen = pygame.display.set_mode([640, 480])
running  = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

执行:

20170217-2.JPG
生成一个640*480大小的黑色窗口。

2.2 画图

代码

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])                               # fill screen with white    
pygame.draw.circle(screen, [255,0,0],[100,100], 30, 0)   # draw red circle
pygame.display.flip()
running  = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            running = False
pygame.quit()

执行

20170217-3.JPG

代码screen.fill([255,255,255])将画布设置为了白色。

注意:除了pygame可以画出很多形状,如aalines,arc,circle,ellipse,lines,polygon…且可以对其大小颜色等属性进行设置。

2.3 单个像素

可以用大量很小的圆或矩形形成一个像素点。

import pygame, sys
import math                                               
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255, 255, 255])


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            running  = False
    for x in range(0, 640):                                   
        y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240)  # sine wave formula

        # draw the wave using small rectangles
        pygame.draw.rect(screen, [0,0,0],[x, y, 1, 1], 1)     # x, y  is the location of each rectangle
                                                              #  in the wave
        pygame.display.flip()
        pygame.time.delay(30)
pygame.quit()

执行

20170217-4.JPG

2.4 图像

在屏幕上画图是一种方法,另一种是将照片添加在屏幕上。

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load("beach_ball.png")      # load the image from a file 
screen.blit(my_ball, [50, 50])                     # draw or 'blit' it to the screen
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

2.5 动画

在新的位置画出图形,再把原来的图形擦掉。

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load('beach_ball.png')
screen.blit(my_ball,[50, 50])                   # draw the beach ball
pygame.display.flip()
pygame.time.delay(2000)
screen.blit(my_ball, [150, 50])                 # draw it again, somewhere else

# "erase" the first beach ballimage
pygame.draw.rect(screen, [255,255,255], [50, 50, 90, 90], 0)   

pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            running  = False
pygame.quit()

2.6 在2D空间中反弹

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
x_speed = 10                             # now we have both x_speed
y_speed = 10                             #  and y_speed

running = True    
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            running = False

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x, y, 90, 90], 0)  
    x = x + x_speed                                 # move the ball hirizontally
    y = y + y_speed                                 #  and vertically              
    if x > screen.get_width() - 90  or  x < 0:      # bounce off the sides
        x_speed = - x_speed
    if y > screen.get_height() - 90 or y < 0:       # bounce off the top, bottom              
        y_speed = -y_speed                                        
    screen.blit(my_ball, [x, y])
    pygame.display.flip()

pygame.quit()

20170217-5.JPG

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值