#17 Create Score Board Using pygame🥀 in Python.

Поділитися
Вставка
  • Опубліковано 19 тра 2024
  • Dear Monkey's, Today You Will Learn - Create Score Board Using pygame
    #coding #python #computer #training #banglacomputertutorial #graphics #turtle #trainingvideo #learning #computermonkeys

КОМЕНТАРІ • 2

  • @babycoderhastoknow
    @babycoderhastoknow Місяць тому

    Vai nirash koriyen na please full koraiyen playlist ta

  • @monkeys5731
    @monkeys5731  Місяць тому

    import pygame
    import os
    import random
    pygame.init()
    # Set your screen
    screen = pygame.display.set_mode((1024, 512))
    pygame.display.set_caption("Play Your Game Now....")
    # Working only RGB color mode
    white = (255, 255, 255)
    red = (255, 0, 0)
    blue = (0, 0, 255)
    black = (0, 0, 0)
    deep_green = (0, 78, 0)
    cup_width, cup_height = 150, 150
    MY_cupter = pygame.image.load(os.path.join('G:\Pygame', 'cupter.png'))
    MY_cupter_image = pygame.transform.rotate(pygame.transform.scale(MY_cupter, (cup_width, cup_height)), 0)
    intro_back_image = pygame.image.load("G:\Pygame\intro_back.jpg")
    fps = 60
    x, y = 50, 50
    v = 5
    score = 0 # Initialize score
    class Button:
    def __init__(self, color, x, y, width, height, text=''):
    self.color = color
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.text = text
    def draw(self, win, outline=None):
    # Call this method to draw the button on the screen
    if outline:
    pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
    pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
    if self.text != '':
    font = pygame.font.SysFont(None, 30)
    text = font.render(self.text, 1, (0, 0, 0))
    win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2),
    self.y + (self.height / 2 - text.get_height() / 2)))
    def is_over(self, pos):
    # Pos is the mouse position or a tuple of (x, y) coordinates
    return self.x < pos[0] < self.x + self.width and self.y < pos[1] < self.y + self.height
    class Fireball:
    def __init__(self, x, y):
    self.x = x
    self.y = y
    self.radius = 10
    self.color = red
    self.speed = 10
    def draw(self, win):
    pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
    def move(self):
    self.x += self.speed # Fireball moves right side
    class Stone:
    def __init__(self, x, y):
    self.x = x
    self.y = y
    self.width = 30
    self.height = 30
    self.color = black
    self.speed = 5
    self.y_direction = random.choice([-1, 1]) # Random initial direction (up or down)
    self.collided = False # Track whether this stone has collided with the cupter
    def draw(self, win):
    pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height))
    def move(self):
    self.x -= self.speed # Stone moves left side
    self.y += self.y_direction * self.speed # Stone moves up or down
    # Change direction randomly
    if random.randint(1, 60) == 1:
    self.y_direction *= -1
    # Keep the stone within the screen bounds
    if self.y = screen.get_height() - self.height:
    self.y_direction *= -1
    play_button = Button((0, 255, 0), 100, 400, 100, 50, 'Play')
    close_button = Button((255, 0, 0), 800, 400, 100, 50, 'Close')
    back_button = Button((0, 0, 255), 450, 400, 100, 50, 'Back')
    welcome_message_visible = False
    welcome_message_timer = 0
    fireballs = [] # List to hold fireballs
    stones = [] # List to hold stones
    def intro_main():
    global welcome_message_visible
    global welcome_message_timer
    intro = True
    while intro:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    quit()
    if event.type == pygame.MOUSEBUTTONDOWN:
    if play_button.is_over(pygame.mouse.get_pos()):
    # Show the welcome message and start the timer
    welcome_message_visible = True
    welcome_message_timer = pygame.time.get_ticks()
    intro = False
    elif close_button.is_over(pygame.mouse.get_pos()):
    pygame.quit()
    quit()
    screen.blit(intro_back_image, (0, 0))
    play_button.draw(screen, (0, 0, 0))
    close_button.draw(screen, (0, 0, 0))
    pygame.display.update()
    # If 'Play' is clicked, hide the intro window and start the game
    main()
    def text(message, size, x_pos, y_pos):
    font = pygame.font.SysFont(None, size)
    render = font.render(message, True, blue)
    screen.blit(render, (x_pos, y_pos))
    pygame.display.update()
    def show_score(score):
    font = pygame.font.SysFont(None, 35)
    text = font.render(f"Score: {score}", True, blue)
    screen.blit(text, (10, 10))
    def movement(keys_pressed, cupter):
    if keys_pressed[pygame.K_a] and cupter.x - v > 0:
    cupter.x -= v
    if keys_pressed[pygame.K_d] and cupter.x + v < 885:
    cupter.x += v
    if keys_pressed[pygame.K_w] and cupter.y - v > -40:
    cupter.y -= v
    if keys_pressed[pygame.K_s] and cupter.y + v < 400:
    cupter.y += v
    def win_component(cupter):
    screen.fill(white)
    screen.blit(MY_cupter_image, (cupter.x, cupter.y))
    for fireball in fireballs:
    fireball.draw(screen)
    for stone in stones:
    stone.draw(screen)
    show_score(score)
    pygame.display.update()
    def main():
    global welcome_message_visible
    global welcome_message_timer
    global score
    cupter = pygame.Rect(x, y, cup_width, cup_height)
    clock = pygame.time.Clock()
    win_run = True
    # Main game loop
    while win_run:
    clock.tick(fps)
    # Event handling
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    win_run = False
    if event.type == pygame.MOUSEBUTTONDOWN:
    if back_button.is_over(pygame.mouse.get_pos()):
    welcome_message_visible = False
    intro_main()
    elif event.button == 1: # Check left mouse button
    fireballs.append(Fireball(cupter.x + cup_width, cupter.y + cup_height // 2))
    keys_pressed = pygame.key.get_pressed()
    movement(keys_pressed, cupter)
    # Check for collisions between cupter and stones
    for stone in stones:
    if cupter.colliderect(pygame.Rect(stone.x, stone.y, stone.width, stone.height)):
    if not stone.collided:
    score += 1 # Increment score by 1
    stone.collided = True # Mark stone as collided
    win_component(cupter)
    if welcome_message_visible:
    current_time = pygame.time.get_ticks()
    if current_time - welcome_message_timer >= 3000: # This is milliseconds
    welcome_message_visible = False
    if welcome_message_visible:
    text("Welcome to my game", 100, 200, 200)
    back_button.draw(screen, (0, 0, 0))
    # Move fireballs
    for fireball in fireballs:
    fireball.move()
    # Remove fireballs that are off-screen
    fireballs[:] = [fireball for fireball in fireballs if fireball.x < screen.get_width()]
    # Move stones
    for stone in stones:
    stone.move()
    # Remove stones that are off-screen
    stones[:] = [stone for stone in stones if stone.x > 0]
    # Spawn a new stone periodically
    if len(stones) < 5 and random.randint(1, 50) == 1: # Adjust spawning frequency as needed
    stones.append(Stone(screen.get_width(), random.randint(0, screen.get_height() - 30)))
    pygame.display.update()
    pygame.quit()
    if __name__ == "__main__":
    intro_main()