《pygame游戏开发实战指南》第三节 理解pygame中的坐标体系

pygame中的坐标体系非常的简单,其实就是一句话:任何对象的左上角都为坐标原点(0, 0),向右为X轴正方向,向下为Y轴正方向。如下图所示。本节主要通过一些示例来带大家理解这一句话。如果读者已经理解的话,可以直接跳过这一节。
pygame坐标系

项目代码下载地址: https://github.com/la-vie-est-belle/pygame_codes


3.1 坐标体系使用示例

在示例代码3-1中,我们往屏幕上放了陆地图片和小恐龙图片,然后往小恐龙头上加了一朵小花。

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((1100, 600))
pygame.display.set_caption('Dino Runner')

icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

land = pygame.image.load('land.png').convert_alpha()
land_rect = land.get_rect()
land_rect.y = 520                                   # 1

dino = pygame.image.load('dino_start.png').convert_alpha()
dino_rect = dino.get_rect()
dino_rect.topleft = (80, 450)                       # 2

flower = pygame.image.load('flower.png').convert_alpha()
flower_rect = flower.get_rect(topleft=(70, 10))     # 3

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

    screen.fill((255, 255, 255))
    screen.blit(land, land_rect)
    screen.blit(dino, dino_rect)
    dino.blit(flower, flower_rect)

    pygame.display.flip()

运行结果如下:
在这里插入图片描述

代码解释如下:
#1 设置陆地图片在屏幕上的y坐标,值为520,坐标为(0, 520)
陆地图片在屏幕上的位置

#2 设置小恐龙图片左上角在屏幕上的位置,坐标为(80, 450)
小恐龙图片在屏幕上的位置

#3 设置小花朵图片左上角在小恐龙图片上的位置,坐标为(70, 10)。这里我们直接在get_rect()函数中设置了topleft的值,效果跟以下代码相同。

flower_rect = flower.get_rect()
flower_rect.topleft = (70, 10)

小花朵图片在小恐龙图片上的位置


3.2 改变坐标原点的位置

一些开发者可能不习惯将坐标原点放在左上角,也不习惯向右为X轴正方向,向下为Y轴正方向。假如我们现在要以屏幕中心为坐标原点,向右为X轴正方向,向上为Y轴正方向,需要怎么做呢?
屏幕中心为原点
因为pygame没有提供改变坐标原点的函数,所以我们可以自己写一个坐标转换函数。请看示例代码3-2。

import sys
import pygame


def convert_coordinate(surface_width, surface_height, x, y):            # 1
    new_x = x + int(surface_width / 2)
    new_y = int(surface_height / 2) - y
    return new_x, new_y


pygame.init()
screen = pygame.display.set_mode((1100, 600))
pygame.display.set_caption('Dino Runner')

icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

dino = pygame.image.load('dino_start.png').convert_alpha()
dino_rect = dino.get_rect()
dino_rect.topleft = convert_coordinate(screen.get_width(), screen.get_height(), 0, 0)   # 2
print(dino_rect.topleft)

flower = pygame.image.load('flower.png').convert_alpha()
flower_rect = flower.get_rect(topleft=convert_coordinate(dino.get_width(), dino.get_height(), 0, 0))    # 3
print(flower_rect.topleft)

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

    screen.fill((255, 255, 255))
    screen.blit(dino, dino_rect)
    dino.blit(flower, flower_rect)

    pygame.display.flip()

运行结果如下:
示例代码3-2运行结果

代码解释如下:
#1 坐标转换函数,根据目标Surface对象的宽高计算得出新坐标体系下的x和y坐标。

#2 在新坐标体系下,如果我们将小恐龙的坐标设置为(0, 0),其实就是将它的左上角位置放在屏幕中心。
新坐标体系下的小恐龙

#3 同理,在新坐标体系下,如果我们将小花朵的坐标设置为(0, 0),其实就是将它的左上角位置放在小恐龙的中心。
新坐标体系下的小花朵


3.3 小结

在本节我们学习了pygame的坐标体系,以及如何通过一个坐标转换函数来建立新的坐标体系。


如果你喜欢笔者的这部专栏,可以给笔者一些打赏,或者通过购买一些项目来支持作者,非常感谢!

微信支付   支付宝   微店
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

la_vie_est_belle

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值