- Joined
- Jul 28, 2024
- Messages
- 3
- Reaction score
- 0
import pygame
import random
from pygame.locals import *
import time
pygame.init()
# Screen dimensions
screen_width = 800
screen_height = 600
# Initialize pygame screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Simple Game')
# Game variables for the first game
player_width = 50
player_height = 50
player_pos2 = [screen_width - 100, 270]
player_pos1 = [screen_width - 100, 430]
player_speed = 5
enemy1_width = 50
enemy1_height = 50
enemy1_speed = 3
enemies1 = []
score1 = 0
game_over1 = False
win1 = False
game_active = True
# Game variables for the second game
player2_width = 50
normal_height = 50
crouch_height = 25
player_pos = [0, screen_height - normal_height]
player_speed = 5
player_current_height = normal_height
enemy2_width = 40
enemy2_height = 25
initial_enemy2_speed = 3
enemy2_speed = initial_enemy2_speed
enemies2 = []
score2 = 0
game_over2 = False
win2 = False
start_ticks = pygame.time.get_ticks()
boom = 1000
start2_ticks = pygame.time.get_ticks()
# Function to spawn enemy for the first game
def spawn_enemy_game1():
x = 0
y = random.randint(60, screen_height - 110)
enemies1.append(pygame.Rect(x, y, enemy1_width, enemy1_height))
# Function to spawn enemy for the second game
def spawn_enemy_game2():
y = random.choice([screen_height - 50, screen_height - 25])
enemy = pygame.Rect(screen_width, y, enemy2_width, enemy2_height)
enemies2.append(enemy)
# Function to check collision with player in the first game
def check_collision_game1(player_rect1, player_rect2):
for enemy in enemies1:
if player_rect1.colliderect(enemy) or player_rect2.colliderect(enemy):
return True
return False
# Function to check collision with player in the second game
def check_collision_game2(player_rect):
for enemy in enemies2:
if player_rect.colliderect(enemy):
return True
return False
# Function to restart game for the first game
def restart_game1():
global player_pos1, player_pos2, enemies1, game_over1, win1, score1
player_pos1 = [screen_width - 100, 430]
player_pos2 = [screen_width - 100, 270]
enemies1.clear()
game_over1 = False
win1 = False
score1 = 0
# Function to restart game for the second game
def restart_game2():
global player_pos, enemies2, game_over2, win2, enemy2_speed, start_ticks, player_current_height, score2, player2_width, boom, start2_ticks
player_current_height = normal_height
player_pos = [0, screen_height - normal_height]
enemies2.clear()
game_over2 = False
win2 = False
enemy2_speed = initial_enemy2_speed
start_ticks = pygame.time.get_ticks()
start2_ticks = pygame.time.get_ticks()
score2 = 0
player2_width = 50
boom = 1000
# Menu function
def show_menu():
screen.fill((0, 0, 0))
menu_font = pygame.font.SysFont(None, 60)
menu_text1 = menu_font.render('Press 1 to open first game', True, (255, 255, 255))
menu_text2 = menu_font.render('Press 2 to open the other game', True, (255, 255, 255))
screen.blit(menu_text1, (screen_width // 2 - menu_text1.get_width() // 2, screen_height // 2 - 30))
screen.blit(menu_text2, (screen_width // 2 - menu_text2.get_width() // 2, screen_height // 2 + 30))
pygame.display.flip()
# Function to show pause menu
def show_pause_menu():
screen.fill((0, 0, 0))
pause_font = pygame.font.SysFont(None, 60)
pause_text = pause_font.render('Press SPACE to Continue', True, (255, 255, 255))
screen.blit(pause_text, (screen_width // 2 - pause_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
# Function to show game over menu
def show_game_over_menu():
screen.fill((0, 0, 0))
game_over_font = pygame.font.SysFont(None, 60)
game_over_text = game_over_font.render("Game Over!", True, (255, 0, 0))
screen.blit(game_over_text, (screen_width // 2 - game_over_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
# Function to show win menu
def show_win_menu():
global game_active
screen.fill((0, 0, 0))
win_font = pygame.font.SysFont(None, 60)
win_text = win_font.render("You Win!", True, (0, 255, 0))
screen.blit(win_text, (screen_width // 2 - win_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
game_active = False
# Game loop
clock = pygame.time.Clock()
running = True
in_menu = True
game1_active = False
game2_active = False
paused = False
show_pause = False
while running:
dt = clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if win2:
if event.key == pygame.K_r:
paused = False
win2 = False # Reset win2 to avoid re-entering this block immediately
restart_game2()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
if not in_menu:
if not paused:
paused = True
show_pause = True
else:
paused = False
show_pause = False
elif event.key == K_SPACE and show_pause:
paused = False
show_pause = False
elif event.key == K_m:
if not in_menu:
in_menu = True
game1_active = False
game2_active = False
elif event.key == K_1:
if in_menu:
in_menu = False
game1_active = True
game2_active = False
restart_game1()
elif event.key == K_2:
if in_menu:
in_menu = False
game1_active = False
game2_active = True
restart_game2()
elif event.key == K_r:
if game1_active and game_over1:
restart_game1()
if game2_active and game_over2:
restart_game2()
elif event.key == K_DOWN:
if game2_active:
player_current_height = crouch_height
player_pos[1] = screen_height - crouch_height
elif event.key == K_UP:
if game2_active:
player_current_height = crouch_height
player_pos[1] = 550
current_time = pygame.time.get_ticks()
# Calculate the elapsed time
elapsed_time = (current_time - start2_ticks) / 1000 # Convert milliseconds to seconds
# Adjust boom and player width based on elapsed time
if 10 <= score2 < 20:
boom = 500
player2_width = 25
if 20 <= score2 < 30:
boom = 400
player2_width = 12.5
if 30 <= elapsed_time <= 40:
boom = 300
if 40 <= score2:
boom = 250
if in_menu:
show_menu()
elif show_pause:
show_pause_menu()
elif game1_active:
if not paused:
keys = pygame.key.get_pressed()
if keys[K_DOWN] and player_pos2[1] + player_height < screen_height:
player_pos2[1] += player_speed
player_pos1[1] += player_speed
if keys[K_UP] and player_pos2[1] > 0:
player_pos2[1] -= player_speed
player_pos1[1] -= player_speed
player_pos1[1] = max(0, min(player_pos1[1], screen_height - player_height))
player_pos2[1] = max(0, min(player_pos2[1], 390))
player_rect1 = pygame.Rect(player_pos1[0], player_pos1[1], player_width, player_height)
player_rect2 = pygame.Rect(player_pos2[0], player_pos2[1], player_width, player_height)
middle_top = min(player_pos1[1], player_pos2[1]) + player_height
middle_bottom = max(player_pos1[1], player_pos2[1])
middle_rect = pygame.Rect(player_pos1[0], middle_top, 60, middle_bottom - middle_top)
if random.randint(1, 100) == 1:
spawn_enemy_game1()
for enemy in enemies1[:]:
enemy.x += enemy1_speed
if enemy.x > screen_width:
enemies1.remove(enemy)
if check_collision_game1(player_rect1, player_rect2):
game_over1 = True
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), player_rect1)
pygame.draw.rect(screen, (255, 255, 255), player_rect2)
for enemy in enemies1[:]:
pygame.draw.rect(screen, (255, 0, 0), enemy)
if enemy.colliderect(middle_rect):
score1 += 1
enemies1.remove(enemy)
score_font = pygame.font.SysFont(None, 36)
score_text = score_font.render(f"Score: {score1}", True, (255, 255, 255))
screen.blit(score_text, (10, 10))
if game_over1:
show_game_over_menu()
elif game2_active:
if not paused:
keys = pygame.key.get_pressed()
if keys[K_UP]:
player_current_height = crouch_height
player_pos[1] = 550
elif keys[K_DOWN]:
player_current_height = crouch_height
player_pos[1] = screen_height - crouch_height
else:
player_current_height = 50
player_pos[1] = 550
if pygame.time.get_ticks() - start_ticks > boom:
spawn_enemy_game2()
start_ticks = pygame.time.get_ticks()
for enemy in enemies2[:]:
enemy.x -= enemy2_speed
if enemy.x < 0:
score2 += 1
enemies2.remove(enemy)
if score2 >= 1:
win2 = True
if check_collision_game2(pygame.Rect(player_pos[0], player_pos[1], player2_width, player_current_height)):
game_over2 = True
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255),
pygame.Rect(player_pos[0], player_pos[1], player2_width, player_current_height))
for enemy in enemies2[:]:
pygame.draw.rect(screen, (255, 0, 0), enemy)
score_font = pygame.font.SysFont(None, 36)
score_text = score_font.render(f"Score: {score2}", True, (255, 255, 255))
screen.blit(score_text, (10, 10))
if game_over2:
show_game_over_menu()
pygame.display.flip()
can anyone help me fix the bug when you win in game 2 it doesnt stop and after that is just chaos
pygame.quit()
import random
from pygame.locals import *
import time
pygame.init()
# Screen dimensions
screen_width = 800
screen_height = 600
# Initialize pygame screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Simple Game')
# Game variables for the first game
player_width = 50
player_height = 50
player_pos2 = [screen_width - 100, 270]
player_pos1 = [screen_width - 100, 430]
player_speed = 5
enemy1_width = 50
enemy1_height = 50
enemy1_speed = 3
enemies1 = []
score1 = 0
game_over1 = False
win1 = False
game_active = True
# Game variables for the second game
player2_width = 50
normal_height = 50
crouch_height = 25
player_pos = [0, screen_height - normal_height]
player_speed = 5
player_current_height = normal_height
enemy2_width = 40
enemy2_height = 25
initial_enemy2_speed = 3
enemy2_speed = initial_enemy2_speed
enemies2 = []
score2 = 0
game_over2 = False
win2 = False
start_ticks = pygame.time.get_ticks()
boom = 1000
start2_ticks = pygame.time.get_ticks()
# Function to spawn enemy for the first game
def spawn_enemy_game1():
x = 0
y = random.randint(60, screen_height - 110)
enemies1.append(pygame.Rect(x, y, enemy1_width, enemy1_height))
# Function to spawn enemy for the second game
def spawn_enemy_game2():
y = random.choice([screen_height - 50, screen_height - 25])
enemy = pygame.Rect(screen_width, y, enemy2_width, enemy2_height)
enemies2.append(enemy)
# Function to check collision with player in the first game
def check_collision_game1(player_rect1, player_rect2):
for enemy in enemies1:
if player_rect1.colliderect(enemy) or player_rect2.colliderect(enemy):
return True
return False
# Function to check collision with player in the second game
def check_collision_game2(player_rect):
for enemy in enemies2:
if player_rect.colliderect(enemy):
return True
return False
# Function to restart game for the first game
def restart_game1():
global player_pos1, player_pos2, enemies1, game_over1, win1, score1
player_pos1 = [screen_width - 100, 430]
player_pos2 = [screen_width - 100, 270]
enemies1.clear()
game_over1 = False
win1 = False
score1 = 0
# Function to restart game for the second game
def restart_game2():
global player_pos, enemies2, game_over2, win2, enemy2_speed, start_ticks, player_current_height, score2, player2_width, boom, start2_ticks
player_current_height = normal_height
player_pos = [0, screen_height - normal_height]
enemies2.clear()
game_over2 = False
win2 = False
enemy2_speed = initial_enemy2_speed
start_ticks = pygame.time.get_ticks()
start2_ticks = pygame.time.get_ticks()
score2 = 0
player2_width = 50
boom = 1000
# Menu function
def show_menu():
screen.fill((0, 0, 0))
menu_font = pygame.font.SysFont(None, 60)
menu_text1 = menu_font.render('Press 1 to open first game', True, (255, 255, 255))
menu_text2 = menu_font.render('Press 2 to open the other game', True, (255, 255, 255))
screen.blit(menu_text1, (screen_width // 2 - menu_text1.get_width() // 2, screen_height // 2 - 30))
screen.blit(menu_text2, (screen_width // 2 - menu_text2.get_width() // 2, screen_height // 2 + 30))
pygame.display.flip()
# Function to show pause menu
def show_pause_menu():
screen.fill((0, 0, 0))
pause_font = pygame.font.SysFont(None, 60)
pause_text = pause_font.render('Press SPACE to Continue', True, (255, 255, 255))
screen.blit(pause_text, (screen_width // 2 - pause_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
# Function to show game over menu
def show_game_over_menu():
screen.fill((0, 0, 0))
game_over_font = pygame.font.SysFont(None, 60)
game_over_text = game_over_font.render("Game Over!", True, (255, 0, 0))
screen.blit(game_over_text, (screen_width // 2 - game_over_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
# Function to show win menu
def show_win_menu():
global game_active
screen.fill((0, 0, 0))
win_font = pygame.font.SysFont(None, 60)
win_text = win_font.render("You Win!", True, (0, 255, 0))
screen.blit(win_text, (screen_width // 2 - win_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
game_active = False
# Game loop
clock = pygame.time.Clock()
running = True
in_menu = True
game1_active = False
game2_active = False
paused = False
show_pause = False
while running:
dt = clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if win2:
if event.key == pygame.K_r:
paused = False
win2 = False # Reset win2 to avoid re-entering this block immediately
restart_game2()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
if not in_menu:
if not paused:
paused = True
show_pause = True
else:
paused = False
show_pause = False
elif event.key == K_SPACE and show_pause:
paused = False
show_pause = False
elif event.key == K_m:
if not in_menu:
in_menu = True
game1_active = False
game2_active = False
elif event.key == K_1:
if in_menu:
in_menu = False
game1_active = True
game2_active = False
restart_game1()
elif event.key == K_2:
if in_menu:
in_menu = False
game1_active = False
game2_active = True
restart_game2()
elif event.key == K_r:
if game1_active and game_over1:
restart_game1()
if game2_active and game_over2:
restart_game2()
elif event.key == K_DOWN:
if game2_active:
player_current_height = crouch_height
player_pos[1] = screen_height - crouch_height
elif event.key == K_UP:
if game2_active:
player_current_height = crouch_height
player_pos[1] = 550
current_time = pygame.time.get_ticks()
# Calculate the elapsed time
elapsed_time = (current_time - start2_ticks) / 1000 # Convert milliseconds to seconds
# Adjust boom and player width based on elapsed time
if 10 <= score2 < 20:
boom = 500
player2_width = 25
if 20 <= score2 < 30:
boom = 400
player2_width = 12.5
if 30 <= elapsed_time <= 40:
boom = 300
if 40 <= score2:
boom = 250
if in_menu:
show_menu()
elif show_pause:
show_pause_menu()
elif game1_active:
if not paused:
keys = pygame.key.get_pressed()
if keys[K_DOWN] and player_pos2[1] + player_height < screen_height:
player_pos2[1] += player_speed
player_pos1[1] += player_speed
if keys[K_UP] and player_pos2[1] > 0:
player_pos2[1] -= player_speed
player_pos1[1] -= player_speed
player_pos1[1] = max(0, min(player_pos1[1], screen_height - player_height))
player_pos2[1] = max(0, min(player_pos2[1], 390))
player_rect1 = pygame.Rect(player_pos1[0], player_pos1[1], player_width, player_height)
player_rect2 = pygame.Rect(player_pos2[0], player_pos2[1], player_width, player_height)
middle_top = min(player_pos1[1], player_pos2[1]) + player_height
middle_bottom = max(player_pos1[1], player_pos2[1])
middle_rect = pygame.Rect(player_pos1[0], middle_top, 60, middle_bottom - middle_top)
if random.randint(1, 100) == 1:
spawn_enemy_game1()
for enemy in enemies1[:]:
enemy.x += enemy1_speed
if enemy.x > screen_width:
enemies1.remove(enemy)
if check_collision_game1(player_rect1, player_rect2):
game_over1 = True
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), player_rect1)
pygame.draw.rect(screen, (255, 255, 255), player_rect2)
for enemy in enemies1[:]:
pygame.draw.rect(screen, (255, 0, 0), enemy)
if enemy.colliderect(middle_rect):
score1 += 1
enemies1.remove(enemy)
score_font = pygame.font.SysFont(None, 36)
score_text = score_font.render(f"Score: {score1}", True, (255, 255, 255))
screen.blit(score_text, (10, 10))
if game_over1:
show_game_over_menu()
elif game2_active:
if not paused:
keys = pygame.key.get_pressed()
if keys[K_UP]:
player_current_height = crouch_height
player_pos[1] = 550
elif keys[K_DOWN]:
player_current_height = crouch_height
player_pos[1] = screen_height - crouch_height
else:
player_current_height = 50
player_pos[1] = 550
if pygame.time.get_ticks() - start_ticks > boom:
spawn_enemy_game2()
start_ticks = pygame.time.get_ticks()
for enemy in enemies2[:]:
enemy.x -= enemy2_speed
if enemy.x < 0:
score2 += 1
enemies2.remove(enemy)
if score2 >= 1:
win2 = True
if check_collision_game2(pygame.Rect(player_pos[0], player_pos[1], player2_width, player_current_height)):
game_over2 = True
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255),
pygame.Rect(player_pos[0], player_pos[1], player2_width, player_current_height))
for enemy in enemies2[:]:
pygame.draw.rect(screen, (255, 0, 0), enemy)
score_font = pygame.font.SysFont(None, 36)
score_text = score_font.render(f"Score: {score2}", True, (255, 255, 255))
screen.blit(score_text, (10, 10))
if game_over2:
show_game_over_menu()
pygame.display.flip()
can anyone help me fix the bug when you win in game 2 it doesnt stop and after that is just chaos
pygame.quit()