Here you find the Top 5 Python Game Code with single code brackets then you need to just copy and paste into the code editor, Python is a versatile programming language that can be used to develop a wide variety of applications, including games. With libraries like Pygame, Pyglet, and Turtle, creating games in Python is both easy and fun.
This is the Python Game Code If you have any other requests or project ideas then please share them with YouTube Channel.
Snake Game Code
import turtle
import random
# Set up the screen
screen = turtle.Screen()
screen.title("Snake Game")
screen.bgcolor("black")
screen.setup(width=600, height=600)
# Create the snake head
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Create the food
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.penup()
food.goto(random.randint(-290, 290), random.randint(-290, 290))
# Set up the score
score = 0
score_text = turtle.Turtle()
score_text.color("white")
score_text.penup()
score_text.hideturtle()
score_text.goto(0, 260)
score_text.write("Score: {}".format(score), align="center", font=("Courier", 24, "normal"))
# Set up the game loop
delay = 0
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
elif head.direction == "down":
y = head.ycor()
head.sety(y - 20)
elif head.direction == "right":
x = head.xcor()
head.setx(x + 20)
elif head.direction == "left":
x = head.xcor()
head.setx(x - 20)
def go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_right():
if head.direction != "left":
head.direction = "right"
def go_left():
if head.direction != "right":
head.direction = "left"
screen.listen()
screen.onkeypress(go_up, "Up")
screen.onkeypress(go_down, "Down")
screen.onkeypress(go_right, "Right")
screen.onkeypress(go_left, "Left")
# Set up the snake segments
segments = []
while True:
screen.update()
# Check for collision with the border
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
score_text.clear()
score_text.write("Game Over! Final Score: {}".format(score), align="center", font=("Courier", 24, "normal"))
break
# Check for collision with the food
if head.distance(food) < 20:
food.goto(random.randint(-290, 290), random.randint(-290, 290))
score += 10
score_text.clear()
score_text.write("Score: {}".format(score), align="center", font=("Courier", 24, "normal"))
# Create a new segment of the snake
new_segment = turtle.Turtle()
new_segment.shape("square")
new_segment.color("gray")
new_segment.penup()
segments.append(new_segment)
# Move the segments of the snake
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
# Move the first segment of the snake to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
# Check for collision with the snake body
for segment in segments:
if segment.distance(head) < 20:
score_text.clear()
score_text.write("Game Over! Final Score: {}".format(score), align="center", font=("Courier", 24, "normal"))
break
# Add a new segment to the snake
new_segment = turtle.Turtle()
new_segment.shape("square")
new_segment.color("gray")
new_segment.penup()
segments.append(new_segment)
# Set the delay
turtle.delay(delay)
turtle.done()
Pong Game Code
# Step 1 import set up turtle and Screen
import turtle
import random
s = turtle.Screen()
s.title("Pong")
s.bgcolor("black")
s.setup(width=600, height=400)
# Step 2 Create ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 4
ball.dy = 4
# Step 3 Create AI paddle
ai = turtle.Turtle()
ai.speed(0)
ai.shape("square")
ai.color("white")
ai.penup()
ai.goto(-250, 0)
ai.shapesize(stretch_wid=5, stretch_len=1)
# Step 4 Create a paddle For You
you = turtle.Turtle()
you.speed(0)
you.shape("square")
you.color("white")
you.penup()
you.goto(250, 0)
you.shapesize(stretch_wid=5, stretch_len=1)
# Step 5 Create Function to move AI paddle
def move_ai_paddle():
y = ball.ycor()
if y > 0:
ai.sety(ai.ycor() + 2)
else:
ai.sety(ai.ycor() - 2)
# Step 6 Create a Function to move the your paddle
def paddle2_up():
y = you.ycor()
y += 20
you.sety(y)
def paddle2_down():
y = you.ycor()
y -= 20
you.sety(y)
# Your Paddle control it with key
s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")
# Step 7 Start the game with a while loop
while True:
s.update()
# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Check for collisions with the walls
if ball.ycor() > 190 or ball.ycor() < -190:
ball.dy *= -1
# Move the robot paddle towards the ball
if ball.ycor() > ai.ycor():
ai.sety(ai.ycor() + 4)
elif ball.ycor() < ai.ycor():
ai.sety(ai.ycor() - 4)
# Check for end game conditions
if ball.xcor() > 300:
turtle.textinput("Game End", "You Loss Pong Game With AI!")
break
if ball.xcor() < -300:
turtle.textinput("Game End", "You Win Pong Game With AI!")
break
# Check for collisions with the robot paddle
if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < ai.ycor() + 40 and ball.ycor() > ai.ycor() - 40):
if random.random() < 0.9: # 90% chance of collision
ball.dx *= -1
# Check for collisions with the user paddle
if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40):
ball.dx *= -1
turtle.exitonclick()
Evil Turtle Game Code
# Step 1 import set up turtle and Screen
import turtle
import time
import random
wn = turtle.Screen()
wn.title("Golden Turtle")
wn.bgcolor("lightgreen")
wn.setup(width=600, height=600)
# Step 2 Create a metal collector
head = turtle.Turtle()
head.speed(0)
head.shape("circle")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Step 4 Create a golden turtle
food = turtle.Turtle()
food.speed(0)
food.shape("turtle")
food.color("red")
food.penup()
food.goto(0, 100)
# Step 5 Control collect speed direction
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 2)
if head.direction == "down":
y = head.ycor()
head.sety(y - 2)
if head.direction == "left":
x = head.xcor()
head.setx(x - 2)
if head.direction == "right":
x = head.xcor()
head.setx(x + 2)
def go_up():
head.direction = "up"
def go_down():
head.direction = "down"
def go_left():
head.direction = "left"
def go_right():
head.direction = "right"
# Step 6 Control collect with key
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
# Step 7 Start the game with a while loop
while True:
wn.update()
# Check for collision with the border
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
# Check for collision with the food
if head.distance(food) < 20:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
move()
wn.mainloop()
Dodge The Blocks Game Code
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the window
WIDTH = 400
HEIGHT = 600
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Dodge the Blocks")
# Set up the clock
clock = pygame.time.Clock()
# Set up the player
player_width = 50
player_height = 50
player_x = WIDTH // 2 - player_width // 2
player_y = HEIGHT - player_height - 20
player_color = (0, 255, 0)
player_speed = 5
player_rect = pygame.Rect(player_x, player_y, player_width, player_height)
# Set up the blocks
block_width = 50
block_height = 50
block_color = (255, 0, 0)
block_speed = 5
blocks = []
spawn_counter = 0
spawn_delay = 60
# Set up the game loop
game_over = False
while not game_over:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Move the player
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_rect.left > 0:
player_rect.x -= player_speed
if keys[pygame.K_RIGHT] and player_rect.right < WIDTH:
player_rect.x += player_speed
# Spawn new blocks
spawn_counter += 1
if spawn_counter >= spawn_delay:
spawn_counter = 0
x = random.randint(0, WIDTH - block_width)
y = -block_height
block_rect = pygame.Rect(x, y, block_width, block_height)
blocks.append(block_rect)
# Move the blocks
for block in blocks:
block.y += block_speed
# Check for collisions
for block in blocks:
if block.colliderect(player_rect):
game_over = True
# Remove blocks that have gone off the screen
blocks = [block for block in blocks if block.bottom > 0]
# Draw the game
window.fill((0, 0, 0))
pygame.draw.rect(window, player_color, player_rect)
for block in blocks:
pygame.draw.rect(window, block_color, block)
pygame.display.flip()
# Wait for the next frame
clock.tick(60)
# Clean up the game
pygame.quit()
Racing Game
# Step 1 import and Set the turtle's bg colour
import turtle
import random
screen = turtle.Screen()
screen.bgcolor('orchid')
# Step 2 create turtle colour
turtles = []
colors = ['red', 'green', 'blue', 'purple', 'gold']
# Step 3 using rang loop create turtle and add colour
for i in range(5):
t = turtle.Turtle()
t.color(colors[i])
t.shape('turtle')
t.penup()
t.goto(-160, 100 - i*40)
t.pendown()
turtles.append(t)
# Step 4 Create a finish line
finish_line = turtle.Turtle()
finish_line.color('black')
finish_line.penup()
finish_line.goto(300, -180)
finish_line.pendown()
finish_line.pensize(5)
finish_line.setheading(90)
finish_line.forward(400)
# Step 5 set turtle random speed
for t in turtles:
t.speed(0)
t.forward(random.randint(1,5))
# Step 6 Start the race
while not any(t.xcor() > finish_line.xcor() for t in turtles):
for t in turtles:
t.forward(random.randint(1,5))
# Step 7 Check and write the winner's name
winner = [t for t in turtles if t.xcor() > finish_line.xcor()][0]
turtle.write("The winner is " + winner.color()[0] + " turtle!", align="center", font=("Arial", 18, "bold"))
screen.exitonclick()
Another Programmer For Coders:-
Top 5 Python graphics?
