pygame游戏开发之框架

1 - 游戏基础代码

1 - 全部代码

import pygame, sys

pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("第一个游戏")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]
    screen.fill(BLACK)
    screen.blit(ball,ballrect)
    pygame.display.update()

2 - 分步解析

1 - 初始化init()及设置

pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("第一个游戏")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()

pygame.image.load(filename)
讲filename路径下的图像载入游戏,支持JPG,PNG,GIF(非动画)等13种常见图片格式
ball = pygame.image.load(“PYG02-ball.gif”)
ballrect = ball.get_rect()

Surface对象 ball.get_rect()
Pygame使用内部定义的Surface对象表示所有载入的图像,其中get_rect()方法返回一个覆盖图像的矩形rect对象
Rect对象
Rect对象有一些重要的属性,例如:
left,right,top,bottom表示左右上下
width,height表示宽度和高度
参数说明

2 - 获取事件并逐类响应

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

ballrect.move(x,y)
矩形移动一个偏移量(x,y),即在横轴方向移动x个像素,在纵轴上移动y个像素,xy是整数
壁球的反弹运动

if ballrect.left < 0 or ballrect.right > width:
    speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
    speed[1] = - speed[1]

遇到左右两侧,横向速度取反
遇到上下两侧,纵向速度取反

3 - 刷新屏幕

screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()

screen.fill(color)
显示窗口背景填充为color颜色,采用RGB色彩体系,由于壁球不断运动,运动后原有位置将默认填充白色,因此需要不断刷新背景色
screen.blit(src,dest)
将一个图像绘制在另一个图像上,即将src绘制在dest位置上。通过Rect对象引导对壁球的绘制

v2 - 屏幕帧率设置

代码

import sys

import pygame

pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("第一个游戏")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 24
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit(0)
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)

分析

  1. pygame.time.Clock()
    创建一个Clock对象,用于操作时间
  2. clock.tick(framerate)
    控制帧速度,即窗口刷新速度,例如:clock.tick(100)表示每秒钟100次帧刷新,视频中每次展示的静态图像称为帧

3 - 键盘的基本使用

1 - 关键要素

关键要素

2 - 代码

import pygame
import sys

pygame.init()
size = width, height = 500, 500
BLACK = 0, 0, 0
speed = [1, 0]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("小球移动")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                if speed[0] != 1:
                    speed = [1, 0]
            elif event.key == pygame.K_LEFT:
                if speed[0] != -1:
                    speed = [-1, 0]
            elif event.key == pygame.K_UP:
                if speed[1] != -1:
                    speed = [0, -1]
            elif event.key == pygame.K_DOWN:
                if speed[1] != 1:
                    speed = [0, 1]
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)

3 - 分析

pygame.KEYDOWN
Pygame对键盘敲击的事件定义,每个键对应一个具体定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值