Python simple game code 100 lines, the simplest game code in python

This article will tell you about 100 lines of python simple game code and the simplest python game code. I hope it will be helpful to you. Don’t forget to bookmark this site.

Hello everyone, this article will focus on programming the simplest game code in python. An entry-level game with 20 lines of python code is something that many people want to understand. To understand the entry-level game code of python game programming, you need to understand the following points first thing.

1. Rock-paper-scissors game

Goal : To create a command-line game, the player can choose between rock, scissors and cloth, and the computer PK paper check rate is how much . If the player wins, the score is added until at the end of the game, the final score is displayed to the player pseudo-original tool .

Hint : Take the player's choice and compare it to the computer's choice. The computer's selection is picked randomly from a selection list. If the player wins, add 1 point.

import random

choices = ["Rock", "Paper", "Scissors"]

computer = random.choice(choices)

player = False cpu_score = 0 player_score = 0

while True: player = input("Rock, Paper or Scissors?").capitalize()



# 判断游戏者和电脑的选择



if player == computer:

print("Tie!") elif player == "Rock":



if computer == "Paper":

print("You lose!", computer, "covers", player) cpu_score+=1



else:

print("You win!", player, "smashes", computer) player_score+=1 elif player == "Paper":



if computer == "Scissors":

print("You lose!", computer, "cut", player) cpu_score+=1



else:

print("You win!", player, "covers", computer) player_score+=1 elif player == "Scissors":



if computer == "Rock":

print("You lose...", computer, "smashes", player) cpu_score+=1



else:

print("You win!", player, "cut", computer) player_score+=1 elif player=='E':

print("Final Scores:") print(f"CPU:{cpu_score}") print(f"Plaer:{player_score}")



break else:

print("That's not a valid play. Check your spelling!")

computer = random.choice(choices)

2. Automatically send emails

Purpose : Write a Python script that can be used to send emails.

Tip : The email library can be used to send emails.

import smtplib from email.message

import EmailMessage



email = EmailMessage() ## Creating a object for EmailMessage

email['from'] = 'xyz name' ## Person who is sending

email['to'] = 'xyz id' ## Whom we are sending

email['subject'] = 'xyz subject' ## Subject of email

email.set_content("Xyz content of email") ## content of email



with smtlib.SMTP(host='smtp.gmail.com',port=587) as smtp:

## sending request to server



smtp.ehlo() ## server object

smtp.starttls() ## used to send data between server and client

smtp.login("email_id","Password") ## login id and password of gmail

smtp.send_message(email) ## Sending email



print("email send") ## Printing success message

3. Hangman

Purpose : Create a simple command line hangman game.

Tip: Create a list of passwords and choose a word at random. Now represent each word with an underscore "_", give the user a chance to guess the word, and if the user guesses the word correctly, replace the "_" with the word.

import time

import random



name = input("What is your name? ")



print ("Hello, " + name, "Time to play hangman!")

time.sleep(1)

print ("Start guessing...\n")

time.sleep(0.5) ## A List Of Secret



Words words = ['python','programming','treasure','creative','medium','horror']

word = random.choice(words)

guesses = ' '

turns = 5

while turns > 0:

failed = 0

for char in word:

if char in guesses:

print (char,end="")

else:

print ("_",end=""),

failed += 1



if failed == 0: print ("\nYou won")

break

guess = input("\nguess a character:")

guesses += guess

if guess not in word:

turns -= 1

print("\nWrong")

print("\nYou have", + turns, 'more guesses')

if turns == 0:

print ("\nYou Lose")

For more project source code, please continue to pay attention to the editor. If you encounter difficulties in learning and want to find a python learning and communication environment, you can join our Python learning Q group 249180188 and receive python learning materials, which will save a lot of time and reduce many problems you encounter.

4. Alarm clock

Purpose : Write a Python script that creates an alarm clock.

Tip : You can use the date-time module to create alarm clocks, and the playsound library to play sounds.


 

from datetime import datetime

from playsound import playsound

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")

alarm_hour=alarm_time[0:2]

alarm_minute=alarm_time[3:5]

alarm_seconds=alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

print("Setting up alarm..")

while True:

now = datetime.now()

current_hour = now.strftime("%I")

current_minute = now.strftime("%M")

current_seconds = now.strftime("%S")

current_period = now.strftime("%p")

if(alarm_period==current_period):

if(alarm_hour==current_hour):

if(alarm_minute==current_minute):

if(alarm_seconds==current_seconds):

print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

5. Weather application

Purpose : Write a Python script that receives a city name and uses a crawler to get weather information for that city.

Tip : You can use Beautifulsoup and the requests library to scrape data directly from the Google homepage.

Install : requests, BeautifulSoup

from datetime import datetime

from playsound import playsound

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")

alarm_hour=alarm_time[0:2]

alarm_minute=alarm_time[3:5]

alarm_seconds=alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

print("Setting up alarm..")

while True:

now = datetime.now()

current_hour = now.strftime("%I")

current_minute = now.strftime("%M")

current_seconds = now.strftime("%S")

current_period = now.strftime("%p")

if(alarm_period==current_period):

if(alarm_hour==current_hour):

if(alarm_minute==current_minute):

if(alarm_seconds==current_seconds):

print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

Here I still want to recommend the Python learning Q group I built myself: 249029188. The group is all learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and shares dry goods from time to time ( Only related to Python software development), including a copy of the latest Python advanced materials and zero-based teaching in 2021 compiled by myself, welcome advanced and interested friends to join in Python!

Guess you like

Origin blog.csdn.net/mynote/article/details/132623443