Christo Hardmn
Christo Hardmn

Reputation: 35

appear random in a game with Pygame

I have a problem, in fact, I wish I could do this alone but I think I don't know how to do that, I have a game like you know hit on the mole, my problem is that:

I would like to make it appear randomly but on very precise coordinates that's my code can you help me? My code is not finished yet so it's possible that other things appear strangely.

import pygame    
pygame.init()    
display_width = 600
display_height = 480

gameDisplay = pygame.display.set_mode((display_width, display_height))
fond = pygame.image.load("background.jpg").convert()
gameDisplay.blit(fond, (0, 0))
pygame.display.set_caption('Tape Taupe')
clock = pygame.time.Clock()

BAMimg = pygame.image.load('Marteau.png')
gameIcon = pygame.image.load('Taupe.png').convert_alpha()
pygame.display.set_icon(gameIcon)

BAMimg_width = 73
Taupe1, Taupe2, Taupe3, Taupe4, Taupe5, Taupe6 = pygame.image.load("Taupe.png").convert_alpha()

Taupe = [Taupe1, Taupe2, Taupe3, Taupe4, Taupe5, Taupe6]
for i in range(6):
        {

        }

perso1 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso1, (160, 55))

perso2 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso2, (320, 55))

perso3 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso3, (480, 55))

perso4 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 200))

perso5 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 200))

perso6 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 200))

perso7 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 350))

perso8 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 350))

perso9 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 350))

pygame.display.update()


def BAMImg(x, y):
    gameDisplay.blit(BAMImg, (x, y))
    x = (display_width * 0.45)
    y = (display_height * 0.8)

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -5
                if event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

                BAMimg(x, y)
                x += x_change
                y += y_change

                fond = pygame.image.load("background.jpg").convert()
                gameDisplay.blit(fond, (0, 0))
                pygame.display.update()
                clock.tick(60)


game_loop()
pygame.quit()
quit()

Upvotes: 1

Views: 173

Answers (1)

rawwar
rawwar

Reputation: 5012

if you want them to appear only at the specific coordinates, make a list of all the coordinate. then use random.choice . for example:

import random

lst_of_coordinates= [[1,2], [2,1], [5,5], [6,6]]
print(random.choice(lst_of_coordinates))

Upvotes: 4

Related Questions