Reputation: 1059
I am trying to learn to make a basic game using pygame. I want to import and display an image in .png format. so far my attempt has been:
import pygame
from pygame.locals import*
pygame.image.load('clouds.png')
white = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1
while running:
screen.fill((white))
pygame.display.flip()
The Image (clouds.png) is in the same folder as the file. when i try to run this i get an error:
Traceback (most recent call last):
File "C:\Users\Enrique\Dropbox\gamez.py", line 3, in <module>
pygame.image.load('clouds.png')
error: Couldn't open clouds.png
Upvotes: 7
Views: 60908
Reputation: 11
here you go, this is how I would do it.
import pygame as py # import pygame
py.init() # initialise it
img = py.image.load("clouds.png") # load the image
sc = py.display.set_mode((800, 600)) # create the screen
g = (0, 255, 0)
b = (0, 0, 0)
x = 350
y = 300
def render(x1, y1):
sc.blit(img, (x1, y1))
running = True
while running:
sc.fill(g) # fill the screen
render(x, y) # BLIT IT ON
key = py.key.get_pressed() # for keys
# moving scripts start now
for event in py.event.get():
if event.type == py.KEYDOWN:
if event.key==py.K_UP:
y = y - 25
if event.type == py.KEYDOWN:
if event.key==py.K_DOWN:
y = y + 25
if event.type == py.KEYDOWN:
if event.key==py.K_RIGHT:
x = x + 25
if event.type == py.KEYDOWN:
if event.key==py.K_LEFT:
x = x - 25
if event.type == py.KEYDOWN:
if event.key==py.K_l:
print(py.event.get())
mousex, mousey = py.mouse.get_pos()
# mouse thing
if py.mouse.get_pressed()[0]:
x = mousex
y = mousey
py.display.flip()
I hope this helps!
Upvotes: 1
Reputation: 1
import pygame as pg
pg.init( )
screen = display.set_mode( ( 460 , 640 ) )
def picture( images , x , y , width , height , angle , horizontal_flip , vertical_flip ):
images1 = image.load( images ) # loading image
images2 = transform.scale( images1 , ( width , height ) ) # changing size
images3 = transform.rotate( images2 , angle ) # changing angle 0 - 360 degree
images4 = transform.flip( images3 , horizontal_flip , vertical_flip ) # horizontal or vertical flip - True or False
images4rect = images4.get_rect( )
images4rect.x = x # setting x
images4rect.y = y # setting y
screen.blit( images4 , images4rect ) # image blit
while True:
screen.fill( ( 0 , 0 , 0 ) )
images = "image.png" # image name
x = 100 # x-axis
y = 100 # y-axis
angle = 0 # 0 - 360
horizontal_flip = False # True
vertical_flip = False # True
width = 200 # width of image
height = 150 # height of image
picture( images , x , y , width , height , angle , horizontal_flip , vertical_flip )
pg.display.flip( )
Upvotes: 0
Reputation: 299
Here is an image handling block that I use in my games:
import os, sys
...
-snip-
...
def load_image(name, colorkey=None):
fullname = os.path.join('images', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
You can copy-paste this in any game and it will work. os
and sys
need to be imported into your game or else it won't work.
Upvotes: 2
Reputation: 1779
Here you go. It blits the image to 0,0. Your other problem is that your pyimage doesn't seem to be built with png support
import pygame
from pygame.locals import*
img = pygame.image.load('clouds.bmp')
white = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1
while running:
screen.fill((white))
screen.blit(img,(0,0))
pygame.display.flip()
Upvotes: 8