zerrith zerrith - 18 days ago 6
Python Question

Issue with Sprite


  1. The issue I am currently having is that my image appears to be filling the screen with itself, how can i fix this issue?

    import pygame, os, sys
    from pygame.locals import*

    pygame.init()

    sHEIGHT = 600
    sWIDTH = 800
    gName = "Survive"

    fps = pygame.time.Clock()
    FPS = 10

    GD = pygame.display.set_mode((sWIDTH,sHEIGHT))
    class Main:
    white = (255,255,255)

    def display(white):
    pygame.display.set_caption(gName)
    GD.fill(white)
    def player1():
    img ="Images/"
    health = 100
    global movex
    global movexchange
    global movey
    global moveychange
    global imgplayer1
    movex = 400
    movey = 400
    movexchange = 0
    moveychange = 0

    imgplayer1 = pygame.image.load(os.path.join(img, "Human.png"))




    pygame.display.update()


    def gameLoop():
    fps.tick(FPS)


    while True:
    print(fps)
    global movexchange
    global moveychange
    global movex
    global movey
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()
    if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_a:
    moveychange = 0
    movexchange -=5
    if event.key == pygame.K_w:
    moveychange -=5
    movexchange =0
    if event.key == pygame.K_s:
    moveychange += 5
    movexchange = 0
    if event.key == pygame.K_d:
    moveychange = 0
    movexchange +=5
    if event.type == pygame.KEYUP:
    if event.key == pygame.K_a:
    movexchange =0
    moveychange =0
    if event.key == pygame.K_w:
    movexchange =0
    moveychange =0
    if event.key == pygame.K_s:
    movexchange =0
    moveychange =0
    if event.key == pygame.K_d:
    movexchange =0
    moveychange =0


    movex += movexchange
    movey += moveychange
    GD.blit(imgplayer1,(movex,movey))
    pygame.display.update()


    display(white)
    player1()
    gameLoop()



Does anyone have any ideas as to why this is happening and how i can solve the issue?

Answer

PyGame is low-level library.

You have to clear screen before you blit objects in new places. You can use some image as background or you can use fill(color) to fill screen.

# --- constants --- (UPPER_CASE names)

WHITE = (255, 255, 255)
FPS = 30

# --- functions --- (lower_case names)

def game_loop():
    global move_x_change
    global move_y_change 
    global move_x
    global move_y

    while True:
        # --- events ---

        for event in pygame.event.get():
            # ... code ...

        # --- updates (without draws) ---

        move_x +=  move_x_change
        move_y +=  move_y_change

        # --- draws (without updates) ---

        # clear buffer
        screen.fill(WHITE)

        # draw elements
        screen.blit(img_player_1, (move_x, move_y))

        # send buffer on screen
        pygame.display.update()

        # --- FPS ---

        # keep constant speed
        fps.tick(FPS)

# --- main ---

pygame.init()
# ... code ...

BTW: PEP 8 -- Style Guide for Python Code

Example how to organize code: simple and other templates