Python Forum
[PyGame] screen.fill command wont change color of background
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] screen.fill command wont change color of background
#1
Subject explains it all. Any help greatly appreciated.
Code:
import pygame

pygame.init()
# create screen
screen = pygame.display.set_mode((800, 600))
# title and icon
pygame.display.set_caption("GZSG")
icon = pygame.image.load('living-dead.png')
pygame.display.set_icon(icon)
# player and npcs
playerImg = pygame.image.load('target-shooter.png')
playerX = 50
playerY = 250
def player(x,y):


    screen.blit(playerImg, (x, y))
# game loop
running = True
while running:


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 100, 0))

    # if key is pressed check whether its right or left
    if event.type == pygame.KEYDOWN:
        print("somethign has been pressed")
        if event.key == pygame.K_w:
            playerY_change = -0.1
        if event.key == pygame.K_s:
            playerY_change = +0.1
        if event.key == pygame.K_d:
            playerX_change = +0.1
        if event.key == pygame.K_a:
            playerX_change = -0.1
    if event.type == pygame.KEYUP:
        if event.key ==pygame.K_w or event.key == pygame.K_d:
            playerY_change = 0
    if event.type == pygame.KEYUP:
        if event.key ==pygame.K_w or event.key == pygame.K_d:
            playerY_change = 0

playerY += playerY_change
playerX += playerX_change

player(playerX, playerY)
pygame.display.update()
 
Error:
NameError: name 'playerY_change' is not defined
Yoriz write May-14-2021, 05:49 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The error NameError: name 'playerY_change' is not defined means that playerY_change has not been assigned a starting value.

Are the last 4 lines
playerY += playerY_change
playerX += playerX_change
 
player(playerX, playerY)
pygame.display.update()
Meant to be indented inside the while loop
Reply
#3
(May-14-2021, 06:10 PM)Yoriz Wrote: The error NameError: name 'playerY_change' is not defined means that playerY_change has not been assigned a starting value.

Are the last 4 lines
playerY += playerY_change
playerX += playerX_change
 
player(playerX, playerY)
pygame.display.update()
Meant to be indented inside the while loop
Thanks for the reply!
I assigned the value. I assume that I didn't at least mess this up haha
playerY_change = 0
playerX_change = 0
I'm not sure about the last 4 lines. Followed a tutorial.
The problem with the background not changing still persists. It's black when running but for a slight second when closing it turns the desired color. any clue on why it doesn't work?
Reply
#4
I don't use pygame but I would suggest you take another look at the tutorial because to me those last 4 lines should be in the while loop.
try indenting them to look like this (Note the rest of the code has been removed just to illustrate where the last four lines should be).
import pygame
 
pygame.init()
# create screen
screen = pygame.display.set_mode((800, 600))
...
...
...
# game loop
running = True
while running:
    ...
    ...
    ...
    if event.type == pygame.KEYUP:
        if event.key ==pygame.K_w or event.key == pygame.K_d:
            playerY_change = 0
 
    playerY += playerY_change
    playerX += playerX_change
 
    player(playerX, playerY)
    pygame.display.update()
Reply
#5
Does this help you at all?

import pygame
 
pygame.init()
# create screen
screen = pygame.display.set_mode((800, 600))
# title and icon
pygame.display.set_caption("GZSG")
icon = pygame.image.load('living-dead.png')
pygame.display.set_icon(icon)
# player and npcs
playerImg = pygame.image.load('target-shooter.png')
playerX = 50
playerY = 250
def player(x,y):
	screen.blit(playerImg, (x, y))
# game loop
running = True
while running:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = False
	screen.fill((0, 100, 0))
	# if key is pressed check whether its right or left
	key_pressed = pygame.key.get_pressed ()
	if key_pressed [pygame.K_w]:
		playerY -= 0.1 
	if key_pressed [pygame.K_s]:
		playerY += 0.1
	if key_pressed [pygame.K_d]:
		playerX += 0.1
	if key_pressed [pygame.K_a]:
		playerX -= 0.1
	player(playerX, playerY)
	pygame.display.update()
Reply
#6
Yes. Thank you all.
I've managed to accomplish what I set out to. A walk to clear your mind goes a long way.
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] How do you change your horizontal scrolling background to a vertical one? Kalebre 0 1,111 Jul-31-2023, 02:42 PM
Last Post: Kalebre
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 998 Apr-30-2023, 10:11 PM
Last Post: deanhystad
Question [PyGame] my pygame car wont move Erio85 1 1,084 Apr-24-2023, 04:52 PM
Last Post: Erio85
  Text wont draw fierygaming 1 2,988 Jul-28-2018, 10:39 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020