我对pygame很新,所以请原谅我,如果我看起来很愚蠢,但我在尝试获得一个有边框的滚动系统时遇到问题 - 即在达到背景边缘而不是连续滚动时停止的系统。 / p>
这就是我所拥有的:
import pygame
import math
import sys
import random
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1024, 728),0,0)
pygame.display.set_caption("Modern Politics")
b1 = "Blank.png"
back = pygame.image.load(b1).convert()
back1 = pygame.image.load(b1).convert()
x=0
y=0
screenWidth = 1024
screenHeight = 728
while True:
screen.blit(back, (x,y))
screen.blit(back1, (x-screenWidth,y-screenHeight))
for event in pygame.event.get():
if not hasattr(event, "key"): continue
if event.key == K_RIGHT:
x=x-10
elif event.key == K_LEFT:
x=x+10
elif event.key == K_UP:
y=y+10
elif event.key == K_DOWN:
y=y-510
elif event.key == K_ESCAPE:
sys.exit(0)
if x == screenWidth:
x=0
if y == screenHeight:
y=0
msElapsed = clock.tick(100)
pygame.display.flip()
但是这只是出现了一大堆问题,如这个gif所示:
https://gyazo.com/f734c9955d52b0fed1e89766013f4122
我想要滚动的(未完成的)图片是这张1920x1080图片:
https://www.dropbox.com/s/fh2e99nw5jh7eqc/Blank.png?dl=1
如何使图像停止滚动,并且在到达图像末尾时不会滚动?它需要垂直和水平工作。
(顺便说一句,我使用的是Python 3.4)
答案 0 :(得分:1)
我认为它与"following camera"
类似,所以如果你在屏幕上有更多的对象,你可以使用camera offset
,但在这里我会使用不同的东西。
您需要比较左,右,上,下图像边框与左,右,上,下屏幕边框以停止滚动
# left borders
if x > 0:
x = 0
# top borders
if y > 0:
y = 0
# right borders
if x+image_width < screen_width:
x = screen_width
# bottom borders
if y+image_height < screen_height:
y = screen_height
我使用pygame.Rect
来代替rect.left
和x
而不是rect.right
而使用x+width
代替rect.top
代替y
}和rect.bottom
代替y+height
)
#!/usr/bin/env python3
# pygame (simple) template
import pygame
# === CONSTANTS === (UPPER_CASE names)
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 728
# === CLASSES === (CamelCase names)
# empty
# === FUNCTIONS === (lower_case names)
# empty
# === MAIN ===
# --- init ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()
pygame.display.set_caption("Modern Politics")
# --- objects ---
image_name = "Blank.png"
image = pygame.image.load(image_name).convert()
image_rect = image.get_rect()
speed = 20
change_x = 0
change_y = 0
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
# --- global events ---
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# --- player events ---
# KEYDOWN/KEYUP event occurs when key is going down/up (short-time event),
# not when key is held down/up (long-time event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_x -= speed
elif event.key == pygame.K_LEFT:
change_x += speed
elif event.key == pygame.K_DOWN:
change_y -= speed
elif event.key == pygame.K_UP:
change_y += speed
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
change_x += speed
elif event.key == pygame.K_LEFT:
change_x -= speed
elif event.key == pygame.K_DOWN:
change_y += speed
elif event.key == pygame.K_UP:
change_y -= speed
# --- updates (without draws) ---
# move object
image_rect.x += change_x
image_rect.y += change_y
# check borders
# right borders (SCREEN_WIDTH)
if image_rect.right < screen_rect.right:
image_rect.right = screen_rect.right
# left borders (0)
if image_rect.left > screen_rect.left:
image_rect.left = screen_rect.left
# bottom borders (SCREEN_HEIGTH)
if image_rect.bottom < screen_rect.bottom:
image_rect.bottom = screen_rect.bottom
# left borders (0)
if image_rect.top > screen_rect.top:
image_rect.top = screen_rect.top
# --- draws (without updates) ---
screen.blit(image, image_rect)
# human eyes need at least 25FPS to see animation,
# monitors mostly refresh screen with 60Hz - it means 60FPS.
clock.tick(30)
pygame.display.flip()
# --- the end ---
pygame.quit()