Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Sure!

Python is a great language


for creating games. Here's a
simple example of a Python code
to create a basic game using the
popular Pygame library:

```python
import pygame
import random

# Initialize the pygame


pygame.init()

# Create the screen


screen =
pygame.display.set_mode((800,
600))

# Title and Icon


pygame.display.set_caption("My
Game")
icon =
pygame.image.load('game_icon.
png')
pygame.display.set_icon(icon)
# Player
playerImg =
pygame.image.load('player.png')
playerX = 370
playerY = 480
playerX_change = 0

def player(x, y):


screen.blit(playerImg, (x, y))

# Game Loop
running = True
while running:
screen.fill((255, 255, 255))
for event in
pygame.event.get():
if event.type ==
pygame.QUIT:
running = False
# If keystroke is pressed
check whether it's right or left
if event.type ==
pygame.KEYDOWN:
if event.key ==
pygame.K_LEFT:
playerX_change = -0.1
if event.key ==
pygame.K_RIGHT:
playerX_change = 0.1
if event.type ==
pygame.KEYUP:
if event.key ==
pygame.K_LEFT or event.key ==
pygame.K_RIGHT:
playerX_change = 0

# Update player position


playerX += playerX_change

# Boundaries
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Draw the player
player(playerX, playerY)

pygame.display.update()
```
This code sets up a basic game
window and allows the player to
move a character left and right
using the arrow keys. You can
also add features like enemies,
scoring, and more complex
behaviors to make a full-fledged
game. This example uses the
Pygame library, so make sure to
install it using pip before running
the code!

You might also like