convert_alpha does not work. Can you help me please? [duplicate] - python

This question already has answers here:
How do I blit a PNG with some transparency onto a surface in Pygame?
(4 answers)
Pygame image transparency confusion
(1 answer)
Closed 8 months ago.
My code don't work, I tried to display a png image with convert_alpha in pygame 2.0 (python 3.10.1).
Here is my code:
import pygame
import sys
pygame.init()
background_colour = (255, 255, 255)
(width, height) = (1300, 700)
screen = pygame.display.set_mode((width, height))
bg = pygame.image.load("images/background.png")
rock = pygame.image.load("images/rock.png").convert_alpha()
pygame.display.set_caption('flying thing')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print("Key up has been pressed, go up")
if event.key == pygame.K_DOWN:
print("Key down has been pressed, go down")
if event.key == pygame.K_SPACE:
print("Key space has been pressed, game stopped")
screen.blit(bg, (0, 0))
screen.blit(rock, (0, 0))
pygame.display.update()
pygame.quit()
quit()
here is my image:
here is my output:

Related

pygame.key.get_pressed is not working correctly [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
How can I make a sprite move when key is held down
(6 answers)
Closed 2 days ago.
I'm trying to get a rectangle to move on screen, but the keyboard presses aren't working, and I can't solve it. Can someone help?
import pygame
WIN = pygame.display.set_mode((900, 500))
pygame.display.set_caption("coolioso")
FPS = 60
keys_pressed = pygame.key.get_pressed()
vel = 10
player = pygame.Rect(450, 250, 50, 50)
def draw_window():
WIN.fill((255, 255, 255))
pygame.draw.rect(WIN, (255, 0, 0), (player))
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if keys_pressed[pygame.K_w]:
player.y += vel
draw_window()
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
I tried using multiple different methods like functions, putting the key press statement elsewhere, but nothing worked and nothing changed.

I'm trying to move a sprite while animating it in pygame, the x value isn't changing though [duplicate]

This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
How to get keyboard input in pygame?
(11 answers)
Closed last month.
I'm trying to create a game in pygame where the character moves while animating, I tired using sprites but got the same results, however I did notice that pygame wasn't detecting that I pressed the right arrow key, I tried to get it to work but alas.
here's my code
:
import pygame
from sys import exit
def player_animation():
global player_current_sprite, player_frames, player_image
player_current_sprite += 0.01
player_image = player_frames[int(player_current_sprite)]
if player_current_sprite >= 1.3:
player_current_sprite = 0.7
pygame.display.set_caption('pausing has consequences')
pygame.init()
screen = pygame.display.set_mode((1000, 700))
clock = pygame.time.Clock()
player_frame_1 = pygame.image.load('graphics/player_frame_1.png')
player_frame_2 = pygame.image.load('graphics/player_frame_2.png')
player_current_sprite = 0
player_frames = [player_frame_1, player_frame_2]
player_image = player_frames[player_current_sprite]
player_rect = player_image.get_rect(center=(450, 250))
move = False
background_surface = pygame.image.load('graphics/background.png')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
move = True
if event.type == pygame.K_RIGHT and move == True:
player_rect.x += 10
if event.type == pygame.KEYUP:
move = False
screen.blit(background_surface, (0, 0))
player_animation()
screen.blit(player_image, player_rect)
pygame.display.flip()
clock.tick(60)
The x value does not change when I pressed the button, I did notice that pygame did not detect my button press

Image duplicates when player presses a key in Pygame [duplicate]

This question already has an answer here:
How can I move the ball instead of leaving a trail all over the screen in pygame?
(1 answer)
Closed 1 year ago.
I was trying to make a Pygame game, and when I went to the player movement, the player duplicates.
Here is the code:
#import libraries
import pygame
import sys
#variables
width = 850
height = 850
player_movement = 425
pygame.init()
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
pygame.display.set_caption("Prokect!")
player = pygame.transform.scale2x(pygame.image.load("player.png"))
#main loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player_movement -= 5
if event.key == pygame.K_DOWN:
player_movement += 5
screen.blit(player, (10, player_movement))
pygame.display.update()
clock.tick(60)
The question is, why does the player duplicate when the sprite (player) is moving?
Clear the screen with screen.fill(color) and then redraw all images (like the player with new position) that should be visible.
color can be a tuple or list with 3 or 4 elements or a pygame.Color object.
Clear the screen by filling the screen with a colour.
screen.fill(#Your chosen colour)

Python: Image is not moving [duplicate]

This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
How to get keyboard input in pygame?
(11 answers)
How to get if a key is pressed pygame [duplicate]
(1 answer)
Closed 2 years ago.
I want to move an image in python/pygame, but it doesn't work, python doesn't even notice that the key is pressed. I found solutions and compared my code to others, but it still doesn't work... I'm very new to coding (started only this week)
please correct my code
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
bright_red = (255,0,0)
bright_green = (0,255,0)
red = (200,0,0)
green = (0,200,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
smileImg = pygame.image.load('mouth.png')
clock = pygame.time.Clock()
def smile(x,y):
gameDisplay.blit(smileImg, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
go = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
go = -10
if event.type == pygame.KEYUP:
if event.key == pygame.K_BACKSPACE:
go = 0
y += go
gameDisplay.fill(white)
smile(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

Image flickering in PyGame? [duplicate]

So I run the code and it just starts glitching out. I am new to pygame.
Here is the code:
import pygame
pygame.init()
# Screen (Pixels by Pixels (X and Y (X = right and left Y = up and down)))
screen = pygame.display.set_mode((1000, 1000))
running = True
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
# Player Icon/Image
playerimg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
def player(x, y):
# Blit means Draw
screen.blit(playerimg, (x, y))
# Game loop (Put all code for pygame in this loop)
while running:
screen.fill((225, 0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether is right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("Left arrow is pressed")
if event.key == pygame.K_RIGHT:
print("Right key has been pressed")
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("kEYSTROKE RELEASED")
# RGB (screen.fill) = red green blue
player(playerX, playerY)
pygame.display.update()
The image is not the glitching one as I was not able to post a video but it is what my code does
The problem is caused by multiple calls to pygame.display.update(). An update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.
Remove all calls to pygame.display.update() from your code, but call it once at the end of the application loop:
while running:
screen.fill((225, 0, 0))
# pygame.display.update() <---- DELETE
# [...]
player(playerX, playerY)
pygame.display.update()
If you update the display after screen.fill(), the display will be shown filled with the background color for a short moment. Then the player is drawn (blit) and the display is shown with the player on top of the background.

Categories

Resources