K
K
Konstantin2021-12-17 19:26:45
pygame
Konstantin, 2021-12-17 19:26:45

How to implement an exit from the application if the player is not on one of the pygame.Rect?

I'm making a rhythm game. The game is that you have to stay on the path along which the player is moving.
Here's a visual: https://drive.google.com/file/d/1KpBerWIb8xXl7wrMo...

Here's the code:

import pygame
from pygame.locals import *
import sys
import json

pygame.init()
pygame.mixer.init()

with open('settings.json') as f:
  settings = json.load(f)

resolution = (settings["width"], settings["height"])

screen = pygame.display.set_mode(resolution)
pygame.display.set_caption('Tap Tap Beat - Playing')

clock = pygame.time.Clock()

playerX = 256
playerY = 488
speed = 3

motions = ["up", "left", "right"]
motion = "up"

player = pygame.image.load("player.png")
playerRect = pygame.Rect(playerX, playerY, 48, 48)

point_Up = pygame.image.load("point_up.png")
point_Up = pygame.transform.scale(point_Up, (48, 48))

point_Left = pygame.image.load("point_left.png")
point_Left = pygame.transform.scale(point_Left, (48, 48))

point_Right = pygame.image.load("point_right.png")
point_Right = pygame.transform.scale(point_Right, (48, 48))

rotateMap = [1,0,2]
rotates = 0

gameMap = [
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,4,0,0,0,0,0],
  [0,0,1,0,0,0,0,0],
  [0,0,2,1,3,0,0,0],
  [0,0,0,0,1,0,0,0],
  [0,0,0,0,1,0,0,0],
  [0,0,0,0,1,0,0,0]
]

collisionRects = []

fall = False

for a in range(8):
  for b in range(8):
    if gameMap[a][b] == 1:
      collisionRects.append(pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64)))
    if gameMap[a][b] == 2:
      collisionRects.append(pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64)))
    if gameMap[a][b] == 3:
      collisionRects.append(pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64)))
    if gameMap[a][b] == 4:
      collisionRects.append(pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64)))

def game():
  global playerX, playerY, rotates, motion, playerRect
  while True:
    clock.tick(60)

    screen.fill((53, 195, 215))

    for event in pygame.event.get():
      if event.type == QUIT:
        pygame.quit()
        sys.exit()
      elif event.type == KEYDOWN:
        if event.key == K_SPACE:
          try:
            motion = motions[rotateMap[rotates]]
            rotates += 1
          except IndexError:
            motion = None

    if motion == motions[0]:
      playerY -= speed
    elif motion == motions[1]:
      playerX -= speed
    elif motion == motions[2]:
      playerX += speed


    for a in range(8):
      for b in range(8):
        if gameMap[a][b] == 1:
          pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64))
        if gameMap[a][b] == 2:
          pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64))
          screen.blit(point_Up, (b*64, a*64))
        if gameMap[a][b] == 3:
          pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64))
          screen.blit(point_Left, (b*64, a*64))
        if gameMap[a][b] == 4:
          pygame.draw.rect(screen, (255, 255, 255), (b*64-8, a*64-8, 64, 64))
          screen.blit(point_Right, (b*64, a*64))

    playerRect = pygame.Rect(playerX, playerY, 48, 48)

    # playerRect = pygame.Rect(playerX, playerY, 48, 48)

    # pygame.draw.circle(screen, (255, 0, 0), (playerX, playerY), 20, 2)
    screen.blit(player, (playerX, playerY))

    pygame.display.flip()
game()


How to make it so that when the player does NOT touch the track, for example, the game closes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Chetchasov, 2022-04-02
@TalismanChet

pseudocode:

прог
нач
  алг элемент_жиз_цикла_программы
  нач
    для тчк из getallpoint(trailRect)
    нач
      если не playerRect.collidepoint(nxr) то
      нач
        sys.exit()
      кон
    кон
  кон
  алг getallpoint(rct: Rect = Rect(0, 0, 100, 100))
  нач
    верн: список = []
    для х из промежутка rct.x, rct.x+rct.w
    нач
      для у из промежутка rct.y, rct.y+rct.h
      нач
        добавить [х, у] к верн
      кон
    кон
  вернуть верн
  кон
кон.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question