با توجه به اضاقه شدن کلاس HealthBar به فایل models.py این فایل و همچنین فایل های main.py , settings.py به دلیل اضافه شدن گرافیک های جدید به صورت زیر تغییر میکنند.

#main.py

import pygame
from settings import WIN_HEIGHT,WIN_WIDTH,CAPTION,ENEMY_COUNT
from models import Player,Background,Bullet,Enemy,StatusBar,ScoreMeter,HealthBar
import random
from math import dist

win = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT))
pygame.display.set_caption(CAPTION)
player = Player(WIN_WIDTH//2)
status_bar = StatusBar()
score_meter = ScoreMeter()
health_bar = HealthBar()
backgrounds = [Background(0),Background(-WIN_HEIGHT)]
bullets = []
enemies = []


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:
				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:
					enemy.remove(enemies)
					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()

	player_control()

	status_bar.draw(win)

	score_meter.draw(win)

	health_bar.draw(win,player.health)

	pygame.display.update()

#main_loop
def main_loop():
	clock = pygame.time.Clock()
	running = True
	while running:
		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:
					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)
		
		redraw()

main_loop()
#models.py

from settings import (
	WIN_HEIGHT,
	WIN_WIDTH,
	PLAYER,
	BACKGROUND,
	BULLET_IMG,
	ENEMY,
	FONT_SMALL,
	HEART
)
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)

 

#settings.py

from pygame import font
font.init()

#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"

#number of renders
ENEMY_COUNT = 7

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

 

سایر قسمت ها :

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

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

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

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

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

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

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

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