با توجه به کامل شدن بازی فایل های main.py , models.py , settings.py به حالت های زیر تغییر میکنند:

 

(در صورت گرفتن ارور Failed to execute script pyi_rth_pkgres دستور زیر را موقع تبدیل فایل py به فایل exe وارد کنید:)

--hidden-import pkg_resources.py2_warn
#settings.py



from pygame import font,mixer
import os

font.init()
mixer.pre_init(44100, 16, 1, 512)
mixer.init()

BASE_DIR = os.getcwd()

#display
WIN_WIDTH = 600
WIN_HEIGHT = 750
CAPTION = "pygame tutorial"



#images
PLAYER = "player.png"
BACKGROUND = "background.jpg"
BULLET_IMG = "bullet.png"
ENEMY = "enemy.png"
HEART = "heart.png"
LOGO = "alien.png"
EXPLOSION_EFFECT = "explosion.png"
HEALTH_PACK = "healthpack.png"

#numberh of renders
ENEMY_COUNT = 7
BULLET_COUNT = 10
HEALTH_PACK_LEN = 1

#fonts
FONT_SMALL = font.SysFont("Sans",30)
FONT_BIG = font.SysFont("Sans",80)

#sound effects
SOUND_EFFECTS = {
	"shoot": mixer.Sound(os.path.join(BASE_DIR,"musics/shoot1.wav")),
	"hit":  mixer.Sound(os.path.join(BASE_DIR,"musics/hit.wav")),
	"collision":  mixer.Sound(os.path.join(BASE_DIR,"musics/collision.wav")),
	"menu-music": mixer.music.load(os.path.join(BASE_DIR,'musics/menu.wav')),
	"heal": mixer.Sound(os.path.join(BASE_DIR,'musics/heal.wav')),
}
#models.py


from settings import (
	WIN_HEIGHT,
	WIN_WIDTH,
	PLAYER,
	BACKGROUND,
	BULLET_IMG,
	ENEMY,
	FONT_SMALL,
	HEART,
	EXPLOSION_EFFECT,
	HEALTH_PACK
)
from pygame import image,transform,draw

class Player(object):
	def __init__(self,x):
		self.x = x - 32
		self.y = WIN_HEIGHT - 130
		self.img = image.load(PLAYER)
		self.vel = 0
		self.health = 100

	def draw(self,screen):
		screen.blit(self.img,(self.x,self.y))

	def move(self):
		if (self.x >= 0 or self.vel > 0) and (self.x <= WIN_WIDTH - 64 or self.vel < 0):
			self.x += self.vel

		if self.vel > 0:
			self.img = transform.rotate(image.load(PLAYER),-15)
			self.y = WIN_HEIGHT - 140

		if self.vel < 0:
			self.img = transform.rotate(image.load(PLAYER),15)
			self.y = WIN_HEIGHT - 140

		if self.vel == 0:
			self.img = image.load(PLAYER)
			self.y = WIN_HEIGHT - 130


class Background(object):
	def __init__(self,y):
		self.x = 0
		self.y = y
		self.img = transform.scale(image.load(BACKGROUND),(WIN_WIDTH,WIN_HEIGHT))
		self.vel = 2

	def draw(self , screen):
		screen.blit(self.img,(self.x,self.y))

	def move(self):
		self.y += self.vel
		if self.y == WIN_HEIGHT:
			self.y = -WIN_HEIGHT

class Enemy(object):
	def __init__(self,x,y):
		self.x = x 
		self.y = y
		self.vel = 3
		self.img = image.load(ENEMY)
		self.damage = 10

	def draw(self,screen):
		screen.blit(self.img,(self.x,self.y))

	def move(self):
		self.y += self.vel

	def add(self,list_):
		list_.append(self)

	def remove(self,list_):
		list_.pop(list_.index(self))




class Bullet(object):
	def __init__(self,x,y):
		self.x = x + 15
		self.y = y
		self.vel = -10
		self.acc = -0.5
		self.img = transform.scale(image.load(BULLET_IMG),(32,32))

	def draw(self,screen):
		screen.blit(self.img,(self.x,self.y))

	def move(self):
		self.vel += self.acc
		self.y += self.vel

	def add(self,list_):
		list_.append(self)

	def remove(self,list_):
		list_.pop(list_.index(self))


class StatusBar(object):
	def __init__(self):
		self.x = 0
		self.y = WIN_HEIGHT - 50
		self.color = (255,255,255)
		self.width = WIN_WIDTH
		self.height = 50

	def draw(self,screen):
		draw.rect(screen,self.color,(self.x,self.y,self.width,self.height))


class ScoreMeter(object):
	def __init__(self):
		self.x = WIN_WIDTH - 250
		self.y = WIN_HEIGHT - 45
		self.count = 0
		self.text = FONT_SMALL.render("SCORE: "+str(self.count),True,(0,0,0))

	def draw(self,screen):
		screen.blit(self.text,(self.x,self.y))

	def increment_count(self):
		self.count += 1
		self.text = FONT_SMALL.render("SCORE: "+str(self.count),True,(0,0,0))

class HealthBar(object):
	def __init__(self):
		self.x = 47
		self.y = WIN_HEIGHT-41
		self.width = 206
		self.height = 32
		self.color = (0,0,0)
		self.meter_height = 26
		self.meter_color = (3,150,23)
		self.pic = transform.scale(image.load(HEART),(40,40))

	def draw(self,screen,health):
		screen.blit(self.pic,(self.x-43,self.y-6))
		draw.rect(screen,self.color,(self.x,self.y,self.width,self.height),3)
		draw.rect(screen,self.meter_color,(self.x+3,self.y+3,2*health,self.meter_height))

		if health <= 40:
			self.meter_color = (201,18,18)
		elif health <= 70:
			self.meter_color = (214,211,0)
		elif health <= 100:
			self.meter_color = (3,150,23)

class Explosion(object):
	def __init__(self,x,y,time):
		self.x = x
		self.y = y
		self.time = time
		self.img = image.load(EXPLOSION_EFFECT)
		self.vel = 2

	def draw(self,screen):
		screen.blit(self.img,(self.x,self.y))

	def move(self):
		self.y += self.vel

	def add(self,list_):
		list_.append(self)

	def remove(self,list_,time):
		if (time - self.time) > 300:
			list_.pop(list_.index(self))

class HealthPack(object):
	def __init__(self,x,y,time,timer):
		self.x = x
		self.y = y
		self.time = time
		self.timer = timer
		self.vel = 2
		self.angle = 1
		self.pic = image.load(HEALTH_PACK)
		self.rotate = 1
		self.let_move = False

	def draw(self,screen,time):
		if time - self.time >= self.timer:
			screen.blit(self.pic,(self.x,self.y))
			self.let_move = True

	def move(self):
		if self.let_move:
			self.y += self.vel
			self.pic = transform.rotate(image.load(HEALTH_PACK),self.angle)
			self.angle += self.rotate
			if self.angle == 15:
				self.rotate = -1
			elif self.angle == -15:
				self.rotate = 1
	def add(self,list_):
		list_.append(self)

	def remove(self,list_):
		list_.pop(list_.index(self))

 

#main.py


import pygame
from settings import WIN_HEIGHT,WIN_WIDTH,CAPTION,ENEMY_COUNT,SOUND_EFFECTS,BULLET_COUNT,LOGO,HEALTH_PACK_LEN,FONT_SMALL,FONT_BIG
from models import Player,Background,Bullet,Enemy,StatusBar,ScoreMeter,HealthBar,Explosion,HealthPack
import random
from math import dist

pygame.init()
win = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT))
pygame.display.set_caption(CAPTION)
pygame.display.set_icon(pygame.image.load(LOGO))
player = Player(WIN_WIDTH//2)
status_bar = StatusBar()
score_meter = ScoreMeter()
health_bar = HealthBar()
backgrounds = [Background(0),Background(-WIN_HEIGHT)]
bullets = []
enemies = []
explosions = []
health_packs = []
start_text = FONT_SMALL.render('Press ENTER to Start',True,(255,255,255))
game_over_text = FONT_BIG.render('GAME OVER',True,(255,0,0))
start_again_text = FONT_SMALL.render("Press Enter to start again",True,(255,255,255))

def health_pack_control():
	if player.health < 50 and len(health_packs)<HEALTH_PACK_LEN:
		new_pack = HealthPack(random.randint(0,WIN_WIDTH-64),random.randint(-1600,-50),pygame.time.get_ticks(),random.randint(10000,60000))
		new_pack.add(health_packs)

	for pack in health_packs:
		pack.draw(win , pygame.time.get_ticks())
		pack.move()
		if pack.y >= WIN_HEIGHT:
			pack.remove(health_packs)
		if dist((pack.x,pack.y),(player.x,player.y)) <= 40:
			SOUND_EFFECTS.get("heal").play()
			player.health += 10
			pack.remove(health_packs)


def explosion_control():
	for explosion in explosions:
		explosion.draw(win)
		explosion.move()
		explosion.remove(explosions,pygame.time.get_ticks())

def enemy_control():
	if len(enemies) < ENEMY_COUNT:
		r_x = random.randint(0,WIN_WIDTH - 64)
		r_y = random.randint(-1200,-200)
		if all(dist((r_x,r_y),(enemy.x,enemy.y))>70 for enemy in enemies):
			Enemy(r_x,r_y).add(enemies)

	for enemy in enemies:
		enemy.draw(win)
		enemy.move()
		if dist((player.x,player.y),(enemy.x,enemy.y))<40:
			if enemy in enemies:
				SOUND_EFFECTS.get("collision").play()
				enemy.remove(enemies)
				player.health -= enemy.damage

		if enemy.y > WIN_HEIGHT:
			enemy.remove(enemies)
		for bullet in bullets:
			if dist((bullet.x,bullet.y+22),(enemy.x+15,enemy.y+30)) < 35:
				if enemy in enemies:
					SOUND_EFFECTS.get("hit").play()
					enemy.remove(enemies)
					Explosion(enemy.x,enemy.y,pygame.time.get_ticks()).add(explosions)
					score_meter.increment_count()
				bullet.remove(bullets)


def bullet_control():
	for bullet in bullets:
		bullet.draw(win)
		bullet.move()
		if bullet.y < 0:
			bullet.remove(bullets) 

def background_control():
	for background in backgrounds:
		background.draw(win)
		background.move()


def player_control():
	player.draw(win)
	player.move()

def redraw():
	win.fill((0,0,0))

	background_control()

	bullet_control()

	enemy_control()

	explosion_control()

	health_pack_control()

	player_control()

	status_bar.draw(win)

	score_meter.draw(win)

	health_bar.draw(win,player.health)

	pygame.display.update()

def reset_game():
	global player , enemies , bullets , explosions , score_meter , health_packs
	player = Player(WIN_WIDTH//2)
	score_meter = ScoreMeter()
	enemies=[]
	bullets = []
	health_packs = []
	explosions = []


#main_loop
def main_loop():
	clock = pygame.time.Clock()
	running = True
	menu = True
	while running:
		while menu:
			if pygame.mixer.music.get_busy():
				pygame.mixer.music.play()

			if player.health<=0:
				win.blit(game_over_text,(90,300))
				win.blit(start_again_text,(147,400))
			else:
				win.fill((9,129,176))
				win.blit(start_text,(170,300))	
						
			for event in pygame.event.get():
				if event.type == pygame.KEYUP:
					if event.key == pygame.K_RETURN:
						menu = False
						reset_game()
						pygame.mixer.music.stop()

			pygame.display.update()
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				running = False

			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_RIGHT:
					player.vel = 7
				if event.key == pygame.K_LEFT:
					player.vel = -7
				if event.key == pygame.K_SPACE:
					if len(bullets) < BULLET_COUNT:
						SOUND_EFFECTS.get("shoot").play()
						Bullet(player.x,player.y).add(bullets)

			if event.type == pygame.KEYUP:
				if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
					player.vel = 0

		clock.tick(60)
		
		if player.health <= 0:
			menu = True

		redraw()

main_loop()

 

سایر قسمت ها :

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت اول

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت دوم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت سوم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت چهارم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت پنجم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت ششم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت هفتم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت هشتم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت نهم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت دهم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت یازدهم

آموزش بازی سازی با زبان برنامه نویسی پایتون قسمت دوازدهم