1
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

score = 0
startedplus = False
startedminus = False

test_font = pygame.font.Font("letters.ttf", 50)
text_surface = test_font.render((f"Score: {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:
    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))
      
    adding = adding + 0.001

    if player_rect.x <= 400:
      box_rect.x += (1 + adding)
    elif player_rect.x >= 400:
      box_rect.x -= (1 + adding)
        
    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

    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)

How can I make the box like follow the player. I want it like when the player is "right" of the box then the box will start going right and when the player is "left" of the box the box will start going left.

I tried many ways but none worked so in the end I was left with this:

    adding = adding + 0.001

    if player_rect.x <= 400:
      box_rect.x += (1 + adding)
    elif player_rect.x >= 400:
      box_rect.x -= (1 + adding)

And I thought it would work but it didnt.

1 Answers1

1

You have to compare the position of the box with the position of the player. e.g.:

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

Also not, that pygame.Rect is supposed to represent an area on the screen, therfore a pygame.Rect object can only store integral data.

The coordinates for Rect objects are all integers. [...]

The fractional part of the coordinates is lost when a floating point value is added to a coordinate of a pygame.Rect object. Therefore box_rect.x += (1 + adding) always adds 1 to and box_rect.x -= (1 + adding) always subtracts 2 (as long 0 < a <= 1). Also see Pygame doesn't let me use float for rect.move, but I need it

Rabbid76
  • 202,892
  • 27
  • 131
  • 174