i have started a new project in python using pygame and for the background i want the bottom half filled with gray and the top black. i have used rect drawing in projects before but for some reason it seems to be broken? i don't know what i am doing wrong. the weirdest thing is that the result is different every time i run the program. sometimes there is only a black screen and sometimes a gray rectangle covers part of the screen, but never half of the screen.
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
You need to update the display.
You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().
See pygame.display.flip():
This will update the contents of the entire display.
While pygame.display.flip() will update the contents of the entire display, pygame.display.update() allows updating only a portion of the screen to updated, instead of the entire area. pygame.display.update() is an optimized version of pygame.display.flip() for software displays, but doesn't work for hardware accelerated displays.
The typical PyGame application loop has to:
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 (draw all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == QUIT:
run = False
# clear display
DISPLAY.fill(0)
# draw scene
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
exit()
repl.it/#Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop
simply change your code to:
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))
pygame.display.flip() #Refreshing screen
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
it should help
Related
I would like to know how to draw images using pygame. I know how to load them.
I have made a blank window.
When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.
This is a typical layout:
myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()
while 1:
your_code_here
screen.fill(black)
screen.blit(myimage, imagerect)
pygame.display.flip()
import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))
player = pygame.image.load(os.path.join("player.png"))
player.convert()
while True:
screen.blit(player, (10, 10))
pygame.display.flip()
pygame.quit()
Loads the file player.png.
Run this and it works perfectly. So hopefully you learn something.
Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:
my_image_surface = pygame.load.image('my_image.jpg')
However, the pygame documentation notes that:
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.
Use the appropriate conversion method for best performance:
image_surface = pygame.load.image('my_image.jpg').convert()
alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()
A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:
window_surface.blit(image_surface, (x, y))
window_surface.blit(image_surface,
image_surface.get_rect(center = window_surface.get_rect().center))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygameSurface = pygame.image.load('apple.png').convert_alpha()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.
If you want to use images in PyGame that are loaded with other libraries, see:
PIL and pygame.image
How do I convert an OpenCV (cv2) image (BGR and BGRA) to a pygame.Surface object
For information on loading Scalable Vector Graphics (SVG) files, see:
SVG rendering in a PyGame application
Loading animated GIF files is presented at:
How can I load an animated GIF and get all of the individual frames in PyGame?
How do I make a sprite as a gif in pygame?
Or see how to load NumPy frames:
Pygame and Numpy Animations
After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.
I recently changed the way my pygame game handles menus. Any text I'm rendering is blit to a surface called guiSurface before it is blit to the display (screen).
The problem is that this causes the text to appear aliased, whereas blitting it directly to the display didn't have this issue.
With guiSurface (screenshot):
menuFont = pygame.font.Font('assets/MarkPro.otf',15,bold=False,italic=False)
menuText = menuFont.render(version, 1, (255,255,255))
guiSurface.blit(menuText,(790-(menuText.get_size()[0]),475))
screen.blit(guiSurface, (0,0))
pygame.display.update()
Directly to display (screenshot):
menuFont = pygame.font.Font('assets/MarkPro.otf',15,bold=False,italic=False)
menuText = menuFont.render(version, 1, (255,255,255))
screen.blit(menuText,(790-(menuText.get_size()[0]),475))
screen.blit(guiSurface, (0,0))
pygame.display.update()
I've tried using .convert() and .convert_alpha() on both the menuText and guiSurface surfaces, after line 2 in both code snippets, but to no avail. And I have no idea at which point the anti-aliasing is lost.
Help is of course appreciated, and thanks in advance.
The fault is mine. I didn't clear guiSurface after blitting it to the display, meaning I was drawing over what was already there instead of a fresh surface.
I'm learning how to use pygame, and I'm just trying to open up a window for the game I'm creating.
The program compiles fine, and I tried drawing a circle to see if that would change anything but in both scenarios I still just get a blank window that freezes. My computer has plenty of storage space and only 2 applications open, and yet this is the only application that is freezing.
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
I have to force quit Python because it stops responding. I have Python version 3.7.4 and Pygame version 1.9.6.
Any advice?
A minimal, typical PyGame application
needs a game loop
has to handle the events, by either pygame.event.pump() or pygame.event.get().
has to update the Surface whuch represents the display respectively window, by either pygame.display.flip() or pygame.display.update().
See also Python Pygame Introduction
Simple example, which draws a red circle in the center of the window: repl.it/#Rabbid76/PyGame-MinimalApplicationLoop
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
# main application loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the display
window.fill(0)
# draw the scene
pygame.draw.circle(window, (255, 0, 0), (250, 250), 100)
# update the display
pygame.display.flip()
Hi i want to ask a question which has been answered a lot of times on this site but i never found an appreciable answer.How to remove the images in Pygame if there is already a background image in game window.
In most of the answers,they say to use screen.fill(color) but it makes the area black.It works fine if there is no background image but with background image,it looks odd when only a certain region is colored black.How can i get rid of it?Is there an alternative way to remove the image in this particular situation.By the way I'm not adding any specific code here because i deal with this issue a lot of time while building games.
You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:
The main application loop has to:
handle the events by 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 either pygame.display.update() or pygame.display.flip()
A minimum application is
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game states and move objects
# [...]
# clear the display
window.fill(0) # or `blit` the back ground image instead
# draw the scene - draw all the objects
# [...]
# update the display
pygame.display.flip()
I'm still pretty new to python and have been trying to learn it in my spare time. I'm starting a new project, using pygame, and I was using the draw function to start laying out my UI. here is the total code:
import os, sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode([1080, 720])
Clock = pygame.time.Clock()
def Mainloop():
while 1:
Clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
else:
pass
screen.fill([255, 255, 255])
pygame.draw.line(screen, [0, 0, 0], [0, 520], [1080, 520], 8)
pygame.draw.line(screen, [0, 0, 0], [750, 0], [750, 520], 8)
pygame.draw.circle(screen, [0, 0, 0], [200, 260], 80, 8)
pygame.display.flip()
Mainloop()
The problem is that no matter what thickness I set my circle to, it's black outline is sprinkled with white pixels. I just want a solid black outline on my circle. I'm sorry if this is stupid but I have been unable to find anything on this specific issue.
Thank you in advance for the help.
EDIT: Here is a pic of of the screen I am getting: http://imgur.com/xGvdrhd (It was cropped some though)
Unfortunately you can't fix this problem. I think this is actually a glitch in pygame itself, because I've checked your code and there's nothing wrong with it. I even went and drew my own circle with pygame and the same problem persisted with me too. I also noticed that these 'white sprinkles' are whatever colour the background is. Because this is an error in pygame itself, either the creator is going to have to do something about it or you will. Now I'm not too experienced with this kind of thing but what I know for a fact that it will be no easy task. If you ever need a thick circle for whatever reason, then I would make it using something like GIMP or Photoshop then transfer it as an image in pygame- or like I said you could find a way to fix this poblem by looking at the pygame source code, but it's no easy task for a beginner.