Well, this is my first program I write with Pygame. Before opening this thread I read two other thread without any positive result. I almost copied a program to resolve this problem without success. Here is my code:
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
car = pygame.image.load('car.png')
def car(x,y):
gameDisplay.blit(car,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(blue)
car(x,y)
pygame.display.update()
clock.tick(24)
pygame.quit()
quit()
I thought there could be a problem with directories, but I don't think so. I create a folder in the desktop named "Nuova Cartella" (New Folder in italian, and I wrote it in italic ahah) and I put there two files, the first one is the program that I've just post here and the second one is "car.png", that's the image I'd like to load in pygame, of course.
Sorry for my english, I did my best.
A few errors in your code. the most important one was noticed by #PRMoureu. Your definition is called car, and the variable for your image is also called car. Since you created the image variable first, then created a function with the same name you erased the image, and replaced it with the function. So simply change 'car' to something like 'carImage' or anything different than the function name. Also you fill the screen with the color blue, which if you look back you actually haven't defined. after debugging these errors. Here is the code:
import pygame
from pygame.locals import *
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
carImage = pygame.image.load('car.png')
def car(x,y):
gameDisplay.blit(carImage,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(24)
pygame.quit()
quit()
But Dont try it just yet!
From the title it appears that your error is 'couldnt open car.png'
however the issues in your code that i described should give a different error. I find that pretty wierd, and the only explanation to that(considering the fact that the directory is fine and python has permission to access the image) is that your file extension is NOT a png file, but maybe a jpg or something else.
If that's true, then copy the code i have above but change '.png' in
carImage = pygame.image.load('car.png')
to the correct extension of your image. That should do it.
If none of this works, then i'm afraid this is all the help i can really think of. Maybe more details may help.
I hope this proves helpful anyway. It did work with me, so... Good Luck!
Related
import pygame
import time
# WINDOW SETUP
window = pygame.display.set_mode((900, 500))
pygame.display.set_caption("Pong")
time.sleep(5)
FPS = 60
# RGB VALUE VARIABLES
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
def background(window):
window.fill(WHITE)
pygame.display.update()
# FRAMERATE AND EVENT LOOP INITIALIZATION
def main():
run = True
clock = pygame.time.Clock()
while run:
clock.tick(FPS)
background(window)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
Above, is my code. I'm trying to make a pong game with pygame. The text editor I am coding with is Visual Studios Code (VSCODE).
Firstly, you need to call the main. You should also make you're code nice and easy to read when possible. I imported pygame as pg which makes typing pygame functions a bit faster as you have less to type. Also, it's better to use global variables to hold variables that won't change through the program, like screen width, height, colours, etc. Then make sure you initialise the module.
As well as that, the only update you have is in background(). You should put the update at the bottom of the loop and remove it from background(). This way everything above will update each loop.
I apologise for not adding you're FPS counter in here as well but I think this should be enough to help you get you're window running with more readable code and a more efficient loop.
import pygame as pg
# Global Variables
screen_width = 900
screen_height = 500
screen = pg.display
window = screen.set_mode((screen_width, screen_height))
colour = 'red'
def main():
# Initialise module
pg.init()
pg.display.set_caption('PONG')
running = True
while running:
# This is a better way of writing your loop
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
# Call background function
background()
# Updates window
# place this inside the loop near the bottom
# so everything is updated at the end of each loop
screen.flip()
def background():
window.fill(colour)
# Remember to call your main function
# This if statement is good practise but not required
# You can just place main() here
if __name__ == '__main__':
main()
I can't get pytmx to render transparent tiles from Tiled correctly in pygame.
On this example you can see that the tile rendered from the tmx file is showing a black background, i would like to have it like the image rendered directly from the image file.
I have tried messing with .convert(), .convert_alpha() or even putting the flag pygame.SCRALPHA but no luck there.
Here is a link to get the assets for reproducing the example : https://filebin.net/yvmr5jz04j889mlx
Here is the code of the example :
import pygame
import pytmx
pygame.init()
gameScreen = pygame.display.set_mode((280, 210))
clock = pygame.time.Clock()
# filling in white to see the lack of alpha
gameScreen.fill((255, 255, 255))
# bliting from tmx file, (the alpha is not recognized)
gameMap = pytmx.load_pygame('test_map.tmx')
for layer in gameMap.visible_layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = gameMap.get_tile_image_by_gid(gid)
if tile:
gameScreen.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
# bliting the image directly from pygame (the alpha is correctly recognized)
rock = pygame.image.load('rock.png')
gameScreen.blit(rock, (140, 70))
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()
Found the solution !
Turns out that when I created the Tilesets in Tiled I checked the box "use transparency color".
I had a hard time figuring this out because when I looked at the tileset properties in Tiled after the tileset was created, it showed that the transparency color was not set, and the transparency was taken into account in Tiled.
i want to use python to create an animation of an enlarged raidus of a circle.
i use two ways to accomplish this, one way is success, the another is failed.
i want to know why, is there anyone can help.
version 1:
import pygame
global DISPLAYSURF, FPSCLOCK
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWNWIDTH, WINDOWNHEIGHT))
radius = 1
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
radius = radius + 1
pygame.draw.circle(DISPLAYSURF, RED, (100, 100), radius)
pygame.display.update()
FPSCLOCK.tick(1)
version2:
import pygame
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWNWIDTH, WINDOWNHEIGHT))
for radius in range(0, 100, 2):
pygame.draw.circle(DISPLAYSURF, RED, (100, 100), radius)
pygame.display.update()
FPSCLOCK.tick(1)
while True:
just_wait = True
the version is what i want , the version2 can not work
i run the code on macOs 10.13.1 High Sierra with python3 , why!!!
I think you put code with wrong indentions and it should be
for radius in range(0, 100, 2):
pygame.draw.circle(DISPLAYSURF, RED, (100, 100), radius)
pygame.display.update()
FPSCLOCK.tick(1)
while True:
just_wait = True
And it means you run while True loop inside for loop.
But while True loop is endless loop - you never leave it.
So it never go back to draw new circle and update screen.
You can put print() in code and you will see it.
First version is preferred version because it not only draw animation but it checks events so you can leave program in any time.
On some systems if you will not use pygame.event.get(): to get events from system then system will think that program hung up and system can kill program.
I just wrote some code that works in the commandline, so now I'd like to give it some graphics. Now, this is my very first programming project so bear with me as I try to explain the problem:
I'm using PyGame and initialised the window as follows:
import pygame, pygame.midi,pygame.font, random
(width, height) = (600, 400)
background = (220,220,220)
pygame.midi.init()
pygame.font.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Chord trainer")
screen.fill(background)
pygame.display.flip()
Then I attempt to render text (which does not give any errors):
myfont = pygame.font.SysFont("Arial", 80)
letter = myfont.render("SOME WEIRD TEST TO TRY AND GET THINGS WORKING",0,(0,0,0))
screen.blit(letter,(100,100))
And because I'd like to actually see my text before the program closes, I set up an infinite loop:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
When I run it, I just get the grey screen I wanted, but without any sort of text, which would make me think that there's something wrong with my blit command, but I can't figure out what.
Initially I had the rendering set in a loop, but that just made the program unresponsive so I took is out for debugging. For completeness, here is that loop:
while True:
# Decide on random chord
c1 = random.choice(chords)
# Make sure that no repitition takes place.
if c1==c2:
while c1==c2:
c1=random.choice(chords)
c2 = c1
myfont = pygame.font.SysFont("Arial", 80)
letter = myfont.render(str(c1),0,(0,0,0))
screen.blit(letter,(100,100))
# Listen to Midi device and search for c.
midi_listen(inp,sorted(c1))
score += 1
You need to add
pygame.display.flip()
after
letter = myfont.render("SOME WEIRD TEST TO TRY AND GET THINGS WORKING",0,(0,0,0))
screen.blit(letter,(100,100)
It will update your screen and normally you will be able to see your text.
I recently started programming on pygame, I use python 3.5.1 and the latest pygame and when I load a bitmap(bmp) it puts it on the screen like this
At school when I program on the computer it works fine just on my macbook pro 13'' with el capitain
import pygame #imports pygame
width = 550 #width is 550
height = 420 #height is 420
size = width, height # size is a tuple
screen = pygame.display.set_mode(size)
white = 255,225,255
screen.fill(white)
gameOn = True
mario = pygame.image.load("mario.bmp") #load the bitmap
marioflip = pygame.transform.flip(mario, True, True)
screen.blit(marioflip, (0,0)) # add it to screen
marioscale = pygame.transform.scale(mario, (50,50))
screen.blit(marioscale, (250,250))
mariorotate = pygame.transform.rotate(mario, 45)
screen.blit(mariorotate,(350,0))
while gameOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
You are seeing a known bug in SDL_image on that version of OS X. See this link for details and a workaround:
https://bitbucket.org/pygame/pygame/issues/284/max-osx-el-capitan-using-the-deprecated