Skip to content

Instantly share code, notes, and snippets.

@T31337
Created January 31, 2025 21:06
Show Gist options
  • Save T31337/0c753bb189d969fdb00d6be1a6f3fdc1 to your computer and use it in GitHub Desktop.
Save T31337/0c753bb189d969fdb00d6be1a6f3fdc1 to your computer and use it in GitHub Desktop.
Kivy Pyg Game
import random
import sys
import kivy
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.popup import Popup
class PigGame:
MAX_SCORE = 100
scores = [0, 0]
@staticmethod
def roll():
return random.randint(1, 6)
def computer_turn(self):
#self.turn_label.text = "Computer is rolling..."
roll_again = True
points = 0
while roll_again:
dice_value = self.roll()
if dice_value == 1:
points = 0
break
else:
points += dice_value
roll_again = random.choice([True, False])
self.scores[1] += points
#self.turn_label.text = "Your Turn, Roll The Dice!"
def player_turn(self, roll_again):
points = 0
if roll_again:
dice_value = self.roll()
if dice_value == 1:
points = 0
else:
points += dice_value
return points
class PigGameApp(App):
def build(self):
self.game = PigGame()
self.layout = BoxLayout(orientation='vertical')
self.score_label = Label(text=f"Player Score: {self.game.scores[0]} | Computer Score: {self.game.scores[1]}")
self.layout.add_widget(self.score_label)
self.turn_label = Label(text="It's your turn! Roll the dice.")
self.layout.add_widget(self.turn_label)
self.roll_button = Button(text='Roll Dice', on_press=self.roll_dice)
self.layout.add_widget(self.roll_button)
self.stop_button = Button(text='Stop Rolling', on_press=self.stop_rolling)
self.layout.add_widget(self.stop_button)
self.result_label = Label(text="")
self.layout.add_widget(self.result_label)
return self.layout
def roll_dice(self, instance):
points = self.game.player_turn(True)
if points == 0:
self.result_label.text = "You rolled a 1! Turn over."
self.turn_label.text = "Computer is rolling..."
self.game.scores[0] += points
#self.turn_label.text = "Computer is rolling..."
self.computer_turn()
else:
self.result_label.text = f"You rolled: {points} | Your total points: {self.game.scores[0] + points}"
self.game.scores[0] += points
self.update_scores()
#self.turn_label.text = "Computer is rolling..."
def stop_rolling(self, instance):
self.turn_label.text = "Computer is rolling..."
self.computer_turn()
self.update_scores()
self.check_winner()
def computer_turn(self):
self.game.computer_turn()
def update_scores(self):
if self.game.player_turn:
self.turn_label.text = "Roll the dice!"
else:
self.turn_label.text = "Computer is rolling..."
self.score_label.text = f"Player Score: {self.game.scores[0]} | Computer Score: {self.game.scores[1]}"
def check_winner(self):
if max(self.game.scores) >= self.game.MAX_SCORE:
if self.game.scores[0] > self.game.scores[1]:
self.show_popup("Player Wins!", f"Player Score: {self.game.scores[0]} \nComputer Score: {self.game.scores[1]}")
else:
self.show_popup("Computer Wins!", f"Computer Score: {self.game.scores[1]} \nPlayer Score: {self.game.scores[0]}")
self.reset_game()
def show_popup(self, title, message):
popup = Popup(title=title, content=Label(text=message), size_hint=(0.6, 0.4))
popup.open()
def reset_game(self):
self.game.scores = [0, 0]
self.update_scores()
self.result_label.text = ""
self.turn_label.text = "It's your turn! Roll the dice."
if __name__ == '__main__':
PigGameApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment