在Ubuntu18.04(Virtualbox)上滑动背景的问题

时间:2018-07-16 09:19:56

标签: python python-3.x pygame virtualbox ubuntu-18.04

我正在VirtualBox Vm上运行Ubuntu 18.04,并且试图编写一个非常简单的游戏。我要使用的滑动背景有问题。 在第一个图像周期(开始的背景和第一次重置)之后,背景出现故障并显示无图像。可能与我在虚拟机上运行此.py程序有关的问题吗?还是我使用了错误的代码?我想让背景图像连续循环,而不仅仅是一次。 这是我正在运行的代码:

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()
bg_image_rect = background_image.get_rect()
pygame.mixer.pre_init(44100, 16, 2, 4096)

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, bg_image_rect)
    screen.blit(background_image, bg_image_rect.move(bg_image_rect.width, 0))
    bg_image_rect.move_ip(-2, 0)
    if bg_image_rect.right == 0:
        bg_image_rect.x == 0


    ball.draw(screen)
    pygame.display.update()

    clock.tick(60)

1 个答案:

答案 0 :(得分:2)

当您检查背景的当前位置并对其进行重置时,问题就出在这里:

if bg_image_rect.right == 0:
    bg_image_rect.x == 0

首先,if语句假设您的bg_image_rect.right在滚动时将完全达到0的位置。基于窗口大小和背景大小的组合,滚动可能看起来像: 13, 11, 9, 7, 5, 3, 1, -1, -3, -5。这意味着right == 0永远不会是True

第二,您在分配x时有错字。 bg_image_rect.x == 0正在比较这两个值。您需要bg_image_rect.x = 0

此部分需要更改为:

if bg_image_rect.right <= 0:
    bg_image_rect.x = 0