vendredi 18 février 2022

How can a randomize games in python

I've created one bit of code that just makes shapes fall from the top to the bottom of the screen continuously, I would like to do the same type of code but replace the shapes possibly with arrows instead and have both of them. And then when the user selects start game it would either be shapes or arrows, I know I need to use random but I cannot seem to figure it out? Would I need to put my program in a class and randomize that? I've included my code for the shapes game and the game loop.

Shapes Code:

import pygame
import os


WHITE = (250, 250, 250)
WIDTH, HEIGHT = 750, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Eye Reaction App !")
FPS = 60
IMAGE_WIDTH, IMAGE_HEIGHT = 60, 50

GOLD_STAR_IMAGE = pygame.image.load(
    os.path.join('Assets', 'gold_star.png'))
GOLD_STAR = pygame.transform.scale(GOLD_STAR_IMAGE, (IMAGE_WIDTH, IMAGE_HEIGHT))

GREEN_TRIANGLE_IMAGE = pygame.image.load(
    os.path.join('Assets', 'green_triangle.png'))
GREEN_TRIANGLE = pygame.transform.scale(GREEN_TRIANGLE_IMAGE, (IMAGE_WIDTH, IMAGE_HEIGHT))

LIGHT_BLUE_RECTANGLE_IMAGE = pygame.image.load(
    os.path.join('Assets', 'light_blue_rectangle.png'))
LIGHT_BLUE_RECTANGLE = pygame.transform.scale(LIGHT_BLUE_RECTANGLE_IMAGE, (IMAGE_WIDTH, IMAGE_HEIGHT))

ORANGE_SQUARE_IMAGE = pygame.image.load(
    os.path.join('Assets', 'orange_square.png'))
ORANGE_SQUARE = pygame.transform.scale(ORANGE_SQUARE_IMAGE, (IMAGE_WIDTH, IMAGE_HEIGHT))

PURPLE_HEXAGON_IMAGE = pygame.image.load(
    os.path.join('Assets', 'purple_hexagon.png'))
PURPLE_HEXAGON = pygame.transform.scale(PURPLE_HEXAGON_IMAGE, (IMAGE_WIDTH, IMAGE_HEIGHT))

RED_CIRCLE_IMAGE = pygame.image.load(
    os.path.join('Assets', 'red_circle.png'))
RED_CIRCLE = pygame.transform.scale(RED_CIRCLE_IMAGE, (IMAGE_WIDTH, IMAGE_HEIGHT))


def draw_window_one(star, triangle, rectangle, square, hexagon, circle):
    WIN.fill(WHITE)
    WIN.blit(GOLD_STAR, (star.x, star.y))
    WIN.blit(GREEN_TRIANGLE, (triangle.x, triangle.y))
    WIN.blit(LIGHT_BLUE_RECTANGLE, (rectangle.x, rectangle.y))
    WIN.blit(ORANGE_SQUARE, (square.x, square.y))
    WIN.blit(PURPLE_HEXAGON, (hexagon.x, hexagon.y))
    WIN.blit(RED_CIRCLE, (circle.x, circle.y))

    pygame.display.update()


def start_one():
    star = pygame.Rect(75, 0, WIDTH, HEIGHT)
    triangle = pygame.Rect(575, 0, WIDTH, HEIGHT)
    rectangle = pygame.Rect(175, 0, WIDTH, HEIGHT)
    square = pygame.Rect(275, 0, WIDTH, HEIGHT)
    hexagon = pygame.Rect(375, 0, WIDTH, HEIGHT)
    circle = pygame.Rect(475, 0, WIDTH, HEIGHT)

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        if star.y > HEIGHT:
            star.y = -1
        elif triangle.y > HEIGHT:
            triangle.y = -1
        elif rectangle.y > HEIGHT:
            rectangle.y = -1
        elif square.y > HEIGHT:
            square.y = -1
        elif hexagon.y > HEIGHT:
            hexagon.y = -1
        elif circle.y > HEIGHT:
            circle.y = -1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        star.y += 2
        triangle.y += 2
        rectangle.y += 2
        square.y += 2
        hexagon.y += 2
        circle.y += 2
        draw_window_one(star, triangle, rectangle, square, hexagon, circle)

    pygame.quit()

if __name__ == "__main__":
    start_one()

game loop:

def game_loop(self):
        while self.playing:
            self.check_events()
            if self.START_KEY:
                self.playing= False
                self.display.fill(self.BLACK)
                self.start_one()
            else:
                self.display.fill(self.BLACK)
                self.draw_text('Thanks for Playing', 30, self.DISPLAY_W/2, self.DISPLAY_H/2)
                self.window.blit(self.display, (0,0))
                pygame.display.update()
                self.reset_keys()



Aucun commentaire:

Enregistrer un commentaire