PyGame 鼠标移动背景,实时观察战场动态 修复(全屏300帧)

在这里插入图片描述

原理:鼠标到达边界,即控制背景移动 #

1、内容:建造5w*5w大小地板背景 使人物能绘制在地板上,从而实现2D开放世界 与 视角观察
2、准备张 5000*5000 像素的图片,
from pygame.locals import *
import pygame,time,sys
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((1920,1080),FULLSCREEN )
surface = pygame.image.load("images/5000.jpg").convert_alpha()

def Start():
    surface_x = 0
    surface_y = 0
    # 展示帧
    fps_count = 0
    start_fps = time.time()
    clock = pygame.time.Clock()
    myfont = pygame.font.Font(None,60)
    switch  =True
    while switch :
        clock.tick(600)
        now = time.time()
        fps = fps_count / (now - start_fps)
        fpsImage = myfont.render(str(fps), True, (255, 255, 255))
        fps_count += 1
        #绘制地板
        screen.blit(surface, (surface_x, surface_y))

        # 建造地板
        Scope = 5000  # 注意间距与图片匹配,否则掉帧的很奇妙
        for x in range(11):
            screen.blit(surface,(surface_x + Scope, surface_y))  # 隔一个,并向右画10次  # 减5000是为了不隔开,直接向右画10次  因为第一个screen放进循环里会卡爆
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 5000))  # 向右画10次
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 10000))  # 为啥不循环? 循环就猛掉帧
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 15000))  #
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 20000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 25000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 30000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 35000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 40000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 45000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 50000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 55000))
            screen.blit(surface, (surface_x + Scope - 5000, surface_y + 60000))
            Scope += 5000
        fps_count += 1
        screen.blit(fpsImage, (10, 10))

        # 鼠标控制背景移动 =====================================
        for x in range(6):
            c, d = pygame.mouse.get_pos()  # 鼠标位置
            screen_speed = 10  # 背景移动速度
            if c > 0 and d == 0:
                surface_y += screen_speed
            if c == 0 and d > 0:
                surface_x += screen_speed
            if c > 0 and d == 1079:  # 1920*1080
                surface_y -= screen_speed
            if c == 1919 and d > 0:  # 1920*1080
                surface_x -= screen_speed
            # 如果大于边界
            if surface_x > 0:
                surface_x = 0
            if surface_y > 0:
                surface_y = 0



        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                switch = False
        keys = pygame.key.get_pressed()  # 获取键盘事件
        if keys[K_ESCAPE]:  # 如果按下ESC键
            sys.exit()  # 退出游戏
        pygame.display.update()
if __name__ == '__main__':
    Start()




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值