flinnzos
flinnzos

Reputation: 11

(Pygame) Player not moving

I'm a beginner, so sorry if this is a dumb mistake, but the player in my game is not moving. I was pretty sure that this is correct, because this is what I was taught in the game that I used for my computer programming camp (the game for the camp worked perfectly fine) but this one just doesn't work.

# pygame import
import pygame
from pygame import *
pygame.init()

# game window
game_window_width = 800
game_window_height = 600

# colour
red = 255, 0, 0
orange = 255, 128, 0
yellow = 255, 255, 0
green = 0, 255, 0
darkGreen = 0, 128, 0
blue = 0, 0, 255
darkBlue = 0, 0, 128
purple = 200, 0, 255
pink = 255, 60, 203
white = 255, 255, 255
black = 0, 0, 0
grey = 128, 128, 128
brown = 100, 80, 30
color = 0

# game setup
game_window = display.set_mode((game_window_width, game_window_height))
display.set_caption('gameTemplate')

print('outside game loop')

clock = pygame.time.Clock()
game_over = False

# game loop
while not game_over:
    for event in pygame.event.get():
        if event.type == QUIT:
            game_over = True

    print('inside game loop')

    # game background
    backgroundIMG = pygame.image.load('crossyRoadBG.png')
    backgroundIMG = pygame.transform.scale(backgroundIMG, (game_window_width, game_window_height))
    game_window.blit(backgroundIMG, (0, 0))

    #man
    manR = 80
    manX = 220
    manY = 520
    manSpeed = 30
    draw.rect(game_window, brown, (manX, manY, manR, manR))
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            manX += manSpeed
        if event.key == pygame.K_LEFT:
            manX -= manSpeed

    # frame
    clock.tick(30)
    # update
    display.update()

# game exit
pygame.quit()

Any help?

Upvotes: -1

Views: 102

Answers (1)

furas
furas

Reputation: 143097

It is not problem with PyCharm but with mess in code. You have elements in wrong places. You check keys outside for event loop. You set position manX = 220, manY = 520 inside loop so you move player again and again to the same place - and it can't move.

import pygame
from pygame import *

# --- constants ---

# game window
game_window_width = 800
game_window_height = 600

# colour
red = 255, 0, 0
orange = 255, 128, 0
yellow = 255, 255, 0
green = 0, 255, 0
darkGreen = 0, 128, 0
blue = 0, 0, 255
darkBlue = 0, 0, 128
purple = 200, 0, 255
pink = 255, 60, 203
white = 255, 255, 255
black = 0, 0, 0
grey = 128, 128, 128
brown = 100, 80, 30
color = 0

# --- classes ---

# empty

# --- functions ---

# empty

# --- main ---

print('outside game loop')

pygame.init()

# game setup
game_window = display.set_mode((game_window_width, game_window_height))
display.set_caption('gameTemplate')

# - objects -

# game background
backgroundIMG = pygame.image.load('crossyRoadBG.png')
backgroundIMG = pygame.transform.scale(backgroundIMG, (game_window_width, game_window_height))

#man
manR = 80
manX = 220
manY = 520
manSpeed = 30

# - loop -

clock = pygame.time.Clock()
game_over = False

# game loop
while not game_over:

    print('inside game loop')

    # - events -

    for event in pygame.event.get():
        if event.type == QUIT:
            game_over = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                manX += manSpeed
            if event.key == pygame.K_LEFT:
                manX -= manSpeed

    # - moves/collisions/updates (without draws) -
    
    # empty
    
    # - draws (without moves/collisions/updates) -
    
    game_window.blit(backgroundIMG, (0, 0))

    draw.rect(game_window, brown, (manX, manY, manR, manR))

    # frame
    clock.tick(30)
    # update
    display.update()

# - exit -

pygame.quit()

BTW: next time you can use print() to see values in variables and which part of code is executed. It is called "print debuging" and it helps to see what program is doing.


BTW: using current method with events you will have to press again and again to move object. You may need pygame.key.get_pressed()

Upvotes: 1

Related Questions