Background in pygame not working [duplicate] - python

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)

Related

cannot blit anything to screen. why not? [duplicate]

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)

Why is my Pygame script not correctly filling the screen? [duplicate]

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()

Why does Pygame keep closing when I add more code? [duplicate]

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!

The png image moves way too fast, how can I slow it down ? (python) [duplicate]

This question already has answers here:
Why is my pygame application loop not working properly?
(1 answer)
Why is my PyGame application not running at all?
(2 answers)
Closed 2 years ago.
I'm writing a program for school, but when I press a specific key to make my laser move across the screen, it disappears instantly. How can I make it move slowly, so the player can see it going from the left side to the right side of the screen ?
Here's the code (I wrote it again so I had only the part doing the laser moves)
#
import pygame, time
from pygame_functions import*
pygame.init()
win = pygame.display.set_mode((1440, 480))
laserImg = pygame.image.load('laser.png')
backgroundImg = pygame.image.load('bg.jpg')
laserX = 90
laserY = 400
clock = pygame.time.Clock()
run = True
while run:
clock.tick(14)
win.blit(backgroundImg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
win.blit(laserImg, (laserX, laserY))
if event.type == pygame.KEYDOWN:
print("touche")
if keys[pygame.K_UP]:
while laserX < 1500:
laserX += 10
pygame.display.update()
pygame.quit
#
Thanks if you can help me
edit : idk why but there wasn't the line saying "while laserX < 1500"
Try something like this:
run = True
laser_moving=False
while run:
clock.tick(14)
win.blit(backgroundImg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
win.blit(laserImg, (laserX, laserY))
if event.type == pygame.KEYDOWN:
print("touche")
if keys[pygame.K_UP]:
laser_moving=True
if laser_moving:
laser_x+=1
if laser_x > 1600:
laser_moving=False
Your issue is that you immediately move the laser outside the screen in your loop, without waiting for the next frame to be drawn in between. You need to exit your loop so the next frame can be drawn and the intermediate states of the laser are visible.

Windows/Python pygame.error: video system not initialized after adding pygame.init() [duplicate]

This question already has answers here:
Windows/Python pygame.error: video system not initialized after adding Mp3 file
(2 answers)
Closed 5 years ago.
I added some music to my pygame game and pygame.init() to the script to initialize the video system before it is called, but I think the code is so messy that nothing is in the right place even after moving everything to where it needs to be. As a result of this addition, I'm now getting this error still after adding pygame.init():
Traceback (most recent call last):
File "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\My First game ERROR.py", line 31,
in for event in pygame.event.get():
pygame.error: video system not initialized
Here is the code that I have written:
# This just imports all the Pygame modules
import pygame
pygame.init()
class Game(object):
def main(self, screen):
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('St.Patrick game')
Game().main(screen)
clock = pygame.time.Clock()
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.quit():
pygame.quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
pygame.mixer.init(44100, -16,2,2048)
import time
pygame.mixer.music.load('The Tonight Show Star Wars The Bee Gees Stayin Alive Shortened.mp3')
pygame.mixer.music.play(-1, 0.0)
#class Player(pygame.sprite.Sprite):
# def __init__(self, *groups):
# super(Player, self.__init__(*groups)
#self.image = pygame.image.load('Sprite-01.png')
# self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
#def update(self):
# key = pygame
image = pygame.image.load('Sprite-01.png')
# initialize variables
image_x = 0
image_y = 0
image_x += 0
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
image_x -= 10
if key[pygame.K_RIGHT]:
image_x += 10
if key[pygame.K_UP]:
image_y -= 10
if key[pygame.K_DOWN]:
image_y += 10
screen.fill((200, 200, 200))
screen.blit(image, (image_x, image_y))
pygame.display.flip()
pygame.mixer.music.stop(52)
Your problem can be pygame.quit() inside while loop.
pygame.quit() uninitializes modules initialized with pygame.init() - but it doesn't exit while loop so while-loop tries to use event.get() in next loop. And then you get problem because you uninitialized modules.
Besides, it makes no sense
if event.type == pygame.quit():
it has to be
if event.type == pygame.QUIT:
pygame.quit() is function which ends what pygame.init() started.
pygame.QUIT is constant value - try print(pygame.QUIT) - which you can compare with event.type.
We use UPPER_CASE_NAMES for constant values. Read: PEP 8 -- Style Guide for Python Code
Finally, you need rather
running = True
while running:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
So it exits loop but it doesn't uninitialize modules which you will need in rest of code.

Categories