This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
Closed 1 year ago.
im a beginner in pygame, and i dont have any experience in programming,
im using pygame.key.get_pressed() :
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 400))
pygame.display.set_caption('Hello World!')
green = (0,255,0)
FPS = 60
FpsClock = pygame.time.Clock()
playerY = 200
playerX = 200
while True:
BG = pygame.draw.rect(DISPLAYSURF,(255, 255,255),(0,0,400,400))
player = pygame.draw.rect(DISPLAYSURF,(green),(playerX, playerY,20, 20))
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if keys[pygame.K_z]:
playerY -= 5
if keys[pygame.K_s]:
playerY += 5
if keys[pygame.K_d]:
playerX += 5
if keys[pygame.K_q]:
playerX -= 5
pygame.display.update(player)
pygame.display.update()
FpsClock.tick(FPS)
when i click one time on s key for example everything works just fine the player moves, but
if i hold down s button there is a delay before the player move's down while im holding the button
(samething for w,a,d)
It is a matter of Indentation. You must evaluate the pressed keys in the application loop instead of the event loop:
while True:
BG = pygame.draw.rect(DISPLAYSURF,(255, 255,255),(0,0,400,400))
player = pygame.draw.rect(DISPLAYSURF,(green),(playerX, playerY,20, 20))
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# INDENTATION
#<--|
if keys[pygame.K_z]:
playerY -= 5
if keys[pygame.K_s]:
playerY += 5
if keys[pygame.K_d]:
playerX += 5
if keys[pygame.K_q]:
playerX -= 5
pygame.display.update(player)
pygame.display.update()
FpsClock.tick(FPS)
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.
Related
This question already has an answer here:
Difference between rect.move() and rect.move_ip in pygame
(1 answer)
Closed 9 months ago.
So I can't get my sprite to jump when the key is pressed. I've fixed a few errors but now I have one that says: File "/Users/josh/Desktop/fresh/main.py", line 52, in
screen.blit(player, player_rect)
TypeError: invalid destination position for blit
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('Shooter Mania')
text = pygame.font.Font(None, 50)
clock = pygame.time.Clock()
#
bg = pygame.image.load('background.png')
#
player = pygame.image.load('player.png').convert_alpha()
player_rect = player.get_rect(midbottom=(80,300))
playerX = 0
playerY = 0
#
score_surf = text.render('SCORE:', False, 'white')
score_rect = score_surf.get_rect(center=(400,50))
#
enemy = pygame.image.load('enemy.png').convert_alpha()
enemy_rect = enemy.get_rect(bottomright=(600,300))
#
#
#
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player_rect = player_rect.move_ip(0,5)
print(f'Jump')
screen.blit(bg, (0,0))
pygame.draw.rect(screen, 'Pink',score_rect)
screen.blit(score_surf,score_rect)
enemy_rect.x -= 4
if enemy_rect.right <= 0:
enemy_rect.left = 800
screen.blit(enemy,enemy_rect)
# if player_rect.y <= 400:
# player_rect.y = 300
# playerX +=1
# player_rect.y += playerX
screen.blit(player, player_rect)
pygame.display.update()
clock.tick(60)
When you move your rect, you use move_ip() which updates the rect in place (hence the name) and returns nothing, i.e. None. This then gets assigned to the player_rect variable and passed to screen.blit(), which fails. The solution is not to use the in-place version of move:
if event.key == pygame.K_SPACE:
player_rect = player_rect.move(0, 5)
print(f'Jump')
This will return the updated rect and assign it to player_rect. Alternatively, you could use the in-place version without assignment:
if event.key == pygame.K_SPACE:
player_rect.move_ip(0, 5)
print(f'Jump')
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 11 months ago.
Alright so the enemy I have only moves when I move my character, but it also sometimes spawns completely out of the game's window. Also the background, player and enemy just kinda... Dont pop up until I press a movement button, im so sorry for this lmfao Also sorry about the text and stuff I was told to put them there for reminders on wtf to
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("BackgroundImg.jpg")
pygame.display.set_caption("Real Hero")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
playerX_change = 0
enemyImg = pygame.image.load('Enemy.jpg')
enemyX = random.randint(0, 746)
enemyY = random.randint(50, 746)
enemyX_change = 0.2
enemyY_change = 40
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
running = True
while running:
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
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.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.2
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.2
enemyY += enemyY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
The reason as to why the enemy only moves when you press a movement key is because you have the enemy movement code inside of the if event.type == pygame.KEYDOWN: if statement. You are also only updating the screen when you press a movement key because the pygame.display.update() is also inside that ifstatement. You need to move any code that shouldn't only be run when a key is pressed out of the if statement.
So recently I have been trying to make my first big game but I'm stuck on the shooting phase. Basically, my problem is I want to make it so that I can shoot multiple bullets and move around while doing so, but I can't seem to figure it out.
Code:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Shooter Game!")
#Variables
playerX = 5
playerY = 5
black = (0,0,0)
blue = (0,0,255)
red = (255,0,0)
clicking = False
bulletX = playerX
bulletY = playerY
#End Of Variables#
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill(blue)
player = pygame.image.load("Young_Link_2D.png").convert_alpha()
screen.fill(blue)
player1 = pygame.transform.scale(player, (100,100))
screen.blit(player1,(playerX,playerY))
keyPressed = pygame.key.get_pressed()
#Controls
if keyPressed[pygame.K_a]:
playerX -= 1
bulletX = playerX
if keyPressed[pygame.K_d]:
playerX += 1
bulletX = playerX
if keyPressed[pygame.K_w]:
playerY -= 1
bulletY = playerY
if keyPressed[pygame.K_s]:
playerY += 1
bulletY = playerY
#End of Controls
#Shooting
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
Bullet = pygame.draw.rect(screen, red, (bulletX+35,bulletY + 60,10,25))
bulletY = bulletY + 1
pygame.display.update()
Here is what i meant by classes:
import pygame
import Bullet
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Shooter Game!")
# Variables
playerX = 5
playerY = 5
black = (0, 0, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
clicking = False
bulletX = playerX
bulletY = playerY
bullets = []
newBullet = False
# End Of Variables#
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill(blue)
player = pygame.image.load("100x100_logo.png").convert_alpha()
screen.fill(blue)
player1 = pygame.transform.scale(player, (100, 100))
screen.blit(player1, (playerX, playerY))
keyPressed = pygame.key.get_pressed()
# Controls
if keyPressed[pygame.K_a]:
playerX -= 1
bulletX = playerX
if keyPressed[pygame.K_d]:
playerX += 1
bulletX = playerX
if keyPressed[pygame.K_w]:
playerY -= 1
bulletY = playerY
if keyPressed[pygame.K_s]:
playerY += 1
bulletY = playerY
# End of Controls
# Shooting
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
bullets.append(Bullet.Bullet(bulletX + 35, bulletY + 60))
for i in range(len(bullets)):
bullets[i].draw(screen, (255, 0, 0))
bullets[i].move(1)
print(len(bullets))
pygame.display.update()
Thats your main code. There is still a bug where there is no cooldown so multiple bullets are created consecutively when holding the mouse button.
import pygame
class Bullet:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self, win, colour):
pygame.draw.rect(win, colour, (self.x, self.y, 10, 25))
def move(self, speed):
self.y += speed
and thats the bullet class to create multiple instances
See How do I stop more than 1 bullet firing at once? and How can i shoot a bullet with space bar?.
You need to manage the bullets in a list:
bullets = []
Add a new bullet to the list when the mouse click is detected. Use pygame.Rect objects to represent the bullets. The events must be handled in the event loop:
for event in pygame.event.get():
# [...]
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
bullets.append(pygame.Rect(playerX, playerY, 35, 60))
Move the bullets in a loop. Remove the bullets from the list, when they when they move off the screen. See How to remove items from a list while iterating?:
for bullet in bullets[:]:
bullet.y += 5
if bullet.top > 600:
bullets.remove(bullet)
Draw all the bullets in the list in another loop:
for bullet in bullets:
pygame.draw.rect(screen, red, bullet)
Complete example:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Shooter Game!")
#Variables
player = pygame.image.load("Young_Link_2D.png").convert_alpha()
playerX, playerY = 5, 5
black = (0,0,0)
blue = (0,0,255)
red = (255,0,0)
clicking = False
bullets = []
#End Of Variables#
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
bullets.append(pygame.Rect(playerX, playerY, 35, 60))
keyPressed = pygame.key.get_pressed()
playerX += keyPressed[pygame.K_d] - keyPressed[pygame.K_a]
playerY += keyPressed[pygame.K_s] - keyPressed[pygame.K_w]
for bullet in bullets[:]:
bullet.y += 5
if bullet.top > 600:
bullets.remove(bullet)
screen.fill(blue)
player1 = pygame.transform.scale(player, (100,100))
screen.blit(player1,(playerX,playerY))
for bullet in bullets:
pygame.draw.rect(screen, red, bullet)
pygame.display.update()
pygame.quit()
exit()
This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
Closed 1 year ago.
I'm having a problem with moving a player object in Pygame. I have created the class of Player and called it on my main file, but whenever I try to move the player object it won't move. I have also called it inside the Game loop but still, it won't move. I don't know what's going on: Here is the code I have done so far:
screen.py
import pygame
screen.py
class Screen:
def __init__(self, width, height):
self.width = width
self.height = height
def screen_display(self):
return pygame.display.set_mode((self.width,self.height))
player.py
import pygame
class Player:
playerY_change = 0.5
def __init__(self, playerX,playerY, playerWidth,playerHeight,screen,):
self.playerX = playerX
self.playerY = playerY
self.playerWidth = playerWidth
self.playerHeight = playerHeight
self.screen = screen
def create_player(self):
return pygame.draw.rect(self.screen, [0, 0, 0], [self.playerX, self.playerY, self.playerWidth, self.playerHeight])
enemy.py
import pygame
class Enemy:
def __init__(self, enemyX,enemyY, enemyWidth,enemyHeight,screen):
self.enemyX = enemyX
self.enemyY = enemyY
self.enemyWidth = enemyWidth
self.enemyHeight = enemyHeight
self.screen = screen
def create_enemy(self):
return pygame.draw.rect(self.screen, [0, 0, 0], [self.enemyX, self.enemyY, self.enemyWidth, self.enemyHeight])
Here is my Main file main.py:
import pygame,random,math
from screen import Screen
from player import Player
from enemy import Enemy
# Pygame initilaize
pygame.init()
#Game Screen
screenWidth = 800
screenHeight = 500
window = Screen(screenWidth,screenHeight)
screen = window.screen_display()
# Title and Logo
pygame.display.set_caption("ShootBhoot")
icon = pygame.image.load("logo.png")
pygame.display.set_icon(icon)
# Player
playerX = 10
playerY = 10
playerY_change = 200
playerWidth = 15
playerHeight = 50
player = Player(playerX,playerY,playerWidth,playerHeight,screen)
#Enemy
enemyWidth = 15
enemyHeight = 50
enemyX = screenWidth - (enemyWidth + 10)
enemyY = 10
enemy = Enemy(enemyX,enemyY,enemyWidth,enemyHeight,screen)
# Ball
ballRadius = 10
ballX = random.randint(0, screenWidth - 10)
ballY = random.randint(0, screenHeight - 10)
ballX_change = 0.01
ballY_change = 0
def ball_create(screen, ballX, ballY, radius):
return pygame.draw.circle(screen, (10, 10, 10), (ballX, ballY), radius)
def distance(playerX,playerY,ballX,ballY):
calc = math.sqrt((playerX - ballX)**2 + (playerY - ballY)**2)
print(calc)
#Game loop
running = True
while running:
screen.fill((255,255,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Even while I click btn It won't move
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
playerY_change = -0.5
if event.key == pygame.K_DOWN:
playerY_change = 0.5
# Player Move Object not moving
playerY = playerY_change
enemy.create_enemy()
ball_create(screen, ballX, ballY, ballRadius)
player.create_player()
pygame.display.flip()
pygame.display.update()
playerY is just used when to create the player. You have to change the coordinate attribute of the player:
playerY = playerY_change
player.playerY += playerY_change
However I recommend to use pygame.key.get_pressed() instead of the keyboard 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 list 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:
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.playerY -= 1
if keys[pygame.K_RIGHT]:
player.playerY += 1
screen.fill((255,255,255))
enemy.create_enemy()
ball_create(screen, ballX, ballY, ballRadius)
player.create_player()
pygame.display.flip()
Struggling to make my 'cowboy' image move across the screen in all 4 directions. Nothing happens whenever I press the keys. Help please. This is based off the exercise questions in Python Crash Course.
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Move the Cowboy")
cowboy = pygame.image.load('images/cowboy.bmp')
cowboy = pygame.transform.scale(cowboy, (50, 50))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((240,237,207))
cowboy_rect = cowboy.get_rect()
screen_rect = screen.get_rect()
screen_center = screen_rect.center
cowboy_rect.center = screen_rect.center
cowboy_rect.x = cowboy_rect.width
cowboy_rect.y = cowboy_rect.height
cowboy_x = float(cowboy_rect.x)
cowboy_y = float(cowboy_rect.y)
screen.blit(cowboy, screen_center)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
cowboy_x -= 5
elif keys[pygame.K_RIGHT]:
cowboy_x += 5
elif keys[pygame.K_UP]:
cowboy_y -= 5
elif keys[pygame.K_DOWN]:
cowboy_y += 5
pygame.display.update()
pygame.quit()
You are putting the image at the middle of the screen with screen.blit(cowboy, screen_center). Change screen_center to cowboy_rect as this stores the position of the image.
Also take the cowboy_rect = cowboy.get_rect() out of the loop as this resets it back to 0.
You are then putting the position of cowboy_rect to the center of the screen by doing cowboy_rect.center = screen_rect.center, take that out of the loop so it only happens once, not every frame. Then its getting changed to 50,50 by cowboy_rect.x = cowboy_rect.width and cowboy_rect.y = cowboy_rect.height, so take that out.
Then at the keys, change cowboy_x -= 5 to cowboy_rect.x -= 5 and do the same for the others.
By putting this in: screen.blit(cowboy, screen_center), you are saying that this image will appear at the center of the screen. You want it to appear at (cowboy_x, cowboy_y), as these are the positionment variables you defined. Here is a working version of your code:
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Move the Cowboy")
cowboy = pygame.image.load('images/cowboy.bmp')
cowboy = pygame.transform.scale(cowboy, (50, 50))
cowboy_x, cowboy_y = 0, 0
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((240,237,207))
screen.blit(cowboy, (cowboy_x, cowboy_y))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
cowboy_x -= 1
elif keys[pygame.K_RIGHT]:
cowboy_x += 1
elif keys[pygame.K_UP]:
cowboy_y -= 1
elif keys[pygame.K_DOWN]:
cowboy_y += 1
pygame.display.update()
pygame.quit()
I took the liberty of taking out some completely useless lines of code, and of bringing the stuff you didn't need to re-iterate out of the loop, to save processing space.