Python游戏开发

Pygame

Pygame是一种游戏开发引擎。Pygame适合用于游戏逻辑验证,游戏入门以及系统演示验证


 

python -m pygame.examples.aliens

运行pygame自带的一个小游戏

Pygame最小开发框架

在最小开发框架中,获取事件并逐列响应与屏幕刷新是一对无限循环的关系

不断跟踪对游戏的不同输入,并给出不断的响应,同时刷新效果来保证这个响应让用户看得见

极简开发框架

最小开发框架的逐步解释

最小开发框架的代码

开发游戏最好用Pycharm

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame游戏之旅")

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

壁球小游戏(展示型)与图像的基本使用

为什么要引入Rect对象,因为Rect对象有很多属性,对于我们编写程序操作很方便

矩形对象是图像对象的正切矩形
后边的所有的移动操作以及对边缘的反弹操作都是针对这个矩形的

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("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()

while True:
    for event in pygame.event.get():
        if event.type == 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()

壁球小游戏(节奏型)与屏幕帧率设置

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("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()


while True:
    for event in pygame.event.get():
        if event.type == 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()
    fclock.tick(fps)

壁球小游戏(操纵型)与键盘的基本使用

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("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
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_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0])))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1) * int(speed[1]/abs(speed[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)

Pygame屏幕绘制机制

屏幕绘制的重要函数

pygame提供的显示函数还可以支持OpenGL和硬件加速

Pygame屏幕尺寸和模式设置

注意:每种显示方式要配合相应的处理机制

尽管我们可以设置屏幕的模式为窗口可调、无边框或者全屏显示,同时我们也要注意如何去针对不同的模式设计相关的应对方法

例如如果只把屏幕模式设为窗口可调,而游戏的小球和游戏的区域并没有变,小球的运动区域就还是会原来那样

如果屏幕设置为无边框,就还要增设其他关闭屏幕的方法

如果把屏幕设置为全屏模式,小球的尺寸也会被拉大

适应全屏并且Esc可退出的version

import pygame, sys

pygame.init()
vInfo = pygame.display.Info()
size = width, height =vInfo.current_w, vInfo.current_h #感知操作系统屏幕的宽度和高度
speed = [1,1]
BLACK = 0,0,0
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
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_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0])))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1) * int(speed[1]/abs(speed[1])))
            elif event.key == pygame.K_ESCAPE:    #ESC键退出
                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()
    fclock.tick(fps)

适应窗口大小变化的version

import pygame, sys

pygame.init()

size = width, height =600, 400
speed = [1,1]
BLACK = 0,0,0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)

pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
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_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0])))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1) * int(speed[1]/abs(speed[1])))
            elif event.key == pygame.K_ESCAPE:    #ESC键退出
                sys.exit()
        elif event.type == pygame.VIDEORESIZE:
            size = width, height = event.size[0], event.size[1]
            screen = pygame.display.set_mode(size, pygame.RESIZABLE)
    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)
发布了329 篇原创文章 · 获赞 156 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/hxxjxw/article/details/104217761