vendredi 25 juin 2021

How do my make my image (rect) collide with my randomly generated circles to end the game?

I have been trying to solve this wor weeks. This is a free falling pit game, where my character (in this case a chimpanzee png) falls from the top of the screen to the bottom while trying to dodge the random black circles. I have tried so many angles at tackling this, I have tried the standard collision I was taught (pygame.sprite.groupcollide(Group1, Group2, False, True, collided = None) I have tried doing collisions with the colour black only, different formats of spawning my image and all that, and I haven't been able to find anything that works with my code. It has resulted inmy code being very messy. I have tried to clean it up for this, but it still might be hard to understand, but if anybody has any solution to this, please let me know as I have been stumped for weeks. I just want the game to close or "game over" when they touch a circle Code:

    import sys
import pygame as pg
RED = (255, 0, 0)
GRAY = (150, 150, 150)
GREEN =(34, 177, 76)
BLACK = (0,0,0)

import random
import math




def main():
    width, height = 1024, 768
    hbox, vbox = 80, 80
    w, h = 640, 240
    screen = pg.display.set_mode((width, height))
    BG = pg.image.load('jungle.jpg').convert()
    img = pg.image.load('MONKEY.png')
    img = pg.transform.scale(img, (80, 80))
    img.convert()
    rect = img.get_rect()
    rect.center = w//2, h//2
    clock = pg.time.Clock()
    Score = 0
    class Circle(pg.sprite.Sprite):
        def __init__(self):
            #You can initialise the size to a random value
            self.pos = [random.randint(0, 1000), random.randint(180, 600)]
            self.color = (0,0, 0)
            self.radius = 20

        def draw(self):
            pg.draw.circle(screen, self.color, (self.pos[0], self.pos[1]), self.radius)


    circles = []

    for i in range(20):
        circles.append(Circle())

    def checkIntersection(c1, c2):
        dx = c1.pos[0] - c2.pos[0]
        dy = c1.pos[1] - c2.pos[1]
        d = math.hypot(dx, dy)
        if d < c1.radius + c2.radius:
            return True
        return False


    for i in range(19):
        while checkIntersection(circles[i], circles[i + 1]):
            circles[i].pos = random.randint(0, 1000), random.randint(0, 700)
        
        velocity = (0, 0)
        done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True

        keys = pg.key.get_pressed()

        # booster
        move = 8 if keys[pg.K_LSHIFT] else 4

        if keys[pg.K_a]:  #to move left
            rect.x -= move
        if rect.x < 0 : rect.x = 0

        if keys[pg.K_d]: #to move right
            rect.x += move
        if rect.x > width-hbox : rect.x = width - hbox
        
        
        Score += 1
        rect.y += 2.5
        screen.blit(BG, (0,0))    
        screen.blit(img,rect)
        pg.draw.rect(screen, RED, rect, 1)
        pg.draw.polygon(screen, GREEN, ((1024,768), (0,768), (0,640),(1024,640)))
        font = pg.font.SysFont("comicsansms", 45)
        text = font.render (" " + str(Score), 1, BLACK)
        screen.blit(text,(480,700))
        pg.event.get()
        for circle in circles:
            circle.draw()
        pg.display.update()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()



Aucun commentaire:

Enregistrer un commentaire