R50 4fd6d9d9de7b34638f63f16582928bb5
Дизайн, Python, IT , Арт

"Пианино"

Добавлено 18 дек 2023 в 23:37
"Пианино", в которой вы должны нажимать на клавиши D, F, J и K, чтобы ловить белые прямоугольники, которые падают сверху. Каждый прямоугольник соответствует одному из четырех столбцов на экране, а каждая клавиша соответствует одному из четырех звуков. Вы должны нажимать на клавиши в тот момент, когда прямоугольник пересекает желтую зону внизу экрана. Если вы попадаете в прямоугольник, он исчезает, вы слышите звук и получаете одно очко. Если вы промахиваетесь, прямоугольник становится красным, вы теряете одно очко и игра продолжается. Игра заканчивается, когда вы нажимаете на крестик в правом верхнем углу окна.

import pygame
import sys

# Initialize Pygame
pygame.init()

# Set up the screen
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Your Game Title")

# Define colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

# Other initializations
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
score = 0 # Initialize the score

# Define the class WhiteRect
class WhiteRect(pygame.sprite.Sprite):
def __init__(self, group):
super().__init__()
self.width = 50
self.height = 20
self.image = pygame.Surface((self.width, self.height))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.group = group
self.column = group.column
self.rect.x = self.column * (SCREEN_WIDTH // 4) + (SCREEN_WIDTH // 8) - (self.image.get_width() // 2)
self.rect.y = 0 - self.image.get_height()
self.speed = 5
group.add(self)

def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.y > SCREEN_HEIGHT:
self.kill()
WhiteRect(self.group)

# Define the class YellowZone
class YellowZone(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

# Define the class RedLine
class RedLine(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

# Load the sound files
sound_d = pygame.mixer.Sound("sound_d.wav")
sound_f = pygame.mixer.Sound("sound_f.wav")
sound_j = pygame.mixer.Sound("sound_j.wav")
sound_correct = pygame.mixer.Sound("sound_correct.wav")
sound_wrong = pygame.mixer.Sound("sound_wrong.wav")

# Main game loop
running = True
red_line = RedLine(100, 400, 200, 20)
yellow_zone = YellowZone(300, 300, 200, 20)
all_sprites = pygame.sprite.Group()
all_sprites.add(red_line)
all_sprites.add(yellow_zone)

# Game initialization
for i in range(4):
WhiteRect(all_sprites)

# Drawing code
screen.fill(BLACK)

for i in range(1, 5):
pygame.draw.line(screen, GREEN, (i * SCREEN_WIDTH // 4, 0), (i * SCREEN_WIDTH // 4, SCREEN_HEIGHT), 2)
text = font.render(f"{i}", True, WHITE)
screen.blit(text, (i * SCREEN_WIDTH // 4 - 10, SCREEN_HEIGHT + 30))

all_sprites.update()
all_sprites.draw(screen)

for white_rect in all_sprites:
if white_rect.rect.y + white_rect.image.get_height() >= red_line.y:
white_rect.image.fill(RED)
else:
white_rect.image.fill(WHITE)

pygame.display.flip()

# Event handling code
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Check which key is pressed
if event.key == pygame.K_d:
# Play the sound for D key
sound_d.play()
# Check collision with yellow_zone and update score
for white_rect in all_sprites:
if white_rect.column == 1 and pygame.sprite.collide_rect(white_rect, yellow_zone):
# Play the sound for correct match
sound_correct.play()
# Increase the score by one
score += 1
# Remove the white_rect from the group
white_rect.kill()
# Create a new white_rect in the same column
WhiteRect(all_sprites)
elif white_rect.column == 1 and white_rect.rect.y + white_rect.image.get_height() >= red_line.y:
# Play the sound for wrong match
sound_wrong.play()
# Decrease the score by one
score -= 1
elif event.key == pygame.K_f:
# Similar operations for F key
sound_f.play()
for white_rect in all_sprites:
if white_rect.column == 2 and pygame.sprite.collide_rect(white_rect, yellow_zone):
sound_correct.play()
score += 1
white_rect.kill()
WhiteRect(all_sprites)
elif white_rect.column == 2 and white_rect.rect.y + white_rect.image.get_height() >= red_line.y:
sound_wrong.play()
score -= 1
elif event.key == pygame.K_j:
# Similar operations for J key
sound_j.play()
for white_rect in all_sprites:
if white_rect.column == 3 and pygame.sprite.collide_rect(white_rect, yellow_zone):
sound_correct.play()
score += 1
white_rect.kill()
WhiteRect(all_sprites)
elif white_rect.column == 3 and white_rect.rect.y + white_rect.image.get_height() >= red_line.y:
sound_wrong.play()
score -= 1

# Update game state
all_sprites.update()

# Draw on the screen
screen.fill(BLACK)

for i in range(1, 5):
pygame.draw.line(screen, GREEN, (i * SCREEN_WIDTH // 4, 0), (i * SCREEN_WIDTH // 4, SCREEN_HEIGHT), 2)
text = font.render(f"{i}", True, WHITE)
screen.blit(text, (i * SCREEN_WIDTH // 4 - 10, SCREEN_HEIGHT + 30))

all_sprites.draw(screen)

for white_rect in all_sprites:
if white_rect.rect.y + white_rect.image.get_height() >= red_line.y:
white_rect.image.fill(RED)
else:
white_rect.image.fill(WHITE)

# Draw the score on the screen
text = font.render(f"Score: {score}", True, WHITE)
screen.blit(text

1523e033e7