Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import sqlite3
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.button import Button
- from kivy.uix.label import Label
- from kivy.uix.textinput import TextInput
- from kivy.uix.popup import Popup
- from kivy.uix.screenmanager import ScreenManager, Screen
- from kivy.clock import Clock
- from kivy.core.window import Window
- # color
- Window.clearcolor = (0.9, 0.9, 0.9, 1)
- # Topic
- class TopicSelectionScreen(Screen):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- layout = BoxLayout(orientation='vertical')
- developer_label = Label(text='Developed by: Mohamed Hussein (Mando)', size_hint_y=None, height=40)
- layout.add_widget(developer_label)
- self.topic_input = TextInput(hint_text='Add a custom topic', size_hint_y=None, height=40)
- layout.add_widget(self.topic_input)
- self.topic_buttons = {}
- for topic in ["Foods", "Animals"]:
- button = Button(text=topic, on_press=self.select_topic)
- layout.add_widget(button)
- self.topic_buttons[topic] = button
- add_topic_button = Button(text='Add Topic', on_press=self.add_custom_topic)
- layout.add_widget(add_topic_button)
- self.add_widget(layout)
- def select_topic(self, instance):
- app = App.get_running_app()
- app.selected_topic = instance.text
- app.root.current = 'player_selection'
- def add_custom_topic(self, instance):
- custom_topic = self.topic_input.text.strip()
- if custom_topic and custom_topic not in self.topic_buttons:
- self.topic_buttons[custom_topic] = Button(text=custom_topic, on_press=self.select_topic)
- self.add_widget(self.topic_buttons[custom_topic])
- self.topic_input.text = ''
- else:
- popup_content = BoxLayout(orientation='vertical')
- popup_content.add_widget(Label(text='Topic already exists or is empty.'))
- popup = Popup(title='Error', content=popup_content, size_hint=(0.5, 0.5))
- popup.open()
- class PlayerSelectionScreen(Screen):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.players = []
- layout = BoxLayout(orientation='vertical')
- self.player_input = TextInput(hint_text='Enter player name')
- layout.add_widget(self.player_input)
- self.add_player_button = Button(text='Add Player', on_press=self.add_player)
- layout.add_widget(self.add_player_button)
- self.start_game_button = Button(text='Start Game', on_press=self.start_game, disabled=True)
- layout.add_widget(self.start_game_button)
- self.players_list = BoxLayout(orientation='vertical')
- layout.add_widget(self.players_list)
- self.add_widget(layout)
- def add_player(self, instance):
- player_name = self.player_input.text
- if player_name:
- self.players.append(player_name)
- player_label = Label(text=f"Player {len(self.players)}: {player_name}")
- self.players_list.add_widget(player_label)
- self.player_input.text = ''
- app = App.get_running_app()
- app.players = self.players
- self.start_game_button.disabled = len(self.players) < 3
- def start_game(self, instance):
- if len(self.players) < 3:
- popup_content = BoxLayout(orientation='vertical')
- popup_content.add_widget(Label(text='You need at least 3 players to start the game.'))
- popup = Popup(title='Error', content=popup_content, size_hint=(0.5, 0.5))
- popup.open()
- else:
- app = App.get_running_app()
- app.root.current = 'game_screen'
- app.start_game()
- class GameScreen(Screen):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.layout = BoxLayout(orientation='vertical')
- self.status_label = Label(text='')
- # Timer label
- self.timer_label = Label(text='Time left: 30', size_hint_y=None, height=40)
- self.layout.add_widget(self.timer_label)
- # Time limit inputs
- self.question_time_input = TextInput(hint_text='Question Time (seconds)', size_hint_y=None, height=40)
- self.vote_time_input = TextInput(hint_text='Vote Time (seconds)', size_hint_y=None, height=40)
- self.layout.add_widget(self.question_time_input)
- self.layout.add_widget(self.vote_time_input)
- self.layout.add_widget(self.status_label)
- self.add_widget(self.layout)
- def update_status(self, text):
- self.status_label.text = text
- def start_timer(self, phase):
- if phase == 'question':
- self.time_left = int(self.question_time_input.text) if self.question_time_input.text else 30
- elif phase == 'vote':
- self.time_left = int(self.vote_time_input.text) if self.vote_time_input.text else 30
- Clock.schedule_interval(self.update_timer, 1)
- def update_timer(self, dt):
- if self.time_left > 0:
- self.time_left -= 1
- self.timer_label.text = f'Time left: {self.time_left}'
- else:
- Clock.unschedule(self.update_timer)
- if self.current_phase == 'question':
- self.vote_spy()
- else:
- self.calculate_results()
- # statistics screen
- class StatsScreen(Screen):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- layout = BoxLayout(orientation='vertical')
- self.stats_labels = {}
- title_label = Label(text='Player Statistics', size_hint_y=None, height=40)
- layout.add_widget(title_label)
- self.add_widget(layout)
- def update_stats(self, stats):
- for player, data in stats.items():
- if player not in self.stats_labels:
- self.stats_labels[player] = Label()
- self.add_widget(self.stats_labels[player])
- self.stats_labels[player].text = f"{player}: Rounds Played: {data['rounds']}, Caught as Spy: {data['caught']}"
- # application
- class SpyGameApp(App):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.topics = {
- "Foods": ["Apple", "Banana", "Orange"],
- "Animals": ["Lion", "Tiger", "Elephant"]
- }
- self.selected_topic = None
- self.players = []
- self.points = {}
- self.player_stats = {}
- self.db = sqlite3.connect('game_data.db')
- self.create_table()
- def build(self):
- sm = ScreenManager()
- sm.add_widget(TopicSelectionScreen(name='topic_selection'))
- sm.add_widget(PlayerSelectionScreen(name='player_selection'))
- sm.add_widget(GameScreen(name='game_screen'))
- sm.add_widget(StatsScreen(name='stats_screen'))
- return sm
- def create_table(self):
- with self.db:
- self.db.execute('''CREATE TABLE IF NOT EXISTS scores (
- player TEXT PRIMARY KEY,
- points INTEGER,
- game_date TEXT
- )''')
- def save_scores(self):
- with self.db:
- for player, points in self.points.items():
- self.db.execute('''INSERT OR REPLACE INTO scores (player, points)
- VALUES (?, ?)''', (player, points))
- def start_game(self):
- self.initialize_stats()
- topic = self.selected_topic
- words = random.sample(self.topics[topic], len(self.players))
- self.spy_index = random.randint(0, len(self.players) - 1)
- self.player_words = {}
- for i, player in enumerate(self.players):
- if i == self.spy_index:
- popup_content = BoxLayout(orientation='vertical')
- popup_content.add_widget(Label(text=f"{player}, you are the spy!"))
- popup = Popup(title='Your Role', content=popup_content, size_hint=(0.5, 0.5))
- self.player_words[player] = None
- else:
- word = words[i]
- popup_content = BoxLayout(orientation='vertical')
- popup_content.add_widget(Label(text=f"{player}, your word is: {word}"))
- popup = Popup(title='Your Role', content=popup_content, size_hint=(0.5, 0.5))
- self.player_words[player] = word
- popup.open()
- game_screen = self.root.get_screen('game_screen')
- game_screen.update_status(f"Topic: {topic}")
- game_screen.start_timer('question')
- self.ask_questions()
- def initialize_stats(self):
- for player in self.players:
- if player not in self.player_stats:
- self.player_stats[player] = {'rounds': 0, 'caught': 0}
- def update_player_stats(self, is_spy_caught):
- for player in self.players:
- self.player_stats[player]['rounds'] += 1
- if is_spy_caught and player == self.players[self.spy_index]:
- self.player_stats[player]['caught'] += 1
- def calculate_results(self):
- self.spy_votes = sum(1 for vote in self.votes.values() if vote == self.players[self.spy_index])
- is_spy_caught = self.spy_votes > len(self.players) // 2
- self.update_player_stats(is_spy_caught)
- self.save_scores()
- self.root.current = 'stats_screen'
- stats_screen = self.root.get_screen('stats_screen')
- stats_screen.update_stats(self.player_stats)
- if __name__ == '__main__':
- SpyGameApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement