python游戏编程入门-python游戏编程入门
《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同时被赋值为
这是一个有用的值,所以用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游戏编程入门-python游戏编程入门相关推荐
- python游戏编程入门书籍推荐-游戏编程入门书籍推荐:想要游戏编程尽快入门这些书不要错过...
游戏编程一直都是编程之中的一大热点,这也是由于游戏本身的火热造成的结果,所以每年都有很多人在关注着游戏编程.介于此小编今天就来将一些好的游戏编程入门书籍推荐给大家,希望对想要了解和学习游戏编程的朋友能 ...
- python游戏编程入门 免费-python游戏编程入门 python游戏编程入门课
python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...
- python点击按钮打开游戏_Python如何入门?直接按这个方式玩炸弹超人小游戏,就能掌握编程...
还在从基础知识点开始学python吗? 还不如直接玩python小游戏炸弹超人,跟着详细教程开始,从不会python到掌握python基础知识点,并且还有所有源码,想学不会编程都难. 下面讲一下开发P ...
- Python如何入门?直接按这个方式玩炸弹超人小游戏,就能掌握编程
还在从基础知识点开始学python吗? 还不如直接玩python小游戏炸弹超人,跟着详细教程开始,从不会python到掌握python基础知识点,并且还有所有源码,想学不会编程都难. 下面讲一下开发P ...
- python游戏使用教程_PYTHON游戏编程入门_IT教程网
资源名称:PYTHON游戏编程入门 内容简介: Python是一种解释型.面向对象.动态数据类型的高级程序设计语言,在游戏开发领域,Python也得到越来越广泛的应用,并由此受到重视. 本书教授用Py ...
- python游戏编程入门免费_python游戏编程入门 python游戏编程入门课
python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...
- Python编程教程 Python游戏课程 Python入门到精通视频
Python的修炼之道(入门语法.游戏开发.网络编程) 课程讲师:小强老师 课程分类:Python 适合人群:初级 课时数量:43课时 用到技术:Python 涉及项目:数据库应用程序开发 游戏开发 ...
- pygame 学习笔记(4)推荐一本python入门游戏书籍《PYTHON游戏编程入门》
简介 <PYTHON游戏编程入门>(More Python Programming for the Absolute Beginner)是 S.Harbour写的一本入门书籍,基于pyga ...
- python编程入门 pdf-PYTHON游戏编程入门 PDF 下载
相关截图: 图书简介: Python是一种解释型.面向对象.动态数据类型的高级程序设计语言,在游戏开发领域,Python也得到越来越广泛的应用,并由此受到重视. 本书教授用Python开发精彩游戏所需 ...
最新文章
- Pycharm报错合集:在pycharm运行anaconda配置的Pytorch环境报错(Environment location diretory is not empty )
- 复习计算机网络基础 day4--OSI与TCP/IP初步认识
- Unix系统编程()虚拟内存管理
- 控件属性、事件持久化
- LeetCode-动态规划基础题-343. 整数拆分
- Android 视频播放器 VideoView 的使用,播放本地视频 和 网络 视频
- C++ Strings(字符串)
- android 解决String.format多语言存在的问题
- XML电子口岸自动报关项目 真实百万级项目下载
- Spring Boot微服务,Docker和Kubernetes研讨会–第一部分
- docker nginx tomcat mysql_使用docker部署nginx+tomcat架构(2):访问mysql数据库
- 力扣669. 修剪二叉搜索树(JavaScript)
- json_serializable
- 多线程reactor模型
- 【java】抽象类下有两个具体子类,子类下有两个实例
- mysql 常用操作(整理)
- 略谈永中OFFICE的语言国际化
- 设计模式-第四篇之代理模式
- 两步搞定经验模态分解与离散小波变换
- 让机器人告别乱碰乱撞,激光导航让扫地机“睁开双眼”
热门文章
- [采坑记录] OneDrive同步失败 不能自动上传 不能同步 不能登陆
- 解决远程登录MYSQL数据库
- 几个.Net开源的CMS系统 (转)
- python语言程序设计嵩天-python语言程序设计基础(嵩天版),第二章程序练习题...
- 不从事编程、学python有用吗-为什么你觉得C语言什么都不能做,学了没用?不可能的...
- 自学python找工作工资-大四应届毕业生,学了两个月Python,找工作感觉好难啊?...
- 怎么安装python3-如何装python3
- python下载安装教程mac-Anaconda2 Mac版下载
- 电脑安装python步骤-windows10系统安装python的详细步骤
- 2!=5 or 0在python中是否正确-python 中 and or