使背景在 pygame 中横向移动 [英] Making the background move sideways in pygame

查看:76
本文介绍了使背景在 pygame 中横向移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pygame 创建游戏,并尝试为其添加背景(我使用了 YouTube 视频中的一些代码,但这不起作用).我也不明白代码是关于什么的.我的意思是背景确实会移动,但是当旧背景尚未离开屏幕时,它会自动在屏幕中间添加新版本的背景:

I am trying to create a game using pygame and I am attempting to add a background to it (I have used some code from a YouTube video but this is not working). I also to not understand what the code is on about. I mean the background and does move but it automatically adds a new version of the background in the middle of the screen when the older background has not gone off screen yet:

class Background:
    def __init__(self, x, y, picture):
        self.xpos = x
        self.ypos = y
        self.picture = picture
        self.rect = self.picture.get_rect()
        self.picture = pygame.transform.scale(self.picture, (1280, 720))

    def paste(self, xpos, ypos):
        screen.blit(self.picture, (xpos, ypos))

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))

while True:

background=pygame.image.load("C:/images/mars.jpg").convert_alpha()       

cliff = Background(0, 0, background)


rel_x = x % cliff.rect.width

cliff.paste(rel_x - cliff.rect.width, 0)
if rel_x < WIDTH:
    cliff.paste(rel_x, 0)
    x -= 1

这就是我的背景目前发生的情况[![我的问题是什么样的][1]][1]

This is what currently happens with my background [![what my problem looks like][1]][1]

[![我希望背景像][2]][2]

[![What I want the background to move like ][2]][2]

这就是我希望我的背景看起来像的样子(请忽略它是我唯一能找到的标志)

This is what I want my background to look like (please ignore the sign it was the only one I could find)

我现在发现真正的问题是什么

I have now discovered what the real problem is

推荐答案

如果你想有一个连续重复的背景,那么你必须画两次背景:

If you want to have a continuously repeating background, then you've to draw the background twice:

        +==================+
   +----||---------+------||------+
   |    ||         |      ||      |
   |    ||    1    |   2  ||      |
   |    ||         |      ||      |
   +----||---------+------||------+
        +==================+

您必须知道屏幕的大小.高度背景图像的大小应与屏幕的高度相匹配.背景的宽度可以不同,但​​至少应该是窗口的宽度(否则背景必须绘制2次以上).

You've to know the size of the screen. The size of the height background image should match the height of the screen. The width of the background can be different, but should be at least the with of the window (else the background has to be drawn more than 2 times).

bg_w, gb_h = size
bg =  pygame.transform.smoothscale(pygame.image.load('background.image'), (bg_w, bg_h))

背景可以想象成一排无尽的瓷砖.如果你想在某个位置pos_x绘制背景,那么你必须通过模(%)运算符来计算tile相对于屏幕的位置.第二个图块的位置移动了背景的宽度(bg_w):

The background can be imagined as a endless row of tiles. If you want to draw the background at an certain position pos_x, then you have to calculate the position of the tile relative to the screen by the modulo (%) operator. The position of the 2nd tile is shifted by the width of the background (bg_w):

x_rel = pos_x % bg_w
x_part2 = x_rel - bg_w if x_rel > 0 else x_rel + bg_w

最后背景必须blit两次,以填满整个屏幕:

Finally the background has to be blit twice, to fill the entire screen:

screen.blit(bg, (x_rel, 0))
screen.blit(bg, (x_part2, 0))

您可以通过以下示例程序测试该过程.背景可以通过<-分别移动->

You can test the process by the following example program. The background can be moved by <- respectively ->

import pygame

pygame.init()

size = (800,600)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

bg_w, bg_h = size 
bg = pygame.transform.smoothscale(pygame.image.load('background.image'), (bg_w, bg_h))
pos_x = 0
speed = 10

done = False
while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    allKeys = pygame.key.get_pressed()
    pos_x += speed if allKeys[pygame.K_LEFT] else -speed if allKeys[pygame.K_RIGHT] else 0

    x_rel = pos_x % bg_w
    x_part2 = x_rel - bg_w if x_rel > 0 else x_rel + bg_w

    screen.blit(bg, (x_rel, 0))
    screen.blit(bg, (x_part2, 0))

    pygame.display.flip()

这篇关于使背景在 pygame 中横向移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆