-1

I'm trying to make a pygame game but I'm having a problem. I want when a character collects a coin, the score will change by one and the coin will disappear. I would also like the coins to appear in random places around the player. I've been trying to do it for a week and I still don't know how to do it, and my time is short :/

Any help?

This is my code:

import pygame
import sys
from pygame import mixer
import random

pygame.init()


display = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()


pygame.display.set_caption("Testowa nazwa bez pomysłu :/")
program_ico = pygame.image.load("icon.jpg")
pygame.display.set_icon(program_ico)

mixer.music.load('music.mp3')
mixer.music.play(-1)

bg = pygame.image.load('bg3.png')


player_walk_images = [pygame.image.load("pp1.png"), pygame.image.load("pp2.png"), pygame.image.load("pp3.png"), pygame.image.load("pp4.png")]
player_walk_up = [pygame.image.load("pb1.png"), pygame.image.load("pb2.png"), pygame.image.load("pb3.png"), pygame.image.load("pb4.png")]
player_walk_down = [pygame.image.load("pf1.png"), pygame.image.load("pf2.png"), pygame.image.load("pf3.png"), pygame.image.load("pf4.png")]
player_stay = [pygame.image.load("ps1.png"),pygame.image.load("ps2.png"),pygame.image.load("ps3.png"),pygame.image.load("ps4.png")]

txt_coin = [pygame.image.load("coin1.png"),pygame.image.load("coin2.png"),pygame.image.load("coin3.png"),pygame.image.load("coin4.png"),pygame.image.load("coin5.png"),pygame.image.load("coin6.png"),pygame.image.load("coin7.png"),pygame.image.load("coin8.png"),pygame.image.load("coin9.png"),pygame.image.load("coin10.png"),pygame.image.load("coin11.png"),pygame.image.load("coin12.png"),pygame.image.load("coin13.png"),pygame.image.load("coin14.png"),pygame.image.load("coin15.png"),pygame.image.load("coin16.png"),pygame.image.load("coin17.png"),pygame.image.load("coin18.png"),pygame.image.load("coin19.png"),pygame.image.load("coin20.png"),pygame.image.load("coin21.png")]




borderN = -840
borderE = 1085
borderS = 1050
borderW = -870



class Player:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.animation_count = 0
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.staing = True
        self.hitbox = pygame.Rect(self.x, self.y,self.width, self.height)

    def main(self, display):
        if self.animation_count + 1 >= 16:
            self.animation_count = 0

        self.animation_count += 1

        if self.moving_right:
            display.blit(pygame.transform.scale(player_walk_images[self.animation_count//4], (128, 128)), (self.x, self.y))
        elif self.moving_left:
            display.blit(pygame.transform.scale(pygame.transform.flip(player_walk_images[self.animation_count//4], True, False), (128, 128)), (self.x, self.y))
        elif self.moving_up:
            display.blit(pygame.transform.scale(player_walk_up[self.animation_count//4], (128, 128)), (self.x, self.y))
        elif self.moving_down:
            display.blit(pygame.transform.scale(player_walk_down[self.animation_count//4], (128, 128)), (self.x, self.y))

        elif self.staing:
            display.blit(pygame.transform.scale(player_stay[self.animation_count//4], (128, 128)), (self.x, self.y))

        self.moving_right = False
        self.moving_left = False

player = Player(560,300, 32, 32)

display_scroll = [0,0]


class Cash:
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.hitbox = pygame.Rect(self.x, self.y,self.width,self.height)
        self.animation_count = 0

    def tick(self):
        self.hitbox = pygame.Rect(self.x, self.y)

    def draw(self,display):
        if self.animation_count + 1 >= 16:
            self.animation_count = 0

        self.animation_count += 1
        if True:
            display.blit(pygame.transform.scale(txt_coin[self.animation_count//21], (128, 128)), (self.x, self.y))



rx = random.randint(-870, 1085)
ry = random.randint(-840, 1050)




clocker = 0
score = 0
coins = []
while True:
    display.fill((24,164,86))
    #display.blit(bg)



    for coin in coins:
        if player.hitbox.colliderect(coin.hitbox):
            coins.remove(coin)
            score += 1

    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            sys.exit(0)

    keys = pygame.key.get_pressed()

    #pygame.draw.rect(display, (255,255,255), (100-display_scroll[0], 100-display_scroll[1], 16, 16))
    display.blit(bg,((-940-display_scroll[0], -1200-display_scroll[1], 16, 16)))

    if keys[pygame.K_a]:
        display_scroll[0] -= 5
        staing = False
        player.moving_left = True

    elif keys[pygame.K_d]:
        display_scroll[0] += 5
        staing = False
        player.moving_right = True

    elif keys[pygame.K_w]:
        display_scroll[1] -= 5
        staing = False
        player.moving_up = True
        player.moving_down = False

    elif keys[pygame.K_s]:
        display_scroll[1] += 5
        staing = False
        player.moving_down = True
        player.moving_up = False

    else:
        player.moving_down = False
        player.moving_up = False
        staing = True

    if display_scroll[0] < borderW:
        display_scroll[0] = borderW
    if display_scroll[0] > borderE:
        display_scroll[0] = borderE
    if display_scroll[1] < borderN:
        display_scroll[1] = borderN
    if display_scroll[1] > borderS:
        display_scroll[1] = borderS

    cash = Cash(-display_scroll[0], -display_scroll[1], 16, 16)

    clocker += pygame.time.Clock().tick(60) / 1000
    if clocker >= 2:
        clocker = 0
        coins.append(cash)

    player.main(display)
    cash.draw(display)

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


Hope for quick help

Most often, when I tried a solution, the program did not show any errors, and the coin was not visible anywhere ://

If you need other resources like textures please write

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Akinne
  • 3
  • 2
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Mar 26 '22 at 20:08

1 Answers1

0

So basically you can make a def to check if you are close to the coin like this:

collected = False
def collectCheck(x2, x1, y2, y1):
global collected
distance = math.sqrt(math.pow(x2 - x1, 2) + (math.pow(y2 - y1, 2)))
    
# if the distance is smaller than the distance u set then the apple is collected
if distance < 70:
    return True
else:
    return False

and then you can make an if statement to check if it is true or not then increase the point and also remove the coin

score = 1
if collected:
        score += 1
        display.blit('image you want', (random coords that is out of the screen))

Finally to make a random coordinate for your coins, you can do this:

coinX = random.randint(0, screen width)
coinY = random.randint(0, screen height)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Please read [How do I detect collision in pygame?](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907) – Rabbid76 Mar 26 '22 at 14:30