鱼C论坛

 找回密码
 立即注册
查看: 1392|回复: 1

[已解决]pygame地图移动

[复制链接]
发表于 2021-6-24 16:37:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
当地图大小大于窗口时,怎样让地图跟随人物移动显示?

地图和人物的代码如下:
import random , time
import pygame
print(pygame.init())

class 主角(pygame.sprite.Sprite):
    def __init__(self , 背景图 , 位置):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(背景图)
        self.rect = self.image.get_rect()
        self.rect.center = 位置
        self.移动速度 = 3
        self.速度加倍 = 1
        self.停顿标记 = 1
class 怪物(pygame.sprite.Sprite):
    def __init__(self , 背景图 , 位置):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(背景图)
        self.rect = self.image.get_rect()
        self.rect.center = 位置
        self.移动步数 = 0
        self.移动速度 = 5
        self.速度加倍 = 1
        self.停顿标记 = 1
        self.移动方向 = [0 , 0]
class 石头(pygame.sprite.Sprite):
    def __init__(self , 背景图 , 位置):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(背景图)
        self.rect = self.image.get_rect()
        self.rect.center = 位置
def 随机速度(速度倍数):
    return [random.randint(-速度倍数 , 速度倍数) , random.randint(-速度倍数 , 速度倍数)]

窗口宽 = 450
窗口高 = 600
主角标志 = 1
怪物标志 = 2
石头标志 = 3
帧率 = 100
窗口 = pygame.display.set_mode((450, 600))
pygame.display.set_caption('外星历险')
背景资源数组 = [0 for i in range(36 * 24)]
背景资源数组[0] = 主角标志
怪物数 = 16
for i in range(4):
    背景资源数组[i + 1] = 怪物标志
石头数 = 160
for i in range(36 * 24, 36 * 24 - 160 , -1):
    背景资源数组[i - 1] = 石头标志

窗口.fill((255 , 255 , 255))
random.shuffle(背景资源数组)
背景资源二维矩阵 = []
临时数组 = []
for i in range(len(背景资源数组)):
    临时数组.append(背景资源数组[i])
    if (i + 1) % 9 == 0:
        背景资源二维矩阵.append(临时数组)
        临时数组 = []
#print(背景资源二维矩阵)
主角背景图 = 'res\\主角.png'
主角组 = pygame.sprite.Group()
怪物背景图 = 'res\\怪物.png'
怪物组 = pygame.sprite.Group()
石头背景图 = 'res\\石头.png'
石头组 = pygame.sprite.Group()
所有资源数组 = []
for 行 in range(len(背景资源二维矩阵)):
    临时一维数组 = 背景资源二维矩阵[行]
    for 列 in range(len(临时一维数组)):
        if 主角标志 == 临时一维数组[列]:
            主角初始化位置 = (50 * 列 + 25 , 50 * 行 + 25)
            龙宇云 = 主角(主角背景图, 主角初始化位置)
            主角组.add(龙宇云)
            所有资源数组.append(龙宇云)
        elif 怪物标志 == 临时一维数组[列]:
            怪物初始化位置 = (50 * 列 + 25 , 50 * 行 + 25)
            临时怪物 = 怪物(怪物背景图 , 怪物初始化位置)
            怪物组.add(临时怪物)
            所有资源数组.append(临时怪物)
        elif 石头标志 == 临时一维数组[列]:
            石头初始化位置 = (50 * 列 + 25 , 50 * 行 + 25)
            临时石头 = 石头(石头背景图 , 石头初始化位置)
            石头组.add(临时石头)
            所有资源数组.append(临时石头)
主角组.draw(窗口)
怪物组.draw(窗口)
石头组.draw(窗口)
pygame.display.flip()

#screen.blit(image, (0, 0))
# pygame.draw.rect(screen, [0,0,255],[0,0,200,400],0)
# pygame.draw.rect(screen, [255,0,0],[400,0,200,400],0)



时钟 = pygame.time.Clock()
结束 = False
持续向左 = False
持续向右 = False
持续向上 = False
持续向下 = False
主角死亡 = False
暂停 = True
怪物延迟移动次数上限 = 2 * 帧率
怪物延迟移动次数 = 0
主角组.sprites()[0].移动速度 = 3
主角组.sprites()[0].速度加倍 = 1
while not 结束:
    #t1 = time.time()
    for 事件 in pygame.event.get():
        if 事件.type == pygame.QUIT:
            结束 = True
        if 事件.type == pygame.MOUSEBUTTONDOWN:
            if 事件.button == 1:
                暂停 = False
                print('开始')
            elif 事件.button == 3:
                暂停 = True
                怪物延迟移动次数 = 0
                print('暂停')
        if 事件.type == pygame.KEYDOWN:
            if 事件.key == pygame.K_LEFT:
                持续向左 = True
            if 事件.key == pygame.K_RIGHT:
                持续向右 = True
            if 事件.key == pygame.K_UP:
                持续向上 = True
            if 事件.key == pygame.K_DOWN:
                持续向下 = True
        if 事件.type == pygame.KEYUP:
            if 事件.key == pygame.K_LEFT:
                持续向左 = False
            if 事件.key == pygame.K_RIGHT:
                持续向右 = False
            if 事件.key == pygame.K_UP:
                持续向上 = False
            if 事件.key == pygame.K_DOWN:
                持续向下 = False
    时钟.tick(帧率) #每秒帧率100
    if 暂停:
        continue
    窗口.fill((255, 255, 255))


    if 怪物延迟移动次数 < 怪物延迟移动次数上限:
        怪物延迟移动次数 = 怪物延迟移动次数 + 1
    elif not 主角死亡:
        for 临时怪物 in 怪物组:
            if 临时怪物.停顿标记 != 临时怪物.移动速度:
                临时怪物.停顿标记 = 临时怪物.停顿标记 + 1
            else:
                临时怪物.停顿标记 = 1
                速度倍数 = 临时怪物.速度加倍
                if 临时怪物.移动步数 == 0:
                    临时速度 = 随机速度(速度倍数)
                    临时怪物.移动方向 = 临时速度
                if ((临时怪物.rect.left > 0 or 临时怪物.移动方向[0] != -1) and
                    (临时怪物.rect.right < 窗口宽 or 临时怪物.移动方向[0] != 1) and
                    (临时怪物.rect.top > 0 or 临时怪物.移动方向[1] != -1) and
                    (临时怪物.rect.bottom < 窗口高 or 临时怪物.移动方向[1] != 1)):
                    临时怪物.rect = 临时怪物.rect.move(临时怪物.移动方向)
                    if 临时怪物.rect.left < 0:
                        临时怪物.rect.left = 0
                    if 临时怪物.rect.right > 窗口宽:
                        临时怪物.rect.right = 窗口宽
                    if 临时怪物.rect.top < 0:
                        临时怪物.rect.top = 0
                    if 临时怪物.rect.bottom > 窗口高:
                        临时怪物.rect.bottom = 窗口高
                    临时怪物.移动步数 = 临时怪物.移动步数 + 1
                    if 临时怪物.移动步数 == 100:
                        临时怪物.移动步数 = 0
                else:
                    临时怪物.移动步数 = 0
                if pygame.sprite.spritecollide(主角组.sprites()[0], 怪物组, False):
                    主角死亡 = True
                    print('主角死亡')
                    break
                怪物组.remove(临时怪物)
                if pygame.sprite.spritecollide(临时怪物, 怪物组, False):
                    临时怪物.rect = 临时怪物.rect.move([-临时怪物.移动方向[0] , -临时怪物.移动方向[1]])
                    临时怪物.移动步数 = 0
                if pygame.sprite.spritecollide(临时怪物, 石头组, False):
                    临时怪物.rect = 临时怪物.rect.move([-临时怪物.移动方向[0] , -临时怪物.移动方向[1]])
                    临时怪物.移动步数 = 0
                怪物组.add(临时怪物)

    if not 主角死亡 and not ((持续向左 == True and 持续向右 == True) or (持续向上 == True and 持续向下 == True)):
        if 主角组.sprites()[0].停顿标记 != 主角组.sprites()[0].移动速度:
            主角组.sprites()[0].停顿标记 = 主角组.sprites()[0].停顿标记 + 1
        else:
            主角组.sprites()[0].停顿标记 = 1
            速度倍数 = 主角组.sprites()[0].速度加倍
            if 持续向左 == True and 主角组.sprites()[0].rect.left > 0:
                主角组.sprites()[0].rect = 主角组.sprites()[0].rect.move([-速度倍数 , 0])
                碰到的石头 = pygame.sprite.spritecollide(主角组.sprites()[0], 石头组, False)
                if 碰到的石头:
                    主角组.sprites()[0].rect.left = 碰到的石头[0].rect.right
                if pygame.sprite.spritecollide(主角组.sprites()[0], 怪物组, False):
                    主角死亡 = True
                    print('主角死亡')
                if 主角组.sprites()[0].rect.left < 0:
                    主角组.sprites()[0].rect.left = 0
            if 持续向右 == True and 主角组.sprites()[0].rect.right < 窗口宽:
                主角组.sprites()[0].rect = 主角组.sprites()[0].rect.move([速度倍数 , 0])
                碰到的石头 = pygame.sprite.spritecollide(主角组.sprites()[0], 石头组, False)
                if 碰到的石头:
                    主角组.sprites()[0].rect.right = 碰到的石头[0].rect.left
                if pygame.sprite.spritecollide(主角组.sprites()[0], 怪物组, False):
                    主角死亡 = True
                    print('主角死亡')
                if 主角组.sprites()[0].rect.right > 窗口宽:
                    主角组.sprites()[0].rect.right = 窗口宽
            if 持续向上 == True and 主角组.sprites()[0].rect.top > 0:
                主角组.sprites()[0].rect = 主角组.sprites()[0].rect.move([0 , -速度倍数])
                碰到的石头 = pygame.sprite.spritecollide(主角组.sprites()[0], 石头组, False)
                if 碰到的石头:
                    主角组.sprites()[0].rect.top = 碰到的石头[0].rect.bottom
                if pygame.sprite.spritecollide(主角组.sprites()[0], 怪物组, False):
                    主角死亡 = True
                    print('主角死亡')
                if 主角组.sprites()[0].rect.top < 0:
                    主角组.sprites()[0].rect.top = 0
            if 持续向下 == True and 主角组.sprites()[0].rect.bottom < 窗口高:
                主角组.sprites()[0].rect = 主角组.sprites()[0].rect.move([0 , 速度倍数])
                碰到的石头 = pygame.sprite.spritecollide(主角组.sprites()[0], 石头组, False)
                if 碰到的石头:
                    主角组.sprites()[0].rect.bottom = 碰到的石头[0].rect.top
                if pygame.sprite.spritecollide(主角组.sprites()[0], 怪物组, False):
                    主角死亡 = True
                    print('主角死亡')
                if 主角组.sprites()[0].rect.bottom > 窗口高:
                    主角组.sprites()[0].rect.bottom = 窗口高
    else:
        主角组.sprites()[0].停顿标记 = 1
    主角组.draw(窗口)
    怪物组.draw(窗口)
    石头组.draw(窗口)
    pygame.display.flip()


    # t2 = time.time()
    # print(t2 - t1)
    # 结束 = True
最佳答案
2021-6-24 16:44:54
我没玩过pygame,不过可以给你个思路,想象成人物移动的时候人物只是在做走的动作,但是模型位置并没有动只是地图在动,等任务走到地图边缘位置地图无法移动时再移动人物。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-24 16:44:54 | 显示全部楼层    本楼为最佳答案   
我没玩过pygame,不过可以给你个思路,想象成人物移动的时候人物只是在做走的动作,但是模型位置并没有动只是地图在动,等任务走到地图边缘位置地图无法移动时再移动人物。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 23:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表