I am trying to change the default color from black to white.
not to fill it.
My current code where I fill it:
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE)
surface.fill((255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.update()
It works however it is being shown as black for a second and then it changes itself, and i am looking for a solution where in advance it starts with white background. Thanks in advance
Maybe this will help you
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE)
surface.fill((255, 255, 255))
while True:
surface.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.update()
By #starbuck45 I used the flag pygame.HIDDEN.
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE | pygame.HIDDEN)
surface.fill("white")
pygame.display.set_mode(size)
line = pygame.draw.line(surface, "black", (0, 0), (600, 600))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
and it worked fine!
Related
I was trying to make the display a diferent colour in pygame but the screen just stays black
this is the code i had for it:
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
I was following a tutorial on youtube but when i tried to make any colour it just stayed black.
I am following the same tutorial and you have to make sure that your screen.fill and display.udpate command are in your while loop at the right position under the for event like this:
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
Output:
The output should give you a blue background like above.
Make sure the screen.fill((0, 0, 255)) and the pygame.display.update() are in the game loop (the while running loop).
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
pygame.display.set_caption("space invaders")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
Also, if no action or animation is being display that would require the computer to do anything other than just loop through like normal, you can use the pygame.event.wait() method.
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
pygame.display.set_caption("space invaders")
running = True
while running:
event = pygame.event.wait()
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
So I'm learning pygame and I want to use pixel art for the sprites and I'm trying to scale up my display but it seems it's drawing behind the main display instead of Infront of it
import sys, math
import pygame
from pygame.locals import *
pygame.init()
fps = 60
is_running = True
clock = pygame.time.Clock()
window_size = [600, 400]
scale_size = [300, 200]
screen = pygame.display.set_mode(window_size)
display = pygame.Surface(scale_size)
player_img = pygame.image.load("images/player.png")
player_pos = [150, 100]
player_move_speed = 3
player_y_momentum = 1
while is_running:
for event in pygame.event.get():
if event.type == QUIT:
is_running = False
pygame.quit()
sys.exit()
surf = pygame.transform.scale(display, window_size)
screen.blit(surf, (0, 0))
surf.blit(player_img, player_pos)
surf.fill((255, 255, 255))
pygame.display.update()
clock.tick(fps)
I've tried to troubleshoot the problem but I can't seem to find the solution
The order of drawing/filling is important!
Change the order to the following:
while is_running:
for event in pygame.event.get():
if event.type == QUIT:
is_running = False
pygame.quit()
sys.exit()
surf = pygame.transform.scale(display, window_size)
surf.fill((255, 255, 255))
surf.blit(player_img, player_pos)
screen.blit(surf, (0, 0))
pygame.display.update()
clock.tick(fps)
I an having an error with MOUSEBUTTONDOWN giving an error when used.
Here is the error:
if event.type == MOUSEBUTTONDOWN:
NameError: name 'MOUSEBUTTONDOWN' is not defined
Here is the code that is giving the error:
import pygame
import random
import sys
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(WHITE)
# --- Drawing code should go here
monospace = pygame.font.SysFont("Berlin Sans FB", 169)
label = monospace.render("test", 1, (0, 0, 0))
screen.blit(label, (100, 100))
button_rect = pygame.Rect(0, 0, 50, 50) # start button rectangle
abort = False
start = False
while not abort and not start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
abort = True
if event.type == MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
start = True
# draw title screen
# [...]
done = abort
while not done:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
All the other sources on Stack Overflow about this topic that I can find all talk about errors where MOUSEBUTTONDOWN already works.
Also note that I am using Pycharm.
Either pygame.MOUSEBUTTONDOWN instead of MOUSEBUTTONDOWN or from pygame.locals import *
You need only one application loop:
import pygame
from pygame.locals import *
import random
import sys
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("game")
clock = pygame.time.Clock()
monospace = pygame.font.SysFont("Berlin Sans FB", 169)
label = monospace.render("test", 1, (0, 0, 0))
button_rect = pygame.Rect(0, 0, 50, 50) # start button rectangle
abort = False
start = False
while not abort:
for event in pygame.event.get():
if event.type == pygame.QUIT:
abort = True
if event.type == MOUSEBUTTONDOWN:
if not start and button_rect.collidepoint(event.pos):
start = True
screen.fill(WHITE)
if not start:
# draw title screen
pygame.draw.rect(screen, "red", button_rect)
else:
# draw game
# [...]
screen.blit(label, (100, 100))
pygame.display.flip()
clock.tick(60)
pygame.quit()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
Solution found:
I did not have the needed libraries imported:
For MOUSEBUTTONDOWN to work you need to use:
from pygame.locals import *
Credit to #Rabbid76 for answering in comments.
I am having a weird display when I run a simple Pygame. The game runs perfectly but the screen is weird.
Here's the code:
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('First Game')
pygame.display.update()
x = 50
y = 50
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(window, (255, 0, 0), (x, y, width, height))
pygame.quit()
You need to update the screen every frame. Put pygame.display.update() inside your main_loop, and remove it from original positon:
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(window, (255, 0, 0), (x, y, width, height))
pygame.display.update()
This should work
Hi I'm having some trouble realizing my ideas. I first wanted to draw rectangles with two mouse clicks, but it didn't work properly so i reduced it to this: to draw fixed-size rectangles with one mouse click.
However it still doesn't work/...
import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)
pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1
while running:
screen.fill(white)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = 0
THE PROBLEM IS HERE SOMEWHERRE
elif event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(event.dict["pos"],(30,50))
pygame.draw.rect(screen,black,rect,1)
pygame.display.flip()
I know there might be a lot of conceptual errors with my code... please help!
You are filling white the entire screen every tick. So after you actually draw the screen become blank again on the next tick. Just move screen.fill(white) out of main cycle:
import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)
pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1
screen.fill(white)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(event.dict["pos"],(30,50))
pygame.draw.rect(screen,black,rect,1)
pygame.display.flip()