I want to create my own game. My game's core idea is straightforward. It seems like Chrome Dino. But when developing, I don't know how to end the game when once the player has stepped into the trap. I found the colliderect function after asking around and searching, but it wasn't as effective as I had hoped (Even if the player hasn't seen him step on the trap, he will be notified of a loss. I think the reason for this is because my character photo has a 2/3 white background), If there is a method to remedy this, please tell me. I'm sorry if the question is unclear; I'm not a very good English speaker. My code:
import pygame
from pygame.locals import *
from assets import Image, Color, Audio
import time
import threading
from random import randint
pygame.init()
Audio.theme()
client = pygame.display.set_mode((600,300))
pygame.display.set_caption("Run Now")
running = True
clock = pygame.time.Clock()
sprite = 1
character = Image.characterRun(sprite)
speed = 0.05
characterX = 10
characterY = 100
bgX = 0
bgY = 0
bgMove = 3
score = 0
increaseSpeed = pygame.USEREVENT + 1
jumping = False
charJump = pygame.transform.scale(pygame.image.load("./assets/JumpAnimation/10.png"), (598/4, 523/4))
def update():
while True:
global sprite
global character
global running
global speed
if running:
sprite += 1
character = Image.characterRun(sprite)
else:
break
time.sleep(speed)
updateThread = threading.Thread(target=update)
updateThread.start()
pygame.time.set_timer(increaseSpeed, 120000, 5)
Image.characterRun(sprite)
jumpHeight = 15
trapX = 450
trapY = 190
gravity = 1
vel = jumpHeight
while running:
clock.tick(60)
client.fill(Color.white())
background1 = client.blit(Image.background(), (bgX,bgY))
background2 = client.blit(Image.background(), (bgX + 600,bgY))
trap = client.blit(Image.trap(), (trapX, trapY))
score += 1
bgX -= bgMove
trapX -= bgMove
if trapX <= -20:
trapX = randint(200, 550)
if bgX+600 <= 0:
bgX = 0
if jumping:
characterY -= vel
vel -= gravity
player = client.blit(charJump, (characterX, characterY))
if vel < -jumpHeight:
jumping = False
vel = jumpHeight
else:
player = client.blit(character, (characterX, characterY))
if player.colliderect(trap):
print("Lose")
for e in pygame.event.get():
if e.type == increaseSpeed:
speed -= 0.01
speed = round(speed, 2)
bgMove += 1
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_SPACE:
if not jumping:
jumping = True
pygame.display.update()
pygame.quit()