I want to make an image white on keypress in pygame


Own Shees:

I am trying to create a Tic tac toe game in pygame. I want to display an image on the screen when a key is pressed. Pressing the escape key quits the game, but when I press key 1 the image goes black, nothing happens. this is the code

import pygame
from pygame.locals import (
    K_1,
    K_2,
    K_3,
    K_4,
    K_5,
    K_6,
    K_7,
    K_8,
    K_9,
    K_ESCAPE,
    K_UP,
    KEYDOWN,
    QUIT
)
clock = pygame.time.Clock()
pygame.init()

# Constants
display_width = 300
display_height = 300
white = (255, 255, 255)
black = (0, 0, 0)
run = True

# Creating game screen
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption("Tic Tac Toe Game")
display.fill(white)

# importing images to pygame
circle_image = pygame.image.load("resizedimage1.png")
cross_image = pygame.image.load("resizedimage2.png")

# main game loop
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                run = False
            elif event.key == K_1:
                display.blit(cross_image, (10, 10))
                clock.tick(30)

    display.fill(white)
    pygame.draw.line(display, black, (100, 0), (100, 300))
    pygame.draw.line(display, black, (200, 0), (200, 300))
    pygame.draw.line(display, black, (0, 100), (300, 100))
    pygame.draw.line(display, black, (0, 200), (300, 200))
    pygame.display.update()

pygame.quit()
Christian:

you have to put the line

display.fill(white)

at the top of your runtime. Because now you are rendering the picture and in the next frame everything is drawn white. Therefore, you will not be able to see the image.

You should also use the event KEYUP. Because KEYDOWN will be triggered multiple times, it will cause a flickering effect.

Related


I want to make an image white on keypress in pygame

Own Shees: I am trying to create a Tic tac toe game in pygame. I want to display an image on the screen when a key is pressed. Pressing the escape key quits the game, but when I press key 1 the image goes black, nothing happens. this is the code import pygame

Pygame - How to make an image white but change color?

Potential I have a program for a random flag generator. I made a white "background" for the flag and made it white with different symbols etc. The reason they are white is that I can color them with RGB values. The problem is, I'm not sure how to do this (if i

Pygame - How to make an image white but change color?

Potential I have a program for a random flag generator. I made a white "background" for the flag and made it white with different symbols etc. The reason they are white is that I can color them with RGB values. The problem is, I'm not sure how to do this (if i

Pygame - How to make an image white but change color?

Potential I have a program for a random flag generator. I made a white "background" for the flag and made it white with different symbols etc. The reason they are white is that I can color them with RGB values. The problem is, I'm not sure how to do this (if i

How can I make it into the new scene of the keypress? (Pygame)

gap I'm pretty new and I can't seem to find any tutorials on how to do this. I think it will be very simple. I'm creating an Oregon Trail type game and I need it to go to the next picture when you press E, or quit the program when you press Q. Here is my code:

How can I make it into the new scene of the keypress? (Pygame)

gap I'm pretty new and I can't seem to find any tutorials on how to do this. I think it will be very simple. I'm creating an Oregon Trail type game and I need it to go to the next picture when you press E, or quit the program when you press Q. Here is my code:

I want to draw a circle on an image in pygame

00 holes I want to draw a white circle on the image when the mouse is over the image. So I made this function. However, when I do this, the image loads without the circle. can you help me? def imgbutton(name, size, x, y): active = False (mx, my) = pygame.m

I want to draw a circle on an image in pygame

00 holes I want to draw a white circle on the image when the mouse is over the image. So I made this function. However, when I do this, the image loads without the circle. can you help me? def imgbutton(name, size, x, y): active = False (mx, my) = pygame.m

I want to draw a circle on an image in pygame

00 holes I want to draw a white circle on the image when the mouse is over the image. So I made this function. However, when I do this, the image loads without the circle. can you help me? def imgbutton(name, size, x, y): active = False (mx, my) = pygame.m

I want to draw a circle on an image in pygame

00 holes I want to draw a white circle on the image when the mouse is over the image. So I made this function. However, when I do this, the image loads without the circle. can you help me? def imgbutton(name, size, x, y): active = False (mx, my) = pygame.m

I want to draw a circle on an image in pygame

00 holes I want to draw a white circle on the image when the mouse is over the image. So I made this function. However, when I do this, the image loads without the circle. can you help me? def imgbutton(name, size, x, y): active = False (mx, my) = pygame.m

I want to draw a circle on an image in pygame

00 holes I want to draw a white circle on the image when the mouse is over the image. So I made this function. However, when I do this, the image loads without the circle. can you help me? def imgbutton(name, size, x, y): active = False (mx, my) = pygame.m

I want to make border like attached image

username I want to make border like attached image in my app. Please help me as well. The original screen is the same as the code below. I am going to make the first screen image. tell me the changes in the xml. How can I make the border like a scary image? cu

I want to make border like attached image

username I want to make border like attached image in my app. Please help me as well. The original screen is the same as the code below. I am going to make the first screen image. tell me the changes in the xml. How can I make the border like a scary image? cu

How can I remove the image and then make it white again?

Niki Gholipour I'm creating a game with pygame and I'm running into a problem. My game is a space game: you have meteors coming at you, you have to dodge them, and you can shoot missiles at them. When the missile hits the meteor, I'm going to make the missile

How can I remove the image and then make it white again?

Niki Gholipour I'm creating a game with pygame and I'm running into a problem. My game is a space game: you have meteors coming at you, you have to dodge them, and you can shoot missiles at them. When the missile hits the meteor, I'm going to make the missile

How can I remove the image and then make it white again?

Niki Gholipour I'm creating a game with pygame and I'm running into a problem. My game is a space game: you have meteors coming at you, you have to dodge them, and you can shoot missiles at them. When the missile hits the meteor, I'm going to make the missile