我正在尝试复制那些背景不断向左(或向上或向下)滑动的旧式街机游戏,但我正在寻找一些简单的代码行来实现该目标。 会使背景图像向左移动,然后重置以添加同一图像的另一个副本的操作。您能建议我一个简单的方法将此功能添加到我的代码中吗?
import pygame
import os
size = width, height = 750, 422
screen = pygame.display.set_mode(size)
img_path = os.path.join(os.getcwd())
background_image = pygame.image.load('background.jpg').convert()
pygame.display.set_caption("BallGame")
class Ball(object):
def __init__(self):
self.image = pygame.image.load("ball.png")
self.image_rect = self.image.get_rect()
self.image_rect.x
self.image_rect.y
self.facing = 'LEFT'
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_DOWN] and self.image_rect.y < 321:
self.facing = 'DOWN'
self.image_rect.y += dist
elif key[pygame.K_UP] and self.image_rect.y > 0:
self.facing = 'UP'
self.image_rect.y -= dist
if key[pygame.K_RIGHT] and self.image_rect.x < 649:
self.facing = 'RIGHT'
self.image_rect.x += dist
elif key[pygame.K_LEFT] and self.image_rect.x > 0:
self.facing = 'LEFT'
self.image_rect.x -= dist
def draw(self, surface):
if self.facing == "RIGHT":
surface.blit(pygame.transform.flip(self.image, True, False),(self.image_rect.x,self.image_rect.y))
elif self.facing == "DOWN":
surface.blit(pygame.image.load("ball_down.png"),(self.image_rect.x,self.image_rect.y))
if self.facing == "UP":
surface.blit(pygame.image.load("ball_up.png"),(self.image_rect.x,self.image_rect.y))
elif self.facing == "LEFT":
surface.blit(self.image,(self.image_rect.x,self.image_rect.y))
pygame.init()
screen = pygame.display.set_mode((750, 422))
ball = Ball()
clock = pygame.time.Clock()
pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play(-1, 0.0)
running = True
while running:
esc_key = pygame.key.get_pressed()
for event in pygame.event.get():
if esc_key[pygame.K_ESCAPE]:
pygame.display.quit()
pygame.quit()
running = False
ball.handle_keys()
screen.blit(background_image, [0, 0])
ball.draw(screen)
pygame.display.update()
clock.tick(40)
答案 0 :(得分:2)
这是一个简单的例子。
请注意注释以得到解释。
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
# this is the background image
# we need to blit it two times, let's call them "left" and "right"
back_image = pygame.image.load('background.png')
# this rect will store the position of the "left"-part of the background
back_rect = back_image.get_rect()
while True:
if pygame.event.get(pygame.QUIT):
break
for e in pygame.event.get():
# your event handling here
pass
# this is the "left" image
screen.blit(back_image, back_rect)
# this is the "right" image, next to the left one
# we calculate the "right" position by simply moving the rect by the width of the image
screen.blit(back_image, back_rect.move(back_rect.width, 0))
# we want a scrolling background, so we just move the position rect
back_rect.move_ip(-2, 0)
# it the right edge of the "left" image is zero, that means it's fully out of view
if back_rect.right == 0:
# so we reset the rect and start over
back_rect.x = 0
clock.tick(60)
pygame.display.flip()
这是示例图片(另存为background.png
)
最终结果将如下所示: