This question already has answers here:
Problems getting pygame to show anything but a blank screen on Macos
(10 answers)
pygame installation issue in mac os
(5 answers)
Closed 2 years ago.
I wanted to blit an image in python, a png image. But for some reason about half the image has the wrong pixel colours, it looks all messy, having random coloured pixels in it. I have made a few games on my Macbook Pro (OSX) using pygame with python 3.4, and I had no trouble when it came to blitting images. Here's the code:
import pygame
import os
w = pygame.display.set_mode((300,300))
bug = os.path.join("/Users/Snyman/Desktop/images/double-block_spm_R.png")
bug = pygame.image.load(bug).convert_alpha()
bug = pygame.transform.scale(bug,(64,64))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
w.fill((255,255,255))
w.blit(bug,(10,10))
pygame.display.update()
pygame.quit()
"bug" is my image
Related
This question already has answers here:
How to add a background image into pygame?
(3 answers)
How to scale an image by window size?
(1 answer)
Closed 3 months ago.
I'm trying to make some game similar to Space invaders but they come around from everywhere including sides and bottom and the player can only rotate the ship to shoot lasers at the enemies. Right now, I'm trying to add a background to it to give the game some atmosphere but whatever guides I lookup or try doesn't work. Could anyone show me what I'm supposed to do to get my loaded image onto the background surface? Sorry if dumb question, I'm new to PyGame.
This here is my code so far (without my previous attempts at adding the background)
# Import the Pygame module
import pygame
# Intialize
pygame.init()
# Variables
image = pygame.image.load('Spaceship.gif')
bg = pygame.image.load('bg.gif')
# Setup
pygame.display.set_mode([1000,500])
pygame.display.set_icon(image)
pygame.display.set_caption("Space Man Type Beat")
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
# Quit
pygame.quit()
This question already has answers here:
Python - error: couldn't open .png file [duplicate]
(4 answers)
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Closed 10 months ago.
I'm coding in python with pygame, but my images won't open. When I try to load an image, this error message pops up: Exception has occurred: error Couldn't open player0.png. I have looked at other people's solutions, but none of them worked. I have tried converting the image into .bmp and .jpg, but it made no difference and gave me the same error message. I have also tried to put it into a separate folder and do this: img = pygame.image.load('assets/player0.png'), but it made no difference.
This is my code currently:
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800,600))
player = pygame.image.load('player0.png')
while True:
screen.fill((255,255,255))
screen.blit(player, (100,100))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Make sure the path to the image is correct.
Check out this solution already posted.
Python - error: couldn't open .png file
This question already has an answer here:
Blurring in PyGame
(1 answer)
Closed 2 years ago.
As a beginner in pygame, I am trying to build a game and want to blur the background as the game ends and show player his/her score and ask if he/she wants to play again.
I don't know how to blur the background in pygame. Can anyone help me?
Thank You.
To blur the background in PyGame, you can try using the Python Imaging Library (PIL). Here's a link which has sample code on how to do it:
stackoverflow.com/questions/30723253/blurring-in-pygame
Hope this helps!
Maybe try to insert an video that blur at the end unfortunately i dont think there is an blurring function.
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('the bluring video.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
if i find a way to blur the image ill post it. :)
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)
This question already has answers here:
pygame.event.get() not returning any events when inside a thread
(1 answer)
Is it possible to make the keyboard module work with pygame and threading
(1 answer)
Closed 1 year ago.
I was writing a small program in python 2.7.10 with pygame. Im using multithreading to play sounds (with winsound) and still draw things fast. I was trying to replicate a similar idea of some videos i saw on youtube of sorting algorithms. Unfortunately it runs for about 10 seconds then randomly crashes, the sounds keep going but the pygame window goes not responding and the drawing does not change. Even if i put a print "hi" in the code somewhere it will still be printing in the console but pygame will still have gone not responding. Cant think of whats wrong but it may be something with the multithreading (im very new to it!)
from random import randint
import pygame,winsound,threading,time
from pygame.locals import *
screen=pygame.display.set_mode([1000,500])
def bubbleSort(listName):
sorting=True
while sorting:
listChanged=False
for i in range(len(listName)-1):
draw(listName,1000/len(listName),500/11,i)
if listName[i] < listName[i+1]:
listName[i],listName[i+1]=listName[i+1],listName[i]
threading.Thread(target=playsound,args=([listName[i]])).start()
listChanged=True
if not listChanged:
sorting=False
time.sleep(10)
pygame.quit()
def playsound(frequency):
winsound.Beep(frequency*100,100)
def draw(listName,width,height,comparing):
print "hi"
screen.fill([0,0,0])
for i in range(len(listName)):
if i == comparing or i == comparing+1:
pygame.draw.rect(screen,[0,255,0],[width*(len(listName)-i),500-(height*listName[i]),width,500])
else:
pygame.draw.rect(screen,[255,255,255],[width*(len(listName)-i),500-(height*listName[i]),width,500])
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pygame.display.update()
list1=[]
for i in range(100):
list1.append(randint(1,10))
threading.Thread(target=bubbleSort,args=([list1])).start()