Pygame - Background doesnt work - python

im having a hard time making my first steps in pygame. I just wanna move a Picture around in front of a jpg-background.
I googled several times for a solution and tried different things .. and this is the "nearest" i can come up with:
(but it still doenst work ..)
Any ideas?
Thanks in advance!
Nico
import pygame
pygame.init()
display_width = 1600
display_height = 900
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
Player_width = 73
screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
background = pygame.surface(screen.get_size())
PlayerImg = pygame.image.load("C:\python\warrior1.png")
background_image = pygame.image.load("depositphotos_159346790-stock-photo-
forest-and-stones-2d-game.jpg")
background.blit(background_image)
screen.blit(background (0,0))
def Player(x,y):
gameDisplay.blit(PlayerImg,(x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
y_change = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
[...]
x += x_change
y += y_change
Player(x,y)
if x > display_width - Player_width or x < 0:
gameExit = True
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()

Answer:
delete
background = pygame.surface(screen.get_size())
in player replace
gameDisplay
with
screen
replace
background.blit(background_image)
screen.blit(background (0,0))
with
screen.blit(background_image, (0,0))
add screen.blit(background_image, (0,0))
also in the while Loop
Helpful Tips for Pygame:
Look at error messages and post them too.
Use *.png files in pygame. They can be transparent. You see no edges when the pictures overlap.
Watch Sentdex's Pygame Tutorial again.

Related

player img wont load onto the window in pygame. Theres no error whatsoever though I just cant see the img on screen [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
I was wondering why my playerIMG wont load in pygame
(2 answers)
How can I make a sprite move when key is held down
(6 answers)
Closed 7 months ago.
Here is my code below this is my first pygame project any help will be appreciated! I think its something to do with the
def player(playerX,playerY):
pygame.display.update()
block of code. Though when I play around with it sometimes it won't even display my background object and only displays the screen filler black.
#initializing the game
pygame.init()
FPS = 60
black = (0,0,0)
white = (255,255,255)
#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))
#setting the title of the window
pygame.display.set_caption('Space Crashers')
#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')
#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480
#creating player function
def player(x,y):
gameDisplay.blit(playerImg, (x,y))
# window creation
clock = pygame.time.Clock()
running = True
while running:
#setting the background to black
gameDisplay.fill(black)
#then changing the background to the image we loaded
gameDisplay.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
# if the keystroke is pressed, the player will move
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX -= .01
if event.key == pygame.K_RIGHT:
playerX += .01
if event.key == pygame.K_UP:
playerY -= .01
if event.key == pygame.K_DOWN:
playerY += .01
def player(playerX,playerY):
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
In your game loop you should normally
fill your screen black
draw and blit everything to the screen
update the screen
catch the events
let the clock tick
You didn't call the player() function in your loop to draw the player image neither the clock.tick() function. You don't need to overwrite the player function. Also if ran without any arguments, you should only have one event loop, otherwise some events could be ignored.
pygame.init()
FPS = 60
black = (0, 0, 0)
white = (255, 255, 255)
# window
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Space Crashers')
# background image
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')
# player image
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
# these coords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480
# draw player
def player(x, y):
gameDisplay.blit(playerImg, (x, y))
# window creation
clock = pygame.time.Clock()
running = True
while running:
# reset display
gameDisplay.fill(black)
# blit background
gameDisplay.blit(background, (0, 0))
### blit the player ###
player(playerX, playerY)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# move player
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX -= .01
if event.key == pygame.K_RIGHT:
playerX += .01
if event.key == pygame.K_UP:
playerY -= .01
if event.key == pygame.K_DOWN:
playerY += .01
pygame.display.update()
clock.tick(FPS)
pygame.quit()

How to attach an image to a rect in pygame?

I need help attaching an image to a rectangle in pygame.. I am trying to make a snake game in python(you know, the one that eats the apples and grows lol) and I wanna attach my teacher's face to head of the snake.
I've already tried defining a variable to import the image and then renaming the rectangle to be that image, but nothing seems to work.
snakeFace = pygame.image.load("Morrison.jpg").convert_alpha()
rect = snakeFace.get_rect()
screenWidth = 500
X=50
y= 50
height = 20
vel = 20
run = True
lastKey = None
while run:
pygame.time.delay(10) #1/2 milisecond delay
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False #this controls the "X" button
if event.type == pygame.KEYDOWN:
lastKey = event.key
keys = pygame.key.get_pressed()
if lastKey == pygame.K_LEFT and x > vel:
x-=vel
if lastKey == pygame.K_RIGHT and x< screenWidth - width -vel:
x+=vel
if lastKey == pygame.K_DOWN and y < screenWidth - height - vel:
y+= vel
if lastKey == pygame.K_UP and y > vel:
y-=vel
win.fill((0,0,0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
I expected there to be a square with my teachers face on it that runs around on the screen after a particular key is pressed on the screen but its just the regular old red square.
It's a red square because the code is drawing a red square:
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
Paint the snakeFace bitmap instead:
win.blit(snakeFace, (x, y))
Here is the full example code that I use to add images to rect objects.
import pygame
vel = 5
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('captions')
icon = pygame.image.load('test123.jpg')
pygame.display.set_icon(icon)
playerimg = pygame.image.load('test123.jpg')
playerx = 370
playery = 480
def player():
screen.blit(playerimg,(playerx,playery))
running = True
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and playerx > vel:
playerx += vel
player()
pygame.display.update()

Pygame screen display issue

import pygame
pygame.init()
display_width = (640)
display_height = (480)
title = pygame.display.set_caption("test")
IMG = pygame.image.load("image.png")
screen = pygame.display.set_mode((display_width,display_height))
screen.blit(IMG,(1,1))
pygame.display.update()
Whenever I use pygame, even simple displays like this are skewed for me. it shows 0,0 at around the middle of my display screen and i dont know why. Basically, it is showing - x values on the x axis help!
I am using python 2.7 and this seems to not be a coding issue, but rather something else. please help! ty
I've not been able to replicate your problem with the above code in Python 2.7.12, the image is a red, fifty-pixel square:
Here's an extended demo that will draw the image around the cursor position based on the mouse button clicked. Perhaps that'll help you get towards the behaviour you're after.
import pygame
if __name__ == "__main__":
pygame.init()
screen_width, screen_height = 640, 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Blit Demo')
clock = pygame.time.Clock() #for limiting FPS
FPS = 10
exit_demo = False
# start with a white background
screen.fill(pygame.Color("white"))
img = pygame.image.load("image.png")
width, height = img.get_size()
pos = (1,1) # initial position to draw the image
# main loop
while not exit_demo:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_demo = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
# fill the screen with white, erasing everything
screen.fill(pygame.Color("white"))
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # left
pos = (event.pos[0] - width, event.pos[1] - height)
elif event.button == 2: # middle
pos = (event.pos[0] - width // 2, event.pos[1] - height // 2)
elif event.button == 3: # right
pos = event.pos
# draw the image here
screen.blit(img, pos)
# update screen
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()

python-pygame rectangle won't draw/render

im relatively new to coding and after understanding some basics in python i'm trying to apply oop in pygame
I have this code and I can't figure out why the rectangle won't appear
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("EXAMPLE")
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
FPS = 30
class Puddles(pygame.sprite.Sprite):
puddle_width = 100
puddle_height = 20
def __init__(self,color,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.color = color
self.image = pygame.Surface((Puddles.puddle_width,Puddles.puddle_height))
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.rect.x = self.x # i think problem's here because if I type specific integers for x and y the tile will appear
self.rect.y = self.y
all_sprites = pygame.sprite.Group()
puddle1 = Puddles(red, 400, 600)
all_sprites.add(puddle1)
gameExit = False
while not gameExit:
clock.tick(FPS)
for event in pygame.event.get():
print event
if event.type == pygame.QUIT:
gameExit = True
all_sprites.update()
screen.fill(white)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
quit()
any ideas?
thanks in advance:)
puddle1 = Puddles(red, 400, 600)
The y position of the puddle is below the screen height, so it's outside of the screen. ;) Try to change it for example to puddle1 = Puddles(red, 400, 200).
Also, lines 43-46 should be indented, so that they're in the while loop.

Add walk animation. I just want to have the sprite image change when i hold down the key, like shuffle between 3 different walking images.

This is my first post and i have no idea what i am doing hah, but i started learning pygame tonight
and i want to know to add a walk animation. I just want to variable "walk" to change images every .5 or so seconds of holding the
key down
import pygame
import time
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
#imports pygame and initializes the module
pygameDisplay = pygame.display.set_mode((800,600))
#creates a screen
pygame.display.set_caption("Snake(;")
gameExit = False
walk = pygame.image.load("pokemon walk sprite sheet.png")
walkx = 350
walky = 330
walkx_change = 0
clock = pygame.time.Clock()
while not gameExit:
pygameDisplay.blit(walk,(walkx,walky))
pygame.display.update()
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
walkx_change = -10
walk = pygame.image.load("walk sheet left.png")
#right here i want code to make it switch to another image after .5 or so seconds
if event.key == pygame.K_RIGHT:
walkx_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
walkx_change = 0
walkx += walkx_change
pygameDisplay.fill(white)
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
You can't do animation like that. You have to create a class with Sprite module first. And you have to import every image(frame) of your animation.
class Asker(Sprite):
def __init__(self, konum):
Sprite.__init__(self)
self.resimler = [pygame.image.load("01.png"), pygame.image.load("02.png"),pygame.image.load("03.png"),pygame.image.load("04.png"),pygame.image.load("05.png"),
pygame.image.load("06.png"),pygame.image.load("07.png"),pygame.image.load("08.png"),pygame.image.load("09.png"),pygame.image.load("10.png"),
pygame.image.load("11.png"),pygame.image.load("12.png"),pygame.image.load("13.png"),pygame.image.load("14.png"),pygame.image.load("15.png"),
pygame.image.load("16.png")]
self.image = self.resimler[0]
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = konum
self.say = 0
It's an example from my script, see I load 16 pictures, you have to split them first with gimp or photoshop.That pictures I splitted them from an animation. Then you have to rotate them etc etc. You should check some tutorials of Sprite, before it, learn basic Pygame.

Categories