#代码清单16-1创建一个PYgame窗口
import pygame
pygame.init()
screen = pygame.display.set_mode([640,480])
#代码清单16-2 使得窗口正常工作
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])#背景填充为白色(事件1-Pygame表面)
pygame.draw.circle(screen,[255,0,0],[100,100],30,0)#画圆开始
#代码清单16-4 把圆放在窗口中间
pygame.draw.circle(screen,[255,0,0],[320,240],50,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 = random.choice(['red','green','blue','grey','gray','pink','brown',\
'cyan','orange','yellow','black'])
#color_name = "yellow" #直接赋值则程序正常。查资料是因为python3的兼容问题
color = THECOLORS[color_name]
line_width = random.randint(1,5)
pygame.draw.rect(screen,color,[left,top,width,height],line_width)
pygame.display.flip()
running=True
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
#代码清单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,2)#使用小矩形来画点,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 = 15 #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()
# TIO_CH16_1.py
# Copyright Warren & Carter Sande, 2013
# Released under MIT license http://www.opensource.org/licenses/mit-license.php
# Version $version ----------------------------
# Answer to Try It Out Question 1 in Chapter 16
import pygame, sys
pygame.init()
screen=pygame.display.set_mode((640, 480))
screen.fill((250, 120, 0))
pygame.draw.arc(screen, (255, 255, 0), pygame.rect.Rect(43, 368, 277, 235), -6.25, 0, 15)
pygame.draw.rect(screen, (255, 0, 0), pygame.rect.Rect(334, 191, 190, 290))
pygame.draw.rect(screen, (128, 64, 0), pygame.rect.Rect(391, 349, 76, 132))
pygame.draw.line(screen, (0, 255, 0), (268, 259), (438, 84), 25)
pygame.draw.line(screen, (0, 255, 0), (578, 259), (438, 84), 25)
pygame.draw.circle(screen, (0, 0, 0), (452, 409), 11, 2)
pygame.draw.polygon(screen, (0, 0, 255), [(39, 39), (44, 136), (59, 136), (60, 102), (92, 102), (94, 131), (107, 141), (111, 50), (97, 50), (93, 86), (60, 82), (58, 38)], 5)
pygame.draw.rect(screen, (0, 0, 255), pygame.rect.Rect(143, 90, 23, 63), 5)
pygame.draw.circle(screen, (0, 0, 255), (153, 60), 15, 5)
clock = pygame.time.Clock()
pygame.display.flip()
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
pygame.quit()
# TIO_CH16-5.py
# Copyright Warren & Carter Sande, 2013
# Released under MIT license http://www.opensource.org/licenses/mit-license.php
# Version $version ----------------------------
# Answer to Try It Out Question 5 in Chapter 16
# Put the display.flip inside the while loop
# and add a delay
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)
height = random.randint(0, 100)
top = random.randint(0, 400)
left = random.randint(0, 500)
pygame.draw.rect(screen, [0,0,0], [left, top, width, height], 1)
pygame.display.flip()
pygame.time.delay(30) #增加延迟30秒
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
#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()
pygame.time.delay(30) #增加延迟30秒
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()