Pygame - Pressing backspace to change sprite color

Pygame - Pressing backspace to change sprite color



I'm trying to make a game, and I want to make the user be able to change color of their sprite when pressing backspace. As of now, nothing that I draw appears on screen, I have no idea why. Any detailed answer, explaining how and why you change the color of your sprite would be welcome:
This is my code:


import pygame

pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
width = 800
height = 600
FPS = 100
font = pygame.font.SysFont(None, 25)

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Second snake game.")
clock = pygame.time.Clock()

def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [width/2, height/2])

def gameloop():
exitGame = False
lead_x = width/2
lead_y = height/2
lead_x_change = 0
lead_y_change = 0
block_size = 10
velocity = 0.1
rectangle = pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])

while not exitGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
lead_y_change += -velocity
lead_x_change = 0
if event.key == pygame.K_s:
lead_y_change += velocity
lead_x_change = 0
if event.key == pygame.K_a:
lead_x_change += -velocity
lead_y_change = 0
if event.key == pygame.K_d:
lead_x_change += velocity
lead_y_change = 0
if event.key == pygame.K_BACKSPACE:
rectangle.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
lead_x_change = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
lead_y_change = 0


lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.display.update()
clock.tick(FPS)
gameloop()





Please, edit and add the full code in ``
– SamperMan
Aug 28 at 22:26





1 Answer
1



First, put the rendering functions in an appropriate order and put them in your while loop:


gameDisplay.fill(white) # Display fill comes first
pygame.draw.rect(gameDisplay, black, rectangle) # Then draw your objects
pygame.display.flip() # Update the screen



Then, define the pygame.Rect object properly


rectangle = pygame.Rect(lead_x, lead_y, block_size, block_size)



This is what you are probably trying to find (with my few fixes):


import pygame
pygame.init()

# Constants
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WIDTH = 800
HEIGHT = 600
FPS = 100
FONT = pygame.font.SysFont(None, 25)
BLOCK_SIZE = 10
VELOCITY = 10

gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Second snake game.")
clock = pygame.time.Clock()

def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [width/2, height/2])

def gameloop():
exitGame = False
x = WIDTH / 2
y = HEIGHT / 2
rectangle = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
rectangle_color = BLACK

while not exitGame:
clock.tick(FPS)

# Pygame events
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
pygame.quit()
quit()

if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
y -= VELOCITY
if event.key == pygame.K_s:
y += VELOCITY
if event.key == pygame.K_a:
x -= VELOCITY
if event.key == pygame.K_d:
x += VELOCITY
if event.key == pygame.K_BACKSPACE:
rectangle_color = RED if rectangle_color == BLACK else BLACK

# Update rect coords
rectangle = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)

# Render everything
gameDisplay.fill(WHITE)
pygame.draw.rect(gameDisplay, rectangle_color, rectangle)
pygame.display.flip()

gameloop()





Thanks, I can now see and move around what I've drawn, but I'm still unable to change the color of the sprite I drew! I want to do that after pressing backspace So something like: If backspace pressed: change sprite color (This is the code I have) if event.key == pygame.K_SPACE: pygame.draw.rect(gameDisplay, red, [lead_x, lead_y, block_size, block_size])
– Lol Lol
Aug 28 at 22:58





Edited, try it yourself.
– SamperMan
Aug 28 at 23:01





I think the problem has to do with where I'm updating the screen, as the block appears but it's updated out of the screen right away.
– Lol Lol
Aug 28 at 23:02





saw the edit now, thanks!
– Lol Lol
Aug 28 at 23:03






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)