How to create your first Python game
At first, you should require the python development environment so let's get started.
The first step is to install the latest version of Python 3 on your computer. after that install an IDE Pycharm it is very helpful to run your code in Python now you have installed the required Softwares on your computer next step is to configure the pycharm for your game development so go through the following steps
- create a new python file on your project - Main.py
- then click on FILE
- then click on the settings
- after that click on Python Interpreter
- then click on add option and search for pygame and install it.
- congrats you are ready to code a game
now just paste the given code in your main file
import pygame import random import math from pygame import mixer # initializationpygame.init() # creating the screenscreen = pygame.display.set_mode((800, 600)) # Backgroundbackground = pygame.image.load('space2.jpg') # background soundmixer.music.load('back.mp3') mixer.music.play(-1) # Title and Iconpygame.display.set_caption("Space Invader") icon = pygame.image.load('gaming.png') pygame.display.set_icon(icon) # playerplayerImg = pygame.image.load('player2.png') playerX = 370playerY = 480playerX_change = 0 # EnemyenemyImg = [] enemyX = [] enemyY = [] enemyX_change = [] enemyY_change = [] num_of_enemies = 6 for i in range(num_of_enemies): enemyImg.append(pygame.image.load('signs.png')) enemyX.append(random.randint(0, 736)) enemyY.append(random.randint(50, 150)) enemyX_change.append(3) enemyY_change.append(40) # bullet # Ready means you can't see the bullet# fire means bullet moves in the upward directionbulletImg = pygame.image.load('bullet.png') bulletX = 0bulletY = 480bulletX_change = 0bulletY_change = 10bullet_state = "Ready" # Scorescore_value = 0font = pygame.font.Font('freesansbold.ttf', 32) textX = 10textY = 10 # Game over over_font = pygame.font.Font('freesansbold.ttf', 64) def show_score(x, y): score = font.render("Score: " + str(score_value), True, (255, 255, 255)) screen.blit(score, (x, y)) def game_over_text(): over_text = over_font .render(" GAME OVER ", True, (255, 255, 255)) screen.blit(over_text, (200, 250)) def player(x, y): screen.blit(playerImg, (x, y)) def enemy(x, y, i): screen.blit(enemyImg[i], (x, y)) def fire_bullet(x, y): global bullet_state bullet_state = "Fire" screen.blit(bulletImg, (x + 48, y + 10)) def iscollision(enemyX, enemyY, bulletX, bulletY): distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2))) if distance < 27: return True else: return False # Game looprunning = Truewhile running: screen.fill((0, 255, 255)) screen.blit(background, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # If keystroke is pressed check weather it is left or right if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: playerX_change = -4 if event.key == pygame.K_RIGHT: playerX_change = 4 if event.key == pygame.K_SPACE: if bullet_state is "Ready": bullet_sound = mixer.Sound('shot.wav') bullet_sound.play() bulletX = playerX fire_bullet(bulletX, bulletY) if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: playerX_change = 0 # Checking for boundary for player playerX += playerX_change if playerX <= 0: playerX = 0 elif playerX >= 673: playerX = 673 # Checking for boundary for enemy # enemy movement for i in range(num_of_enemies): # game over if enemyY[i] > 440: for j in range(num_of_enemies): enemyY[j] = 2000 game_over_text() break enemyX[i] += enemyX_change[i] if enemyX[i] <= 0: enemyX_change[i] = 3 enemyY[i] += enemyY_change[i] elif enemyX[i] >= 736: enemyX_change[i] = -3 enemyY[i] += enemyY_change[i] collision = iscollision(enemyX[i], enemyY[i], bulletX, bulletY) if collision: blast_sound = mixer.Sound('blast.wav') blast_sound.play() bulletY = 480 bullet_state = "Ready" score_value += 1 enemyX[i] = random.randint(0, 736) enemyY[i] = random.randint(50, 150) enemy(enemyX[i], enemyY[i], i) # Bullet movement if bulletY <= 0: bulletY = 480 bullet_state = "Ready" if bullet_state is "Fire": fire_bullet(bulletX, bulletY) bulletY -= bulletY_change player(playerX, playerY) show_score(textX, textY) pygame.display.update()
now you have to add some files in your project
for background, a png file of 800 X 600 and name it as space2
for the icon, a png file of 32 X 32 and name it gamming
for a player, a png file of 64 X 64 and name it player2
for an enemy, a png file of 32 X 32 and name it signs
for a bullet, a png file of 32 X 32 and name it bullet
for background music, an mp3 file, and name it back
for shootout sound, a wav file and name it shot
for blast sound effect, a wav file and name it blast
you can download the above files from google.
now click on the run button your game is ready and you can play it
thankyou for visiting the blog please share it to more people
Comments
Post a Comment