با توجه به استفاده از math در فایل main.py فایل نهایی آن به صورت زیر تغییر میکند:

#main.py

import pygame
from settings import WIN_HEIGHT,WIN_WIDTH,CAPTION,ENEMY_COUNT
from models import Player,Background,Bullet,Enemy
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)
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)

		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)
				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()

	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
)
from pygame import image,transform

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

	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 - 110

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

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


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)

	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))
#setting.py


#display
WIN_WIDTH = 600
WIN_HEIGHT = 750

CAPTION = "pygame tutorial"

#images
PLAYER = "player.png"
BACKGROUND = "background.jpg"
BULLET_IMG = "bullet.png"
ENEMY = "enemy.png"

ENEMY_COUNT = 7

 

سایر قسمت ها :

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

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

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

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

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

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