0

Decided to make a game in Pygame. For several weeks I have been thinking about how to make the background move. Tried many different options, none worked. For example, I use the code taken from this site. Separately, the code with a moving background works, but not in the main code. Where did I go wrong? Help a newbie!All images used are attached.enter image description here

import pygame as pg
import sys

RES = WIDTH, HEIGHT = 1200, 800
bg_color = (0, 0, 0)
FPS = 60
ship_img = pg.image.load('Кирилл/image/Space_ship.png')
back_ground = pg.transform.scale(pg.image.load(
    'Кирилл/image/back_ground.jpg'), (WIDTH, HEIGHT))
# laser_img = pg.image.load('image/Laser.png')
player_step1 = 5.5
player_step = float(player_step1)


class Ship():
    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        # self.laser_img = None
        # self.laser = []
        self.cool_down_counter = 0

    def draw(self, sc):
        sc.blit(self.ship_img, (self.x, self.y))

    def get_width(self):
        return self.ship_img.get_width()

    def get_height(self):
        return self.ship_img.get_height()


class Player(Ship):
    def __init__(self, x, y, health=100):
        super().__init__(x, y, health)
        self.ship_img = ship_img
        # self.laser_img = laser_img
        self.mask = pg.mask.from_surface(self.ship_img)
        self.max_health = health

    def control(self):
        self.sc = pg.display.set_mode(RES)
        pos_x = -2
        speed = 10
        keys = pg.key.get_pressed()
        if keys[pg.K_w] and player.y - player_step > 0:
            player.y -= player_step
        elif keys[pg.K_s] and player.y + player_step + player.get_height() < HEIGHT:
            player.y += player_step
        elif keys[pg.K_a] and player.x - player_step > 0:
            player.x -= player_step
        elif keys[pg.K_d] and player.x + player_step + player.get_width() < WIDTH:
            player.x += player_step
        pos_x += speed if keys[pg.K_LEFT] else - \
            speed if keys[pg.K_RIGHT] else -2

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

        self.sc.blit(back_ground, (x_rel, 0))
        self.sc.blit(back_ground, (x_part2, 0))


player = Player(0, 360)


class Game():
    def __init__(self):
        pg.init()
        self.sc = pg.display.set_mode(RES)
        self.clock = pg.time.Clock()

    def new_game(self):
        pass

    def update(self):
        pg.display.flip()
        self.clock.tick(FPS)
        pg.display.set_caption(
            'Futurama game ' + f'{self.clock.get_fps() :.1f}')

    def draw(self):

        self.sc.blit(back_ground, (0, 0))
        player.draw(self.sc)

    def check_events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
        player.control()

    def run(self):
        while True:
            self.check_events()
            self.draw()
            self.update()


if __name__ == '__main__':
    game = Game()
    game.run()```
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Also see [How to make parallax scrolling work properly with a camera that stops at edges pygame](https://stackoverflow.com/questions/63712333/how-to-make-parallax-scrolling-work-properly-with-a-camera-that-stops-at-edges-p/74002486#74002486) – Rabbid76 Apr 12 '23 at 18:07

0 Answers0