与小孩一起学编程16章

与孩子一起学编程16章
今天,要用到一个python module了,网上google pygame,选择合适的版本安装。然后在交互模式中检查
>>> import pygame
>>> 

如果不显示错误信息,就说明已安装好pygame module了,然后本章的所有的代码都要用到这个module。

先来创建一个窗口

import pygame
pygame.init()
screen = pygame.display.set_mode([640, 480])
试试看吧,然后,怎么关闭窗口呢,windows中的红叉怎么运行呢
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
然后,就要在这个窗口里填一些东西了,画一个红心圆
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.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
如果想让这个圆换一个坐标位置,修改哪个可以呢
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255])
pygame.draw.circle(screen, [255,0,0],[320,240], 30, 0)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
看到没,修改了一个坐标后,红心圆的位置就改变了,接着猜一猜另外几个参数是起什么作用的呢。google pygame,draw,circle()函数的用法和参数传递。

用pygame做一个现代艺术会不会很有意思啊,呵呵,试试看

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()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
接着在写个代码,有什么不同,可以自己运行看看
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())
    line_width = random.randint(1, 3)
    pygame.draw.rect(screen, [0,0,0], [left, top, width, height],
                     line_width)
    

pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
还可以做一些其他的图形,自己参考pygame文档,写一个正弦曲线
import pygame, sys
import math
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255])
for x in range (0, 640):
    y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240)
    pygame.draw.rect(screen, [0,0,0],[x,y, 1, 1], 1)
    

pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
还可以让它更完美些,接着来
import pygame, sys
import math
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255])
plotPoints = []
for x in range (0, 640):
    y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240)
    plotPoints.append([x,y])
    
pygame.draw.lines(screen, [0,0,0],False, plotPoints, 2)
    

pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
如果我想用一些图片,怎么加到这个窗口里呢,pygame中的image函数就可以搞定的,在pygame窗口中显示一个网上download的一个沙滩球,要让代码和图片在同一个文件夹中,才能让下面的代码运行成功beach_ball.png与小孩一起学编程16章,文件名要和代码里一致。
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()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
那怎么让它动起来呢,移动位置,上面介绍过坐标位置
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()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
运行完后,发现了什么了吗,球会东了,但是原来的图像,还是保留着呢,这怎么办呢?图像移动需要在新位置画出图形,然后把原来的图形擦掉。
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.draw.rect(screen, [255,255,255], [50, 50, 90, 90], 0)
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
如果移动一个位置不过瘾的话,再来流畅的移动沙滩球
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)
    pygame.draw.rect(screen, [255,255,255], [x, y, 90, 90], 0)
    x = x + 5
    screen.blit(my_ball,[x, y])
    pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
如果想让球反弹呢,怎么处理
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

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    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()
试试看,挺有意思的,呵呵,让它动的更欢快的
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
y_speed = 10

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    pygame.time.delay(20)
    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()
最后,让它从开始的地方,重新出现,循环出现
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 = 5

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    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():
        x = 0
    screen.blit(my_ball, [x, y])
    pygame.display.flip()
还有其他动作,你都可以试试,修改代码,让它跳舞吧!