Skip to content

Instantly share code, notes, and snippets.

@TERNION-1121
Last active March 1, 2024 17:09
Show Gist options
  • Save TERNION-1121/8645bcc59fefc7098f39d4dd18e255b7 to your computer and use it in GitHub Desktop.
Save TERNION-1121/8645bcc59fefc7098f39d4dd18e255b7 to your computer and use it in GitHub Desktop.
A Python script to simulate Rocks, Papers and Scissors using pygame.
from random import random
import pygame
from pygame import Vector2
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
screen_size = (500, 500)
pygame.init()
screen = pygame.display.set_mode(screen_size)
screen.fill(BLACK)
pygame.display.set_caption("RPS Warfare")
pygame.display.flip()
class Mover():
COLOR = WHITE
objects = list()
def __init__(self, pos = None) -> None:
if not pos:
self.position = Mover.rand_pos(*screen_size)
else:
self.position = Vector2(pos)
def show(self) -> None:
pygame.draw.circle(screen, self.COLOR, self.position, 5)
@staticmethod
def rand_pos(screenWidth: int, screenHeight: int) -> Vector2:
return Vector2(random() * screenWidth, random() * screenHeight)
@classmethod
def show_all(cls) -> None:
for obj in cls.objects:
obj.show()
@classmethod
def generate_objects(cls, n: int) -> None:
for _ in range(n):
cls()
@classmethod
def get_centroid(cls) -> Vector2:
return Vector2(sum([obj.position for obj in cls.objects], start= Vector2()) / len(cls.objects))
@classmethod
def draw_centroid(cls) -> None:
pygame.draw.circle(screen, cls.COLOR, cls.get_centroid(), 10)
class Rock(Mover):
COLOR = RED
objects = list()
def __init__(self, pos = None) -> None:
super().__init__(pos)
Rock.objects.append(self)
@classmethod
def show_all(cls) -> None:
super(Rock, cls).show_all()
@classmethod
def generate_objects(cls, n: int) -> None:
super(Rock, cls).generate_objects(n)
@classmethod
def get_centroid(cls) -> Vector2:
return super(Rock, cls).get_centroid()
@classmethod
def draw_centroid(cls) -> None:
super(Rock, cls).draw_centroid()
class Paper(Mover):
COLOR = GREEN
objects = list()
def __init__(self, pos = None) -> None:
super().__init__(pos)
Paper.objects.append(self)
@classmethod
def show_all(cls) -> None:
super(Paper, cls).show_all()
@classmethod
def generate_objects(cls, n: int) -> None:
super(Paper, cls).generate_objects(n)
@classmethod
def get_centroid(cls) -> Vector2:
return super(Paper, cls).get_centroid()
@classmethod
def draw_centroid(cls) -> None:
super(Paper, cls).draw_centroid()
class Scissor(Mover):
COLOR = BLUE
objects = list()
def __init__(self, pos = None) -> None:
super().__init__(pos)
Scissor.objects.append(self)
@classmethod
def show_all(cls) -> None:
super(Scissor, cls).show_all()
@classmethod
def generate_objects(cls, n: int) -> None:
super(Scissor, cls).generate_objects(n)
@classmethod
def get_centroid(cls) -> Vector2:
return super(Scissor, cls).get_centroid()
@classmethod
def draw_centroid(cls) -> None:
super(Scissor, cls).draw_centroid()
Rock.generate_objects(5)
Paper.generate_objects(5)
Scissor.generate_objects(5)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BLACK)
Rock.show_all()
Paper.show_all()
Scissor.show_all()
Rock.draw_centroid()
Paper.draw_centroid()
Scissor.draw_centroid()
pygame.display.flip()
pygame.time.delay(10)
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment