0

So basically whenever I want to move the player a specific distance I need to click the button but I want to be able to just hold the button without having to press it every time I want to move. Can someone explain what the problem is because I have to idea how to fix it.

import pygame         
from sys import exit
import random

pygame.init()

WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My game!")
fps = pygame.time.Clock()
active = True

time = 0

score = 0
startedplus = False
startedminus = False

test_font = pygame.font.Font("letters.ttf", 50)
text_surface = test_font.render((f"Din poäng: {score}") , False, (64,64,64))
text_rect = text_surface.get_rect(center = (400,50))

#game over
game_over = pygame.image.load("game_over.png").convert()

#Heart
heart = pygame.image.load("heart.png").convert_alpha()
heart = pygame.transform.rotozoom(heart,0,0.6)

#Box
box = pygame.image.load("box.png").convert_alpha()
box = pygame.transform.rotozoom(box,0,0.3)
box_rect = box.get_rect(midbottom = (100,305))

#Background och Ground
background = pygame.image.load("sky.png").convert()
ground = pygame.image.load("ground.png").convert()

#player PNG 
player = pygame.image.load("player.png").convert_alpha()
player = pygame.transform.rotozoom(player,0,2.5)
player_rect = player.get_rect(midbottom = (400,325))

#pickaxe PNG
pickaxe = pygame.image.load("pickaxe.png").convert_alpha()
pickaxe = pygame.transform.rotozoom(pickaxe,0,3.5)
pickaxe_rect = pickaxe.get_rect(center = (400,325))

#Gravity
player_gravity = 0
pickaxe_gravity = 0

adding = 0.01

hearts = 4
collided_in_the_last_frame = False

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()
    
    if active:
      if event.type == pygame.MOUSEBUTTONDOWN:
        if player_rect.collidepoint(event.pos) and player_rect.bottom >= 325: 
          player_gravity = -20
          pickaxe_gravity = -20
      
      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT or event.key == pygame.K_a:
          player_rect.x -= 40
          pickaxe_rect.x -= 40

        if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
          player_rect.x += 40
          pickaxe_rect.x += 40
          
        if (event.key == pygame.K_SPACE or event.key == pygame.K_w or event.key == pygame.K_UP) and player_rect.bottom >= 325:
          player_gravity = -20
          pickaxe_gravity = -20
    else:
      if event.type == pygame.KEYDOWN and event.key == pygame.K_x:
        box_x = random.randint(10, 800)
        box_rect = box.get_rect(midbottom = (box_x,305))
        hearts = 4
        active = True
        
  if active:
    text_surface = test_font.render((f"Din poäng: {score}") , False, (64,64,64))


    time += 1

    if time == 500:
      score += 1
      time = 0 

    
    collides = player_rect.colliderect(box_rect)
    if collides and not collided_in_the_last_frame:
      hearts -= 1
    collided_in_the_last_frame = collides
    
    screen.blit(background,(0,0))
    screen.blit(ground,(0,300))

    if player_rect.colliderect(box_rect):
      box_x = random.randint(10, 800)
      box_rect = box.get_rect(midbottom = (box_x,305))
      

    if box_rect.right <= player_rect.left:
      box_rect.x += 1
    elif box_rect.left >= player_rect.right:
      box_rect.x -= 1

    if hearts == 0:
      active = False
    elif hearts == 1:
      screen.blit(heart, (35,35))
    elif hearts == 2:
      screen.blit(heart, (35,35))
      screen.blit(heart, (65,35))
    elif hearts == 3:
      screen.blit(heart, (35,35))
      screen.blit(heart, (65,35))
      screen.blit(heart, (95,35))
    else:
       screen.blit(heart, (35,35))
       screen.blit(heart, (65,35))
       screen.blit(heart, (95,35))
       screen.blit(heart, (125,35))
      
    screen.blit(box, box_rect)
    screen.blit(pickaxe, pickaxe_rect)
    screen.blit(player,player_rect)
    screen.blit(text_surface, text_rect)
    
    player_gravity += 1
    pickaxe_gravity += 1
    player_rect.y += player_gravity
    pickaxe_rect.y += pickaxe_gravity
    if pickaxe_rect.bottom >= 325: pickaxe_rect.bottom = 325
    if player_rect.bottom >= 325: player_rect.bottom = 325

    if player_rect.x <= -10 and pickaxe_rect.x <= -10:
      player_rect.x = -10
      pickaxe_rect.x = player_rect.x - 70

    if player_rect.x >= 700 and pickaxe_rect.x >= 700:
      player_rect.x = 700
      pickaxe_rect.x = player_rect.x - 70

    mouse_pos = pygame.mouse.get_pos()
    if player_rect.collidepoint(mouse_pos):
      player_gravity = -20
      pickaxe_gravity = -20
      player_rect.y += player_gravity
      pickaxe_rect.y += pickaxe_gravity
      if pickaxe_rect.bottom >= 325: pickaxe_rect.bottom = 325
      if player_rect.bottom >= 325: player_rect.bottom = 325

  else:
    screen.blit(game_over, (0,0))

  pygame.display.update()
  fps.tick(60)

This is my whole code. So basically whenever I want to move the player a specific distance I need to click the button but I want to be able to just hold the button without having to press it every time I want to move. Can someone explain what the problem is because I have to idea how to fix it.

0 Answers0