ITPub博客

首页 > 应用开发 > IT综合 > 与小卡特一起学python 第16章 图形 Pygame学习

与小卡特一起学python 第16章 图形 Pygame学习

原创 IT综合 作者:yarking207 时间:2016-04-07 23:49:55 0 删除 编辑
#16章图形
#16-1创建一个PYgame窗口
import pygame
pygame.init()
screen = pygame.display.set_mode([1024,768])

#16-2 使得窗口正常工作
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()

#16-3 画一个圆
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill ([255,255,255])#背景填充为白色
pygame.draw.circle(screen,[255,0,0],[100,100],30,0)#画圆开始
pygame.draw.rect(screen,[0,255,0],[250,150,300,200],2)
pygame.display.flip()#

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()
#16-5 使用DRAW.RECT实现艺术创作
import pygame,sys,random
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255]) #背景填充为白色
for i in range(100):
    width = random.randint(0,250)
    heigh = random.randint(0,100)
    top = random.randint(0,400)
    left = random.randint(0,500)
    pygame.draw.rect(screen,[0,0,0],[left,top,width,heigh],1)
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-6 带颜色的现代艺术
import pygame,sys,random
from pygame.color import THECOLORS
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
for i in range(100):
    width = random.randint(0,250);height = random.randint(0,100)
    top = random.randint(0,400);left = random.randint(0,500)
#   color_name = random.choice(THECOLORS.keys()) 书中的随机颜色调用出错,
    color_name = "yellow" #直接赋值则程序正常。查资料是因为python3的兼容问题
    color = THECOLORS[color_name]
    line_width = random.randint(1,3)
    pygame.draw.rect(screen,color,[left,top,width,height],line_width)
pygame.display.flip()

#16-7  用大量很小的矩形画曲线
import pygame,sys
import math #导入数学函数,包括sin()
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
for x in range(0,640):#从左到右循环0-639
    y = int(math.sin(x/640.0 *4 * math.pi) * 200 + 240)#计算每个点的Y坐标
    pygame.draw.rect(screen,[0,0,0],[x,y,1,1],1)#使用小矩形来画点
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()



#16-8 一条完美的正弦曲线
import pygame,sys
import math #导入数学函数,包括sin()
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
plotPoints = [] #定义列表
for x in range(0,640):#从左到右循环0-639
    y = int(math.sin(x/640.0 *4 * math.pi) * 200 + 240)#计算每个点的Y坐标
    plotPoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotPoints,1)#使用小矩形来画点,lines
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-9 连连看神秘的图片

import pygame,sys
pygame.init()

dots = [[221,432],[225,331],[133,342],[141,310],
        [51,230],[74,217],[58,153],[114,164],
        [123,135],[176,190],[159,77],[193,93],
        [230,28],[267,93],[301,77],[284,190],
        [327,135],[336,164],[402,153],[386,217],
        [409,230],[319,310],[327,342],[233,331],
        [237,432]]

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.lines(screen,[255,0,0],True,dots,5)
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()


#16-10 在PYgame窗口中显示沙滩球图像
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])

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


#16-11 移动沙滩球
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])
pygame.display.flip()
pygame.time.delay(2000)# 延时
screen.blit(my_ball,[150,50])#显示的新位置
pygame.display.flip()#完成移动
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-12 移动沙滩球后把原来图形擦掉
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])
pygame.display.flip()
pygame.time.delay(2000)
screen.blit(my_ball,[400,50])
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()

#16-13 更流畅的移动沙滩球图像
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
screen.blit(my_ball,[x,y])
pygame.display.flip()
for looper in range (1,100):#循环
    pygame.time.delay(20) #时间改为20
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    x = x + 5
    screen.blit(my_ball,[x , y])
    pygame.display.flip()

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


#16-14 让沙滩球在左右边框反弹
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 = 2 #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
    if x > screen.get_width() - 90 or x < 0:
        x_speed = - x_speed #变方向
    screen.blit(my_ball,[x,y])
    pygame.display.flip()
pygame.quit()


#16-15 在2D空间中让沙滩球反弹
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([800,600])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
x_speed = 20
y_speed = 20 #增加垂直运动
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.time.delay(50)
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    x = x + x_speed
    y = y + y_speed
    if x > screen.get_width() - 90 or x< 0:
        x_speed = -x_speed
    if y > screen.get_height() - 90 or y <0:#让球在窗口顶边或者底边反弹
        y_speed = -y_speed
    screen.blit(my_ball,[x,y])
    pygame.display.flip()
pygame.quit()

#16-16 让球翻转
import  pygame,sys
pygame.init()
screen = pygame.display.set_mode([800,600])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
x_speed = 5
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.time.delay(50)
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    y = y + x_speed
    if y > screen.get_height():#球开始落下height
        y = 0 #从顶部开始落下
    screen.blit(my_ball,[x,y])
    pygame.display.flip()
pygame.quit()

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/220205/viewspace-2077162/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-08-03

  • 博文量
    65
  • 访问量
    99301