《Python游戏编程入门》
这些文章负责整理在这本书中的知识点、注意事项和课后习题的尝试实现。
并且对每一个章节给出的最终实例进行分析和注释。

初识pygame:pie游戏
pygame游戏库使得如下功能成为可能:绘制图形、获取用户输入、执行动画
以及使用定时器让游戏按照稳定的帧速率运行。
使用pygame库;
以一定字体打印文本;
使用循环来重复动作;
绘制圆、矩形、线条和户型;
创建pie游戏;

从哪里获得pygame库:http://www.pygame.org/download.shtml
我现在使用的Python2.7和pygame1.9
书中使用的环境是Python3.2和pygame1.9
现在不在Python3的环境下安装上pip工具导致环境无法一致

pygame库的初始化工作:
    import pygame
    from pygame.locals import *
    pygame.init()

创建一个(600,500)大小的屏幕
    screen=pygame.display.set_mode((600,500))
    screen同时被赋值为<Surface(600x500x32 SW)>
    这是一个有用的值,所以用screen变量存储。

打印文本
    1、创建字体对象
    myfont=pygame.font.Font(None,60)
    None:使用默认字体
    60:字体大小
    2、创建一个可以使用screen.blit()绘制的平面
    textimage=myfont.render("Hello Python",True,(255,255,255))
    render需要三个参数,需要被显示的字符串、是否抗锯齿True/False、颜色
    3、将textimage交给screen.blit()进行绘制
    screen.blit(textimage,(100,100))
    screen.blit()需要两个参数,绘制的对象及其(左上角顶点)坐标

背景填充
    screen.fill((0,0,0))
    screen.fill()需要给出背景颜色

刷新显示
    screen.display.update()
    一般配合while循环使用

while循环
    通过while循环可以进行事件处理和持续的屏幕刷新
    while True:
        for event in pygame.event.get():
            if event.type in (QUIT,KEYDOWN):
                sys.exit()
        screen.display.update()

绘制圆形
    pygame.draw.circle(screen,color,position,radius,width)
    color    (0,0,0)给定颜色
    radius圆半径
    position     (0,0)给定圆心坐标
    width线条宽度

绘制矩形
    pygame.draw.rect(screen,color,position,width)
    position      (pos_x,pos_y,100,100)给定左上角顶点的坐标、长和宽

绘制线条
    pygame.draw.line(screen,color,(0,0),(100,100),width)
    (0,0)(100,100)负责给定线段的两个端点

绘制弧形
    start_angle=math.radians(0)
    end_angle=math.radians(90)
    position=x-radius,y-radius,radius*2,radius*2
    #x,y表示弧形所在的圆的圆心坐标,radius表示半径
    pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
    start_angle起始角度 指向正右侧的半径开始逆时针旋转就是0到360
    end_angle结束角度

两段值得学习的示例
1、绘制移动矩形
#!/usr/bin/python

import sys
import random
from random import randint
import pygame
from pygame import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
color=100,100,100            
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()

screen.fill((0,0,200))
    
    pos_x +=vel_x
    pos_y +=vel_y
    if pos_x>500 or pos_x<0:
        vel_x=-vel_x
        rand1=randint(0,255)
        rand2=randint(0,255)
        rand3=randint(0,255)
        color=rand1,rand2,rand3
    if pos_y>400 or pos_y<0:
        vel_y=-vel_y
        rand1=randint(0,255)
        rand2=randint(0,255)
        rand3=randint(0,255)
        color=rand1,rand2,rand3
    width=0
    pos=pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)

pygame.display.update()

2、pie游戏
#!/usr/bin/python

#init
import sys
import math
import pygame
from pygame.locals import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("The Pie Game-Press 1,2,3,4")
myfont=pygame.font.Font(None,60)

color=200,80,60
width=4
x=300
y=250
radius=200
position=x-radius,y-radius,radius*2,radius*2

piece1=False
piece2=False
piece3=False
piece4=False

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            sys.exit()
        elif event.type==KEYUP:
            if event.key==pygame.K_ESCAPE:
                sys.exit()
            elif event.key==pygame.K_1:
                piece1=True
            elif event.key==pygame.K_2:
                piece2=True
            elif event.key==pygame.K_3:
                piece3=True
            elif event.key==pygame.K_4:
                piece4=True

screen.fill((0,0,200))

#draw the four numbers
    textimage1=myfont.render("1",True,color)
    screen.blit(textimage1,(x+radius/2-20,y-radius/2))    
    textimage2=myfont.render("2",True,color)
    screen.blit(textimage2,(x-radius/2,y-radius/2))
    textimage3=myfont.render("3",True,color)
    screen.blit(textimage3,(x-radius/2,y+radius/2-20))
    textimage4=myfont.render("4",True,color)
    screen.blit(textimage4,(x+radius/2-20,y+radius/2-20))

#draw arc,line
    if piece1:
        start_angle=math.radians(0)
        end_angle=math.radians(90)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
        pygame.draw.line(screen,color,(x,y),(x,y-radius),width)

if piece2:    
        start_angle=math.radians(90)
        end_angle=math.radians(180)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
        pygame.draw.line(screen,color,(x,y),(x-radius,y),width)

if piece3:
        start_angle=math.radians(180)
        end_angle=math.radians(270)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x-radius,y),width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)

if piece4:
        start_angle=math.radians(270)
        end_angle=math.radians(360)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)
        pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
        
    #if success,display green
    if piece1 and piece2 and piece3 and piece4:
        color=0,255,0

pygame.display.update()

挑战
1、绘制椭圆
#!/usr/bin/python

import sys
import pygame
from pygame.locals import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Ellipse")

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

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

color=255,0,255
        position=100,100,400,300
        width=8
        pygame.draw.ellipse(screen,color,position,width)

pygame.display.update()
这个题目就是让你认识一下pygame.draw.ellipse()函数的相关使用。
该函数和pygame.draw.rect()函数使用方式十分相似。

2、随机的绘制1000个线条
#!/usr/bin/python
import random
from random import randint
import sys
import pygame
from pygame import *
pygame.init()

screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("Drawing Line")

screen.fill((0,80,0))
color=100,255,200
width=2
for i in range(1,1001):
        pygame.draw.line(screen,color,(randint(0,800),randint(0,600)),(randint(0,800),randint(0,600)),width)

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

pygame.display.update()

通过这个题目理解了如果绘制图形和刷新显示都在循环中时,while True循环每次都会绘
制图形并刷新显示。
调用pygame模块中的randint()函数。
而在while True循环外绘制图形,则图形绘制完成之后保持不变。刷新显示的是一个已经绘制好
的图形。

3、修改矩形程序,使矩形碰到屏幕边界是,矩形会改变颜色

#!/usr/bin/python

import sys
import random
from random import randint
import pygame
from pygame import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3            
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()

screen.fill((0,0,200))
    
    pos_x +=vel_x
    pos_y +=vel_y
    if pos_x>500 or pos_x<0:
        vel_x=-vel_x
        rand1=randint(0,255)
        rand2=randint(0,255)
        rand3=randint(0,255)
        color=rand1,rand2,rand3
    if pos_y>400 or pos_y<0:
        vel_y=-vel_y
        rand1=randint(0,255)
        rand2=randint(0,255)
        rand3=randint(0,255)
        color=rand1,rand2,rand3
    width=0
    pos=pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)

pygame.display.update()

这里需要用到random模块。在每次碰到屏幕边界时,不仅改变矩形的运动方向,而且使用随机数改变矩形的颜色。
也可以先将color设置为定值,可以少写三行代码。

底层(嵌套的层数较多)代码块初次使用的变量在顶层代码块中依然生效。
以上问题属于变量的作用域问题。说明我在这一方面认识不够清晰。

Python游戏编程入门的更多相关文章

  1. Python游戏编程入门 中文pdf扫描版|网盘下载内附地址提取码|

    Python是一种解释型.面向对象.动态数据类型的程序设计语言,在游戏开发领域,Python也得到越来越广泛的应用,并由此受到重视. 本书教授用Python开发精彩游戏所需的[]为重要的该你那.本书不 ...

  2. Python游戏编程入门2

    I/O.数据和字体:Trivia游戏 本章包括如下内容:Python数据类型获取用户输入处理异常Mad Lib游戏操作文本文件操作二进制文件Trivia游戏 其他的不说,我先去自己学习文件类型和字符串 ...

  3. python编程学习--Pygame - Python游戏编程入门(0)---转载

    原文地址:https://www.cnblogs.com/wuzhanpeng/p/4261015.html 引言 博客刚开,想把最近学习的东西记录下来,算是一种笔记.最近打算开始学习Python,因 ...

  4. Pygame - Python游戏编程入门(0) 转

    博客刚开,想把最近学习的东西记录下来,算是一种笔记.最近打算开始学习Python,因为我感觉Python是一门很有意思的语言,很早以前就想学了(碍于懒),它的功能很强大,你可以用它来做科学运算,或者数 ...

  5. Pygame - Python游戏编程入门

    >>> import pygame>>> print(pygame.ver)1.9.2a0 如果没有报错,应该是安装好了~ 如果报错找不到模块,很可能是安装版本的问 ...

  6. Python游戏编程入门4

    Math和Graphics:Analog Clock示例程序本章介绍Python的math模块,该模块可以执行计算,如常见的三角正弦函数.余弦函数.正切函数等. 使用正弦和余弦函数绘制圆创建Anlog ...

  7. Python游戏编程入门3

    用户输入:Bomb Catcher游戏本章介绍使用键盘和鼠标获得用户输入.包括如下主题:学习pygame事件学习实时循环学习键盘和鼠标事件学习轮询键盘和鼠标的状态编写Bomb Catcher游戏 1本 ...

  8. PC游戏编程(入门篇)(前言写的很不错)

    PC游戏编程(入门篇) 第一章 基石 1. 1 BOSS登场--GAF简介 第二章 2D图形程式初体验 2.l 饮水思源--第一个"游戏"程式 2.2 知其所以然一一2D图形学基础 ...

  9. 分享《Python 游戏编程快速上手(第3版)》高清中文版PDF+高清英文版PDF+源代码

    通过编写一个个小巧.有趣的游戏来学习Python,通过实例来解释编程的原理的方式.14个游戏程序和示例,介绍了Python基础知识.数据类型.函数.流程控制.程序调试.流程图设计.字符串操作.列表和字 ...

随机推荐

  1. CAS单点登录入门

    一.单点登录简介 SOO是现在企业比较流行的业务整合解决方案之一,定义解决登录,可以应用在不同系统中,用户只需要登录一次,就可以访问所有相互信任的应用系统(模块开发.同家公司不同产品等等),例如百度, ...

  2. 2017(4)数据库系统,分布式数据库,NoSQL,反规范化

    试题四(共 25 分) 阅读以下关于数据库分析与建模的叙述,在答题纸上回答问题 1至问题 3. [说明] 某电子商务企业随着业务不断发展,销售订单不断增加,每月订单超过了 50 万笔,急需开发一套新的 ...

  3. PHPExcel 读取 xls

    <?php $xlsPath = './test.xls'; //指定要读取的exls路径 //$type = 'Excel2007'; //设置要解析的Excel类型 Excel5(2003或 ...

  4. Python加密保护-对可执行的exe进行保护

    Python 是一种面向对象的解释型计算机程序设计语言,Python 语言写的程序不需要编译成二进制代码,可以直接从源代码运行程序. 在计算机内部,Python解释器把源代码转换成称为字节的中间形式, ...

  5. T-SQL语言基础(1)之理论背景

    从学校就开始接触和使用 SQL 了,但一直没有怎么细细去了解它,最近入职的公司比较重 T-SQL 部分,所以就准备系统的学习一下. 买了一本<Microsoft SQL Server 2008 ...

  6. css实现礼券效果

    <template> <div class="demo"> <div class="stamp stamp01"> < ...

  7. TCP 服务端接收数据解析工具类

    package com.ivchat.common.util; import java.io.BufferedReader;import java.io.IOException;import java ...

  8. java生成二维码工具类

    package com.runtime.extend.utils.CodeCreate; import com.google.zxing.*;import com.google.zxing.commo ...

  9. java-redis

    pom.xml添加如下配置: <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  10. 漫谈GUI开发—各种平台UI开发概况

    前言: 在看这边文章前,可以建议看下:图形界面操作系统发展史——计算机界面发展历史回顾 从CS到BS,现在的前端开发,其实也是GUI开发范畴.现今 各平台的UI开发概况 HTML&CSS,Wi ...