Toby Host
Toby Host

Reputation: 9

Python 3.9.6 background scrolling problem

I am creating a typing racer game where it generates a random set of words and the user needs to match these words. The concept of the game is that a litte character is chased by a monster and will eventually die if the user messes up the words. So to make it as simple as possible for me (beginner) i decided to use just a scrolling background to make it look like the chase is in action. Please help. im so lost

import pygame

pygame.init()

#Setting up the screen display for the python game
screen = pygame.display.set_mode((1000, 800))

#RGB - Red, Green, Blue
screen.fill((92, 150, 150))

# Customizing and personalizing my screen
pygame.display.set_caption("Typing Racer!")
icon = pygame.image.load("exercise (1).png")
pygame.display.set_icon(icon)

#implementing an infinite background to the game in order to create the movement effect
backgroundImg = pygame.image.load("panoramic-view-field-covered-grass-trees-sunlight-cloudy-sky.jpg")
backgroundX = 0
backgroundY = -225

# Creating the player and its placement in the game
playerImg = pygame.image.load("running-man.png")
playerX = 400
playerY = 100

#Creating the monster chasing the player
objectImg = pygame.image.load("godzilla.png")
objectX = 200
objectY = 100

#implementing the data on the screen using .blit
def background():
    screen.blit(backgroundImg, (backgroundX, backgroundY))

#Implementing objects into the game
def player():
    screen.blit(playerImg, (playerX, playerY))

def object():
    screen.blit(objectImg, (objectX, objectY))


#Setting up the typing area for the user of the program

text_font = pygame.font.Font(None, 32)
user_text = ""

#Input text box
input_rect = pygame.Rect(395, 490, 250, 40)


while True:
    for event in pygame.event.get():
        bg_scroll_x = backgroundX % backgroundImg.get_rect().width
        screen.blit(backgroundImg, (bg_scroll_x, 0))

        if bg_scroll_x < 1000:
            screen.blit(backgroundImg, (bg_scroll_x - backgroundImg.get_rect().width, 0))

        #KEYDOWN checks if a button was pressed on the keyboard while unicode checks which button was pressed
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                user_text = user_text[:-1]
            else:
                user_text += event.unicode

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    #Typing Area for User
    text_surface = text_font.render(user_text, True, (255 ,255 ,255))

    screen.blit(text_surface, (input_rect.x + 5, input_rect.y + 10))

    pygame.draw.rect(screen, (128, 128, 0), input_rect, 3)

    #input_rect.w = max(text_surface.get_width()+10)

    #Starting the background movement
    backgroundX -= 0.8

    #Calling the functions
    background()

    object()

    player()

    pygame.display.update()

Upvotes: 1

Views: 107

Answers (1)

Rabbid76
Rabbid76

Reputation: 210876

You must draw the background in the application loop.

while True:
    for event in pygame.event.get():
        bg_scroll_x = backgroundX % backgroundImg.get_rect().width
        
        #KEYDOWN checks if a button was pressed on the keyboard while unicode checks which button was pressed
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                user_text = user_text[:-1]
            else:
                user_text += event.unicode

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(backgroundImg, (bg_scroll_x, 0))
    if bg_scroll_x < 1000:
        screen.blit(backgroundImg, (bg_scroll_x - backgroundImg.get_rect().width, 0))

    # [...]

The event loop is only executed when an event occurs. However, the application loop is executed in every frame.

The typical PyGame application loop has to:

Upvotes: 1

Related Questions