Hey I want to learn how to move your image to a position.
I searched on Google and Youtube but I got a little bit information.
This is my code
import pygame as pg
pg.init()
# Create screen
screen = pg.display.set_mode((1100, 700))
# Background
background = pg.image.load('map.png')
# Player
playerImg = pg.image.load('player.png')
playerX = 80
playerY = 595
def player(x, y):
screen.blit(playerImg, (x, y))
# Game Loop
running = True
while running:
# Background image
screen.blit(background, (0, 0))
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
# If keystroke is pressed check whether its right or left
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
screen.blit(playerImg, [80, 598])
if event.key == pg.K_w:
screen.blit(playerImg, [370, 598])
if event.key == pg.K_e:
screen.blit(playerImg, [665, 598])
if event.key == pg.K_r:
screen.blit(playerImg, [950, 598])
if playerX <= 80:
playerX = 80
elif playerX >= 950:
playerX = 950
player(playerX, playerY)
pg.display.update()
The problem what I have now
When I press one of the keys it shows the image but for like 1 second.
What I want
When I press the key it show the image and when I press the second key
I want that the image go to the new position.
If you don't want to show the image a the beginning, then init the player coordinates with an off screen position.
If a key is pressed, then you have to change the position (playerX, playerY):
playerX = -100
playerY = -100
running = True
while running:
# Background image
screen.blit(background, (0, 0))
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
# If keystroke is pressed check whether its right or left
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
playerX, playerY = 80, 598
if event.key == pg.K_w:
playerX, playerY = 370, 598
if event.key == pg.K_e:
playerX, playerY = 665, 598
if event.key == pg.K_r:
playerX, playerY = 950, 598
player(playerX, playerY)
pg.display.update()
screen.blit(playerImg, ...) would just draw a 2nd player at a different position, for 1 single frame. That is hardly noticeable and may just cause a short flicker of a 2nd player image.
Note, the KEYDOWN event occurs once only, when the button is pressed. In that case your code blit a player, but it gets "cleared" immediately in the next frame by screen.blit(background, (0, 0)).
Related
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.
I have been trying out the Pygame game library on my Mac, and when I got to try moving around an image around the game window, for some reason, it doesn't get erased.
The code I tried to run:
import pygame
pygame.init()
# Creates the screen.
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("ufo.png")
pygame.display.set_icon(icon)
# Player sprite.
playerImg = pygame.image.load("player.png")
playerX = 350
playerY = 500
playerX_change = 0
# Function to draw the player
def player(x, y):
screen.blit(playerImg, (playerX, playerY))
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
print("Left key is pressed!")
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
print("Right key is pressed!")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
I have tried to run similar code on my PC, and it worked like intended. I have no idea what to do.
You have to clear the screen in every frame:
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# update objects
speed = 1
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
# clear screen
screen.fill(0)
# draw scene
player(playerX, playerY)
# update display
pygame.dispaly.flip()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
I try to get my Pygame character on screen but I cannot get it to appear so I can control it for a game I am building
Here is the code:
import pygame
#turn on pygame
pygame.init()
#window, window name and icon
screen = pygame.display.set_mode((1920,1080))
pygame.display.set_caption("When The Sky Turns Grey")
icon = pygame.image.load('cat.png')
pygame.display.set_icon(icon)
#Player
playerIMG = pygame.image.load('cat.png')
playerX = 960
playerY = 1080
playerXchange = 0
def player():
screen.blit(playerIMG, (playerX, playerY))
#Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#KEYS
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
playerX_change = -5
if event.key == pygame.K_d:
playerX_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
playerXchange = 0
playerX += playerXchange
#RGB
screen.fill((0, 0, 255))
#draw player (call player function)
player()
pygame.display.update()
I cannot get it to show up at all, no matter what I change.
I am new to pygame and if anyone can help I will be grateful
Thank you!
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
There is 2 problems in this question
First your cat Y position is to much that it went under the screen, it should be like this:
playerY = 540 # Don't go over 1080 which is the screen height
Second you didn't update the display so it doesn't show anything on the screen, at the very end of the while loop put pygame.display.update after player()
I am currently attempting to programm a small game. An enemy is supposed to chase the player, the actual chasing is not implemented yet. I still need to figure out how to do that, but that's not the question.
I have made it so that the player returns to the start point once they collide with the enemy. In addition to that, a text 'Game over' is supposed to appear. The function for that is called at the end of the game loop and while the text appeared briefly(it actually only appeared once, I have tried it multiple times), it does not stay. I was planing on making it appear and then disappear after a few seconds so that the player can play again, but I'm not sure why it disappears instantly.
If this is the wrong place to post this, please tell me, I will delete this post. This is my code, would be amazing if somebody could help me out:
import pygame #imports pygame
import math
pygame.init()
#screen
screen = pygame.display.set_mode((800,600)) #width and height
#title and icon
pygame.display.set_caption("The Great Chase") #changes title
icon = pygame.image.load('game-controller.png')
pygame.display.set_icon(icon) #setting icon
#player
playerImg = pygame.image.load('scary-monster.png')
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
#enemy
enemyImg = pygame.image.load('caterpillar.png')
enemyX = 370
enemyY = 50
enemyX_change = 0
enemyY_change = 0
#GAME OVER
game_over_font = pygame.font.Font('Bubblegum.ttf',64)
def game_over_text():
game_over_text = game_over_font.render("GAME OVER", True, (255, 0, 0))
screen.blit(game_over_text, (200, 250))
def player(x, y): #function for player
screen.blit(playerImg, (x, y)) #draws image of player, blit -> means to draw
def enemy(x, y): #function for enemy
screen.blit(enemyImg,(x,y))
def isCollision(playerX, playerY, enemyX, enemyY):
distance = math.sqrt((math.pow(playerX-enemyX,2)) + (math.pow(playerY - enemyY,2)))
if distance < 25:
return True
else:
return False
def chase():
distance = math.sqrt((math.pow(playerX-enemyX,2)) + (math.pow(playerY - enemyY,2)))
#Game loop
running = True
while running:
screen.fill((255,255,0)) #0-255 RGB-> red, green & blue
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke check whether right or left or up or down
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.2
if event.key == pygame.K_RIGHT:
playerX_change = 0.2
if event.key == pygame.K_UP:
playerY_change = -0.2
if event.key == pygame.K_DOWN:
playerY_change = 0.2
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
playerY += playerY_change
#creating borders so that player won't leave screen
if playerX <=0:
playerX = 0
elif playerX >=768:
playerX = 768
if playerY <= 0:
playerY = 0
elif playerY >=568:
playerY = 568
#collision
collision = isCollision(playerX, playerY, enemyX, enemyY)
if collision:
playerY = 480
game_over_text()
player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()
The collision only occurs for a moment and the game over text is only shown when the object collides. If you want to persist the text, set a gameover variable when the collision is detected and display the text based on the state of the variable:
gameover = False
running = True
while running:
# [...]
collision = isCollision(playerX, playerY, enemyX, enemyY)
if collision:
playerY = 480
gameover = True
player(playerX, playerY)
enemy(enemyX, enemyY)
if gameover:
game_over_text()
pygame.display.update()
Note, collision is set in every frame depending on the position of the objects. However, gameover is set once when a collision occurs and then maintains its state.
This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
Closed 1 year ago.
Ok so im new to PyGame, and im making a space invader game, im currently building the movement script.
for some reason my character is infinitely moving to the right when i press the right key.. i know this must be a really simple solution i am just not seeing it lol i would appreciate an answer.
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Space Invader')
icon = pygame.image.load('pepper.png')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('sombreroship.png')
playerX = 370
playerY = 480
playerX_Change = 0
def player (x, y): screen.blit(playerImg, (x, y) )
running = True
while running:
# RGB STANDS FOR RED, GREEN, BLUE THE NUMBERS GOES MAX TO 255 FOR EXAMPLE_:
screen.fill((0, 0, 30)) # this will display yellow
for event in pygame.event.get():
if event.type == pygame.QUIT: #if we click the X the program ends
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_Change = -0.1
if event.key == pygame.K_RIGHT:
playerX_Change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_Change = 0.1
playerX += playerX_Change
player(playerX, playerY)
pygame.display.update()
I suggest to use pygame.key.get_pressed() instead of the key board events.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Space Invader')
icon = pygame.image.load('pepper.png')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('sombreroship.png')
playerX = 370
playerY = 480
def player (x, y):
screen.blit(playerImg, (x, y) )
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: #if we click the X the program ends
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] :
playerX += 0.1
if keys[pygame.K_LEFT] :
playerX -= 0.1
# RGB STANDS FOR RED, GREEN, BLUE THE NUMBERS GOES MAX TO 255 FOR EXAMPLE_:
screen.fill((0, 0, 30)) # this will display yellow
player(playerX, playerY)
pygame.display.update()