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

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

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.

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)

line 26, in <module> pressed = pygame.key.get_pressed() pygame.error: video system not initialized [duplicate]

This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
When i run my projekt i get this message:
line 26, in
pressed = pygame.key.get_pressed()
pygame.error: video system not initialized
I tried everything I could find in the internet. I downloaded the newest python and pip version. This is my code:
import pygame
import sys
from pygame.constants import K_DOWN, K_LEFT, K_RIGHT, K_UP
pygame.init()
screen = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()
BLACK = (0,0,0)
MAGENTA = (255,0,255)
x = 300
y = 300
width = 40
height = 40
go = True
while go:
for event in pygame.event.get():
if event.type == pygame.quit():
go = False
pressed = pygame.key.get_pressed()
if pressed[K_UP]:
y -= 3
if pressed[K_DOWN]:
y += 3
if pressed[K_RIGHT]:
x += 3
if pressed[K_LEFT]:
x -= 3
screen.fill(BLACK)
pygame.draw.rect(screen, MAGENTA, (x,y,width,height))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
Please help me :)
In your current code, the first time you test if event.type == pygame.quit():, you actually execute pygame.quit()
Your test should be if event.type == pygame.QUIT.

Pygame used in VS Code with Pygame Snippets [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN
(1 answer)
Closed 2 years ago.
I have installed vs code and added pygame snippets to use pygame library. My big problem is, every time I try to use any key option of pygame, like pygame.KEYDOWN or pygame.QUIT it tells me that QUIT is not a function of pygame. Can someone help me?
Everything else seems to work, like display or surface
even pygame.key.get_pressed() don’t make problems.
import pygame, random, sys
from pygame.locals import *
from pygame.key import *
def set_Background():
screen = pygame.display.set_mode((500,500))
surface = pygame.image.load('Background.png')
surface = pygame.transform.scale(surface, (500, 500))
screen.blit(surface, (0,0))
pygame.display.update()
return screen
def set_Enemy():
enemy = pygame.image.load('Enemy.png')
enemy = pygame.transform.scale(enemy, (50, 50))
return enemy
def set_Player():
player = pygame.image.load('Player.png')
player = pygame.transform.scale(player, (70, 70))
return player
RUNNING = True
while RUNNING:
background = set_Background()
enemy = set_Enemy()
player = set_Player()
enemy_rect = enemy.get_rect()
player_rect = player.get_rect()
e_x = random.randint(10,450)
e_y = random.randint(10,450)
background.blit(enemy, (e_x, e_y))
pygame.display.update()
for event in pygame.event.get():
key = pygame.key.get_pressed()
if event.type == key[pygame.K_ESCAPE]:
#module pygame has no K_ESCAPE member
sys.exit()
if event.type == pygame.QUIT:
#says module pygame has no QUIT member
sys.exit()
pygame.key.get_pressed() shouldn't be in the event loop, but in the main while loop. In the event loop you need to check if the event type is pygame.QUIT and then set the running flag to False.
Here's a fixed version:
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
running = True # Uppercase names are for constants not variables.
while running:
# The event loop.
for event in pygame.event.get():
# If a pygame.QUIT event is in the queue.
if event.type == pygame.QUIT:
running = False
# To check if it was a `KEYDOWN` event.
elif event.type == pygame.KEYDOWN:
# If the escape key was pressed.
if event.key == pygame.K_ESCAPE:
running = False
# Use pygame.key.get_pressed to see if a key is held down.
# This should not be in the event loop.
key = pygame.key.get_pressed()
if key[pygame.K_UP]:
print('up arrow pressed')
screen.fill((30, 30, 30))
pygame.display.flip()
clock.tick(60)
Add from pygame.locals import * at the top of your code.
You are mixing two types of key presses in one go. You should instead either
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SOMEKEY:
do_something()
or
keys = pygame.key.get_pressed()
if keys[pygame.K_somekey]:
do_something()
so the code above with the pygame.key.get_pressed() should not be in the event loop

clearing the screen with a key press in pygame [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Pygame level/menu states
(2 answers)
Closed 2 years ago.
this code is supposed to go to a different function which clears the screen when you hit z, and it does do that, but whenever I let go of z it switches back to the title function and redraws everything.
I'm trying to use it as a "hit this key to go to a new window" type thing and the screenfill is just a fill-in for now, so you can see why that's a problem. for the record I'm using python 3.6.
I've also already tried putting global start in the top of the title function, but then the z key does nothing. please and thanks!
import pygame
from pygame.locals import *
import random
pygame.init()
LOGO = pygame.image.load("kelogo.png")
savannah = pygame.image.load("savannah.png")
hitkey = pygame.image.load("hitkey.png")
display_width = 800
display_height = 600
white = (255, 255, 255)
game_display = pygame.display.set_mode((display_width, display_height))
start = False
def main_game():
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.fill(white)
pygame.display.update()
def title():
intro = False
while not intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.blit(savannah, ((0),(0)))
game_display.blit(LOGO, ((0),(100)))
game_display.blit(hitkey, ((130),(100)))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
start = True
main_game()
pygame.display.update()
title()
main_game()
pygame.quit()
quit()

Categories

Resources