I'm trying to run code from a tutorial I found online.
But when I run this code, my background image does not appear, even if it loaded correctly.
Why?
bif = "castle.jpg"
import pygame, sys, pygame.mixer
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1280,960),0, 32)
background = pygame.image.load(bif).convert()
pygame.display.set_caption("castlevania ultimate")
hit_sound = pygame.mixer.Sound("02.wav")
hit = False
if hit is True:
hit_sound.play()
sound = pygame.mixer.Sound("castlevania_1.wav")
sound.play()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
You never call pygame.display.update() in your while loop.
Python is indentation sensitive, so the loop should look like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
Note that pygame.display.update() is now inside the while loop.
Related
I'm trying to add a title screen to my game in pygame using a title_screen function but whenever I call it it doesn't actually load the title screen, what am I doing wrong?
This is my code for the title screen function:
#started
started = False
#declaring surface
window = pygame.display.set_mode((WIDTH,HEIGHT), 0, 32)
pygame.display.set_caption('Pong')
def title_screen(canvas):
canvas.fill(BLACK)
#NOT WORKING
and this is how I'm calling it + how I'm calling the rest of the code for my game
while not started:
title_screen(window)
pygame.display.update()
fps.tick(60)
init()
while True:
draw(window)
for event in pygame.event.get():
if event.type == KEYDOWN:
keydown(event)
elif event.type == KEYUP:
keyup(event)
elif event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fps.tick(60)
try to do pygame.init() before declaring window as an surface.
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.draw.rect(screen, (255,0,0),(400,300,50,50))
pygame.display.flip()
As #Furas writes, the screen update code is not being called from within the loop. Python uses indentation to designate code blocks, so if the function call (or other code section) is not indented to the correct column, it is literally a completely different set of operations.
Since a piece of sample code is worth a thousand words:
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
# Handle user-events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Re-draw the screen
pygame.draw.rect(screen, (255,0,0), (400,300,50,50))
pygame.display.flip()
pygame.quit()
sys.exit()
I'm relatively new to python and very new to pygame. I'm trying to use pygame. All programs seem to work fine, except when I try to quit. The window freezes ("application not responding") and I have to force quit it. I'm using OSX, python 3.6, and running it through sublime text if that matters. Code is below:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
done = True
pygame.display.quit()
pygame.quit()
Thanks for your help!
Try this one, it works for me:
import sys
import pygame
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
you can just do this:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
this is what i do
or you can use this:
import pygame
pygame.init()
running = True
width, heigth = 800, 600
size = (width, heigth)
screen = pygame.display.set_mode(size)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
don't use pygame.display.quit()
I have created a window in pygame, the only problem is that I can't exit the window without it crashing. Is there a problem with my code that causes this?
import pygame
pygame.init()
gamedisplay = pygame.display.set_mode ((800,600))
pygame.display.set_caption ('snake')
pygame.display.update()
while True:
pass
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.quit()
quit()
Get rid of this:
while True:
pass
I don't know why this is happening. I've tested on other computers and it works fine. This is what it looks like:
Code below, didn't think it mattered as it worked on other computers.
pygame.init()
pygame.display.set_caption("Pixelites")
surface = pygame.display.set_mode((WIDTH,HEIGHT), 0, 32)
CLOCK = pygame.time.Clock()
tiles=defaultTiles(surface,WIDTH,HEIGHT)
while True:
surface.fill((0,0,0))
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
pass
if event.type == QUIT:
pygame.quit()
sys.exit()
for t in tiles:
t.draw(surface)
pygame.display.update()
CLOCK.tick(1000)
Didn't update the surface. Oops.