How to make a Rock Paper Scissors game in Django
Shivam Rohilla
Posted on January 14, 2022
Hello Devs,
In this Post I'm gonna show you how to make a rock paper scissors game in Django using Python logic.
Original Post / For More:-
https://www.webdevcodes.com/python/how-to-make-a-rock-paper-scissors-game-in-django/
Here's a Github Repo and Source Code, Live demo
https://github.com/ShivamRohilllaa/rock-paper-scissors
Live demo
https://stonepsgame.herokuapp.com/
I hope you know how to create a django project directory and base and how to create apps, so I'm going to point straightly.
So, we are gonna create following things in the project
1) Start Game / Create User
2) Game Page
3) Messages
4) Result / Store user result
5) End Game
So, now let's create models in models.py
from django.contrib.auth.models import User
class Player(models.Model):
'''
Player model
'''
name = models.CharField(max_length=50)
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='player')
def __str__(self):
return self.name
class Result(models.Model):
'''
Result model in which all players result will be stored
'''
player = models.ForeignKey("Player", on_delete=models.CASCADE, related_name='score')
bot_move = models.CharField(max_length=50, blank=True)
user_move = models.CharField(max_length=50, blank=True)
status = models.CharField(max_length=50, blank=True)
def __str__(self):
return 'Status -- ' + self.status + ' ' + self.player.name
Run these commands
python manage.py makemigrations
python manage.py migrate
Now we will create user on home page and take input from user and stored in our database.
So let's create views
def home(request):
'''
homepage and start game logic
'''
if request.method=='POST':
playername = request.POST.get('name')
if User.objects.filter(username__iexact=playername):
messages.warning(request, 'This name is already exists, please try another one.')
return HttpResponseRedirect(request.path_info)
create_user = User.objects.create(first_name=playername, username=playername)
create_player = Player.objects.create(name=playername, user=create_user)
return redirect('start_game')
return render(request, 'index.html')
In the above code, we store username from form and will create user objects from form input and if someone is already entered a username so it will show error message.
and here's a Python simple logic for game and learn how to implement python logic in django.
we make a list and store rock paper and scissors move in list and then make a bot using python random library so we can play against a bot. and when we play a move we will see a message and that message is implemented using django messages framework.
def game(request):
'''
Rock, paper and scissor logic
'''
gamelist = ['rock', 'paper', 'scissors']
bot_action = random.choice(gamelist)
user = Player.objects.all().last()
if request.method == 'POST':
user_answer = request.POST.get('name')
if user_answer == bot_action:
messages.info(request, f"Both players selected. It's a tie!")
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Tie')
log.debug("Both players selected. It's a tie!")
elif user_answer == "rock":
if bot_action == "scissors":
messages.success(request, "Rock smashes scissors! You win!")
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Win')
log.debug(f"Rock smashes scissors! You win! - Actions: Bot - {bot_action} User - {user_answer}")
else:
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Lose')
messages.info(request, "Paper covers rock! You lose.")
log.debug(f"Paper covers rock! You lose. Actions: Bot - {bot_action} User - {user_answer}" )
elif user_answer == "paper":
if bot_action == "rock":
messages.success(request, "Paper covers rock! You win!")
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Win')
log.debug(f"Paper covers rock! You win! Actions: Bot - {bot_action} User - {user_answer}")
else:
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Lose')
messages.info(request, "Scissors cuts paper! You lose.")
log.debug(f"Scissors cuts paper! You lose. Actions: Bot - {bot_action} User - {user_answer}")
elif user_answer == "scissors":
if bot_action == "paper":
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Win')
messages.success(request, "Scissors cuts paper! You win!")
log.debug(f"Scissors cuts paper! You win! Actions: Bot - {bot_action} User - {user_answer}")
else:
result = Result.objects.create(player=user, bot_move=bot_action, user_move=user_answer, status='Lose')
messages.info(request, "Rock smashes scissors! You lose.")
log.debug(f"Rock smashes scissors! You lose. Actions: Bot - {bot_action} User - {user_answer}")
return render(request, 'game.html', {'user':user})
here's a result page in which you can see your result for every move.
def result(request):
'''
All users results
'''
res = Result.objects.all().order_by('-id')
context = {'res':res}
return render(request, 'result.html', context)
Now define views urls path in urls.py
urlpatterns = [
path('', views.home, name='home'),
path('start-game/', views.game, name='start_game'),
path('result/', views.result, name='result'),
]
Here's a Github Repo and Source Code, Live demo
https://github.com/ShivamRohilllaa/rock-paper-scissors
Live demo
https://stonepsgame.herokuapp.com/
Here's images of Project
Posted on January 14, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.