一、pygame入门
0、安装Pygame
$ python -m pip install --user pygame
1、用pygame画一个点
# ShowDot.py
import pygame #imports the package with all the available pygame modules.
pygame.init() #initializes each of these modules.
#create a graphical window,也就是
#creates a new Surface object that represents the actual displayed graphics.
screen = pygame.display.set_mode([800,600])
keep_going = True
GREEN = (0,255,0) # RGB color triplet for GREEN
radius = 50
while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
pygame.draw.circle(screen, GREEN, (100,100), radius)
pygame.display.update()
pygame.quit()
2、Pygame中的新内容
- 新的坐标系统
- 要用循环来开发pygame游戏
pygame需要游戏循环持续更新屏幕并处理事件。
- 处理事件
pygame.event.get()获取事件列表,循环这些事件,使用if判断某个事件发生了,然后调用处理事件的函数。
3、游戏的主要构成
我们做一个显示笑脸图片的程序,笑脸如下:
设置
# ShowPic.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
创建游戏循环
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
screen.blit(pic, (100,100))
pygame.display.update()
退出程序
pygame.quit() # Exit
整合
确保程序和图片在同一目录下
# ShowPic.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
screen.blit(pic, (100,100))
pygame.display.update()
pygame.quit() # Exit
二、时间刚刚好-----移动和弹跳
在窗口上绘制一次称为一帧。
我们的目标是创建一秒60帧的动画,让动画看起来很平滑。
1、移动笑脸
# SmileyMove.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([600,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
#下面两行代码确保图片黑色边角是透明的
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
#BLACK = (0,0,0)
#timer = pygame.time.Clock() # Timer for animation
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += 1 # Move the picture
picy += 1
#screen.fill(BLACK) # Clear screen
screen.blit(pic, (picx,picy))
pygame.display.update()
#timer.tick(60) # Limit to 60 frames per second
pygame.quit() # Exit
存在两个问题,一个是有移动痕迹,一个是移动太快。
问题一解决方法是绘制新位置笑脸之前用背景色填充screen界面,那么屏幕界面的所有像素都成了背景色。
问题二解决方法是添加一个时钟对象,控制窗口更新速率。
修改后的代码如下:
# SmileyMove.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([600,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
#下面两行代码确保图片黑色边角是透明的
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock() # Timer for animation
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += 1 # Move the picture
picy += 1
screen.fill(BLACK) # Clear screen
screen.blit(pic, (picx,picy))
pygame.display.update()
timer.tick(60) # Limit to 60 frames per second
pygame.quit() # Exit
2、将笑脸从墙上弹开
碰撞检测:笑脸的位置是否到达了屏幕的边缘【 picx <= 0 or picx + pic.get_width() >= 600】,屏幕上的内容都是虚拟的,所以需要编程来确定是否“碰”在一起。
改变笑脸的方向:就是要改变坐标的趋势,越来越大要改为越来越小,反之亦然,看上去好像是在弹跳,常用技巧是通过设置速度speed = -speed。
代码如下:
# SmileyBounce1.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([600,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock()
speed = 5
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += speed
picy += speed
if picx <= 0 or picx + pic.get_width() >= 600:
speed = -speed
screen.fill(BLACK)
screen.blit(pic, (picx,picy))
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit
3、在四面墙上弹回笑脸
上面的程序中的笑脸只能沿同一条斜线来回弹跳,是因为笑脸的水平速度和垂直速度时时刻刻保持一致。所以要在四面墙上弹回笑脸,只要破坏这一致性。可以通过把窗口改成长方形,来破坏笑脸的水平速度和垂直速度时时刻刻保持一致性。
水平速度和垂直速度
speedx = 5
speedy = 5
碰撞四面墙
if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
if picy <= 0 or picy + pic.get_height() >= 600:
speedy = -speedy
整合
# SmileyBounce2.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += speedx
picy += speedy
if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
if picy <= 0 or picy + pic.get_height() >= 600:
speedy = -speedy
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit
如果要显示笑脸的弹跳痕迹,只要在绘制每一帧前不要清除之前绘制的笑脸,即注释掉screen.fill(BLACK)