از آنجایی که نعداد زیادی از آبجکت ها ممکن است که روی صفحه رندر شود و در صورت نیاز لازم داریم که هر کدام از آنها را تشخیص دهیم و ریموو کنیم تمام آبجکت هارا در یک لیست قرار میدهیم پس فایل main.py به صورت زیر تغییر میکند:

#main.py

import pygame
from settings import WIN_HEIGHT,WIN_WIDTH,CAPTION
from models import Player,Background,Bullet

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 = []


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

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

#display
WIN_WIDTH = 600
WIN_HEIGHT = 750

CAPTION = "pygame tutorial"

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

 

سایر قسمت ها :

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

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

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

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