This question already has answers here:
Python Pygame Newbie Code Showing Blank Screen
(4 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 10 months ago.
so im trying to blit tonk but its not working. can you help? all it shows is black
import pygame,pynput
pygame.init()
width = 700
height = 700
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('generic shooter pew pew')
Tonk = pygame.image.load("TONK.png")
screen.blit(Tonk,(0,0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
clock = pygame.time.Clock()
clock.tick(60)
You need to blit in the while loop and add a display update too :
import pygame,pynput
pygame.init()
width = 700
height = 700
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('generic shooter pew pew')
Tonk = pygame.image.load("TONK.png")
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
screen.blit(Tonk,(0,0))
pygame.display.flip()
clock.tick(60)
Related
This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
My code is below, when it is run the Pygame screen stays black and is not filled with the correct color at all, (87, 160, 211)
import pygame
pygame.init()
window_size = (800,600)
screen = pygame.display.set_mode((800,600))
car_image = pygame.image.load('car.png')
def display_car(x,y):
screen.blit(car_image, (x,y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((87, 160, 211))
display_car(window_size[0]/2, window_size[1]/2)
You must update the display by calling either pygame.display.update() or pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((87, 160, 211))
display_car(window_size[0]/2, window_size[1]/2)
pygame.display.flip()
This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 1 year ago.
So basically, its all well and good when I have it at this stage:
import pygame
from pygame.locals import *
pygame.init()
screen_width = 1000
screen_height = 1000
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Platformer')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
but as soon as i add something like this:
import pygame
from pygame.locals import *
pygame.init()
screen_width = 1000
screen_height = 1000
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Platformer')
sun_img = pygame.image.load('img/sun.png')
bg_img = pygame.image.load('img/sky.png')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
it just opens and closes immediately.
Im a huge novice with code and am just having some fun for now but this issue is really annoying and I would appreciate any help I could get!
This question already has answers here:
Pygame unresponsive display
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
When I run the code a blue rect is displayed in its proper location however the entire window freezes and eventually crashes. How can I fix this?
import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
pygame.time.delay(100)
filled_rect = pygame.Rect(100, 100, 25, 25)
pygame.draw.rect(win, (0,0,255), filled_rect)
pygame.display.update()
You have to add an event loop. Handle the events, by either pygame.event.pump() or pygame.event.get(). Thus the IO and internal events are handled and the window keeps responding. e.g.:
import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
pygame.time.delay(100)
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the disaply
win.fill(0)
# draw the scene
filled_rect = pygame.Rect(100, 100, 25, 25)
pygame.draw.rect(win, (0,0,255), filled_rect)
# update the dispaly
pygame.display.update()
pygame.quit()
This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Why is my PyGame application not running at all?
(2 answers)
Closed 2 years ago.
import pygame
import sys
import os
basic stuff
'''
Objects
'''
# put Python classes and functions here
'''
Setup #...
'''
# put run-once code here
fps = 60
ani = 4
clock = pygame.time.Clock()
pygame.init()
I think here might be a problem:
On the internet I saw something with .convert() but it keeps giving me :
AttributeError: 'str' object has no attribute 'convert'
when I try to put .conver()
it's been 2 hours of searching and I can't find this noob stuff...
background = pygame.image.load(os.path.join('background.png'))
size = (width, height) = background.get_size()
world = pygame.display.set_mode(size)
...
'''
Main Loop
'''
or here:
world.blit(background, (0,0))
...
while main == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
pygame.display.flip()
clock.tick(fps)
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)