I'm currently trying to follow the Introduction to Pygame tutorial and I'm stuck on one step where the speaker makes the surface. His surface is bright red while my surface isn't visible at all.
Here's the code:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(test_surface, (0, 0))
pygame.display.update()
clock.tick(60)
I've already tried starting from the very beginning, my code and the steps I make are identical to his. I've also tried deleting python and pygame. I've installed Python version that the speaker has (3.9), but nothing helps.
It is a matter of indentation. You have draw the scene and update the display in the application loop:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# INDENTATION
#<------|
screen.blit(test_surface, (0, 0))
pygame.display.update()
clock.tick(60)
Related
I made a window in pygame, tried to make the window white and even though I only call pygame.display.update() once it keeps flashing between white and black.
This is my code:
import pygame
running = True
pygame.init()
White = (255, 255, 255)
while running == True:
screen = pygame.display.set_mode((1400, 800))
screen.fill(White)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
I tried changing it to pygame.display.flip() but it still stayed the same.
I also tried to put pygame.display.update() out of the loop but then the screen just stayed black.
You need to create the Pygame window (pygame.display.set_mode) once before the application loop instead of constantly creating a new window in the application loop:
import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((1400, 800))
White = (255, 255, 255)
while running == True:
screen.fill(White)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
(i followed a yt tut so idk) my code: idk if i put code in wrong place
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
player_sprite = pygame.image.load("will smith.png") .convert_alpha
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
pygame.display.update()
pygame.quit()
quit()
not really sure why it does this, im new to python so idk
There are multiple things that needs to fixed. First of all to you actually need to blit the image on the screen.
while not exit:
for event in pygame.event.get():
#...[block of code]
display.fill(white)
display.blit(player_sprite, (100,100)) # BLIT IMAGE TO SCREEN
.convert_alpha() is a method and you need to call it after you initialize pygame and set the video mode.
You can read more about convert_alpha() here
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
#converting it after init and setting game mode
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Here's the final working code
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# print(event)
display.fill(white)
display.blit(player_sprite, (100,100))
pygame.display.update()
pygame.quit()
quit()
You have to blit the image in the application loop, after clearing the display and before updating the display:
display.blit(player_sprite, (100, 100))
convert_alpha is function. You missed the parentheses
player_sprite = pygame.image.load("will smith.png").convert_alpha
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Also, you cannot call a pygame function before you initialize pygame. pygame.init() should be the very first pygame function called.
Complete example:
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
display.blit(player_sprite, (100, 100))
pygame.display.update()
pygame.quit()
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()
I'm new to PyGame (and python in general) and I am just trying to get a window to pop up. That's all I'm after for now. Here's my code:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
I am using Python 3.7.0 in Pycharm and PyGame 1.9.4.
Following Python's PyGame tutorial, your next step is to start a while loop to handle the game frames:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
clock = pygame.time.Clock() # Determine FPS (frames-per-second)
crashed = False
# Game loop
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
Because you need to first put it inside a loop (so it refreshes whatever is inside it), that way it stays on until something alters the condition of that loop.
# Event loop (HERE YOU PUT IT).
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()
My code runs with no errors, but the background image does not display and neither does the caption at the top of the window. I have checked that my script is importing the correct pygame module, and tried different images. I have Python 3.6.2 on a Mac.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Users/MyMacbook/Desktop/pac-man/background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()
Yeah it appears that #skrx is correct, there's something blocking by not clearing the event buffer. This code works for me:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Path/To/Background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()
I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame.
I saved a png image in the same folder as my py script. When I run the script it shows no error but my image does not shows in pygame window. Kindly advice
Here's the script
import pygame,sys
pygame.init()
WIDTH,HEIGHT = 640,360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock()
FPS = 24
dog_img = pygame.image.load("dog.png")
#PROCESS
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#PROCESS
#LOGIC
#LOGIC
#DRAW
screen.blit(dog_img,(0,0))
pygame.display.flip()
#DRAW
clock.tick(FPS)
Fix the indentation. screen.blit(dog_img, (0, 0)) and the two lines below should be inside of the while loop (indented with 4 spaces).
import pygame,sys
pygame.init()
WIDTH, HEIGHT = 640, 360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock()
FPS = 24
# Always use `.convert()` or `.convert_alpha()`. It'll improve the performance.
dog_img = pygame.image.load("dog.png").convert_alpha()
#PROCESS
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#LOGIC
#DRAW
screen.blit(dog_img, (0, 0))
pygame.display.flip()
clock.tick(FPS)