2

I'm making a simple game with pygame where I wanted to move a picture, but that picture seems to be made every time it clicks, and it's not what I want at all, I want to move it by pressing a key, but every time Moves one is made

screen shot

Code:

# import modules
import pygame
from pygame.locals import *
import sys
import os
#################
pygame.init()
#################

#Colors
red = (255 , 0 , 0) # RED
green = (0, 255, 0) # GREEN
blue = (10, 60, 225) # BLUE
white = (255, 255, 255) # WHITE
black = (0, 0, 0) # BLACK

# window
window = pygame.display.set_mode((600, 400)) # window size
pygame.display.set_caption("Ball away") # title
window.fill(white) # backgrond color
img = pygame.image.load("bin/images/icon.png") # window icon
pygame.display.set_icon(img) # load icon

# screen
#lines
lineup = pygame.draw.line(window, black, (0,35), (100000,0),4)
linedown = pygame.draw.line(window, black, (0,350), (1000000,4),4)
#TEXTS
ping_text = "Ping : 50"
font = pygame.font.SysFont(None, 25)
t_p = font.render(ping_text, True, (0, 0, 0))
window.blit(t_p, (515, 10))

count_win = "0 - 0"
font = pygame.font.SysFont(None, 30)
c_w = font.render(count_win, True, (0, 0, 0))
window.blit(c_w, (284, 10))


exit_ico = pygame.image.load("bin/images/exit.png")
exit_i = pygame.transform.scale(exit_ico,(30, 30))
window.blit(exit_i , (3, 3))

# players
    # RED
P1_x = 3
P1_y = 140
speed = 5

Player_RED = pygame.image.load("bin/images/Player1_RED.png")
player1_red = pygame.transform.scale(Player_RED,(80, 85))

    # BLUE
P2_x = 520
P2_y = 145
speed = 5

Player_BLUE = pygame.image.load("bin/images/Player2_BLUE.png")
player2_blue = pygame.transform.scale(Player_BLUE,(80, 85))

# ball
Ball_lets = pygame.transform.scale(img,(35,35))
window.blit(Ball_lets, (300, 170))

# soundObj = pygame.mixer.Sound('bin/sounds/Music.mp3')
# soundObj.play()

# main loop
running = True
while running:
    
    
    for event in pygame.event.get():
        if event.type == QUIT:
            os.system('cls')
            running = False
            
        if event.type == KEYUP:
            if event.key == K_w:
                P1_y -= speed
            if event.key == K_s:
                P1_y += speed
        window.blit(player1_red, (P1_x, P1_y))

        if event.type == KEYUP:
            if event.key == K_UP:
                P2_y -= speed
            if event.key == K_DOWN:
                P2_y += speed
        window.blit(player2_blue, (P2_x, P2_y))

    pygame.display.update()
pygame.quit()
sys.exit()

Indentations are not my problem, I am a newbie to stackoverflow

please help

1 Answers1

1

You have to use to 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.

# main loop
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            os.system('cls')
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        P1_y -= speed
    if keys[pygame.K_s]:
        P1_y += speed
    if keys[pygame.K_UP]:
        P2_y -= speed
    if keys[pygame.K_DOWN]:
        P2_y += speed
   
    window.fill(0)
    window.blit(t_p, (515, 10))
    window.blit(c_w, (284, 10))
    window.blit(exit_i , (3, 3))
    window.blit(Ball_lets, (300, 170))
    window.blit(player1_red, (P1_x, P1_y))
    window.blit(player2_blue, (P2_x, P2_y))
    pygame.display.update()

Note, you have to redraw the entire scene in every frame. The typical PyGame application loop has to:

See also How can I make a sprite move when key is held down

Rabbid76
  • 202,892
  • 27
  • 131
  • 174