Game / app.py
Somnath3570's picture
create app.py
4efcb98 verified
raw
history blame contribute delete
No virus
2.91 kB
import streamlit as st
import pygame
import random
# Initialize pygame
pygame.init()
# Set up the window dimensions
window_width = 800
window_height = 600
screen = pygame.Surface((window_width, window_height))
# Set up the title of the window
pygame.display.set_caption('Battle Royale')
# Define some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Define the player class
class Player:
def __init__(self):
self.x = window_width // 2
self.y = window_height // 2
self.speed = 5
self.health = 100
def move(self, direction):
if direction == 'up':
self.y -= self.speed
elif direction == 'down':
self.y += self.speed
elif direction == 'left':
self.x -= self.speed
elif direction == 'right':
self.x += self.speed
def update(self):
# Update the player's position based on their speed and direction
pass
# Define the enemy class
class Enemy:
def __init__(self):
self.x = random.randint(0, window_width)
self.y = random.randint(0, window_height)
self.speed = 2
self.health = 50
def update(self):
# Update the enemy's position based on their speed and a random direction
pass
# Create a list to store the players and enemies
players = [Player()]
enemies = [Enemy()]
# Streamlit interface
st.title('Battle Royale Game')
# Game loop logic in Streamlit
while True:
# Handle Streamlit events (not actual Pygame events)
for event in st._loop._get_events_for_streamlit():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
for player in players:
player.move('up')
elif event.key == pygame.K_DOWN:
for player in players:
player.move('down')
elif event.key == pygame.K_LEFT:
for player in players:
player.move('left')
elif event.key == pygame.K_RIGHT:
for player in players:
player.move('right')
# Update the players and enemies (Streamlit doesn't need this part)
for player in players:
player.update()
for enemy in enemies:
enemy.update()
# Draw everything onto a Streamlit canvas
screen.fill(WHITE)
for player in players:
pygame.draw.rect(screen, GREEN, (player.x, player.y, 20, 20))
for enemy in enemies:
pygame.draw.rect(screen, RED, (enemy.x, enemy.y, 20, 20))
# Convert Pygame surface to image and display in Streamlit
image_data = pygame.image.tostring(screen, 'RGB')
st.image(image_data, caption='Battle Royale Game', use_column_width=True)
# Pause to control the frame rate (not necessary in Streamlit)
pygame.time.delay(100)
# Quit Pygame (not necessary in Streamlit)
pygame.quit()