原理:鼠标到达边界,即控制背景移动 #
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()