#18 Destroy Stone Using 🔥Fire Ball, Pygame 🥀in Python.
Вставка
- Опубліковано 21 тра 2024
- Dear Monkey's, Today You Will Learn - 18 Destroy Stone Using 🔥Fire Ball
#coding #python #computer #graphics #training #banglacomputertutorial #turtle #learning #trainingvideo #computermonkeys
import pygame
import sys
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
life = 0
stones_destroyed = 0 # Initialize stone destroyed 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
self.visible = True # Add a visibility flag
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if self.visible: # Only draw if visible flag is True
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
if self.x < pos[0] < self.x + self.width:
if self.y < pos[1] < self.y + self.height:
return True
return False
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 # Move to the right
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 = []
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(label, score, x, y):
font = pygame.font.SysFont(None, 35)
text = font.render(f"{label}: {score}", True, blue)
screen.blit(text, (x, y))
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("Life", life, 10, 10) # Changed from "Score" to "Life"
show_score("Stones Destroyed", stones_destroyed, 150, 10)
pygame.display.update()
def main():
global welcome_message_visible
global welcome_message_timer
global fireballs
global stones_destroyed
global life # Use global variable life
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: # left mouse button
fireballs.append(Fireball(cupter.x + cup_width, cupter.y + cup_height // 2)) # Create a fireball
keys_pressed = pygame.key.get_pressed()
movement(keys_pressed, cupter)
for stone in stones:
if cupter.colliderect(pygame.Rect(stone.x, stone.y, stone.width, stone.height)):
if not stone.collided:
life += 1 # Increment life when a stone collides with the cupter
stone.collided = True
win_component(cupter)
# Check if it's time to hide the welcome message
if welcome_message_visible:
current_time = pygame.time.get_ticks()
if current_time - welcome_message_timer >= 3000: # 3000 milliseconds = 3 seconds
welcome_message_visible = False
if welcome_message_visible:
text("Welcome to my game", 100, 200, 200)
back_button.draw(screen, (0, 0, 0)) # Draw the back button continuously
# Move fireballs
for fireball in fireballs:
fireball.move()
# Remove fireballs that move off the 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]
# Check for collisions between fireballs and stones
for fireball in fireballs:
for stone in stones:
if pygame.Rect(fireball.x - fireball.radius, fireball.y - fireball.radius,
fireball.radius * 2, fireball.radius * 2).colliderect(pygame.Rect(stone.x, stone.y, stone.width, stone.height)):
stones.remove(stone)
stones_destroyed += 1
fireballs.remove(fireball) # Remove the fireball
break
# Spawn new stones periodically, more rapidly and sometimes multiple stones
if len(stones) < 10 and random.randint(1, 20) == 1: # Adjusted for higher frequency
num_stones = random.randint(1, 3) # Spawn 1 to 3 stones at once
for _ in range(num_stones):
stones.append(Stone(screen.get_width(), random.randint(0, screen.get_height() - 30)))
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
intro_main()