Untitled

 avatar
unknown
plain_text
2 years ago
7.3 kB
5
Indexable
import pygame
import os
import time
pygame.init()


width = 800
height = int(width * 0.8)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Game cua Nam Le")



#set FPS:
clock = pygame.time.Clock()
FPS = 30

moving_left = False
moving_right = False
shoot = False
nade = False


gravity = 0.75

#Colour of bg:
Cyan = (102,255,255)
RED = (255,0,0)
# bg = pygame.image.load("img/Background/mountain.png")
# bg = pygame.transform.scale(bg,(int(bg.get_width()),int(bg.get_height())))


#image input
bullet_img = pygame.image.load("img/icons/bullet.png").convert_alpha()
grenade_img = pygame.image.load("img/icons/grenade.png").convert_alpha()

class soldier (pygame.sprite.Sprite):
	def __init__(self,char_type, x, y, scale, speed, magazine, health):
		pygame.sprite.Sprite.__init__(self)
		self.char_type = char_type

		self.Alive = True

		self.jump = False
		self.in_air = True
		self.flip = False

		self.speed = speed

		self.direction = 1
		

		self.vel_y = 0



		self.scale = scale


		self.animation_list = []
		self.action = 0
		self.index = 0
		self.update_time = pygame.time.get_ticks()

		self.grenade = 5
		self.magazine = magazine
		self.start_bullet = magazine
		self.shoot_cooldown = 0

		self.health = health
		self.start_health = self.health
		self.alive = True

		animation_type = ["Idle","Run", "Jump", "Death"]
		for animation in animation_type:
			temp = []
			num_of_frames = len(os.listdir(f'img/{self.char_type}/{animation}'))
			for i in range(0,num_of_frames):
				img = pygame.image.load(f"img/{self.char_type}/{animation}/{i}.png").convert_alpha()
				img = pygame.transform.scale(img, (int(img.get_width() * self.scale), int(img.get_height()*self.scale)))
				temp.append(img)
			self.animation_list.append(temp)

		self.image = self.animation_list[self.action][self.index]
		self.rect = img.get_rect()
		self.rect.center = (x,y)


	def move (self, moving_left, moving_right):
		dx = 0
		dy = 0
		if moving_left:
			dx = -self.speed
			self.direction = -1
			self.flip = True
		if moving_right:
			dx = self.speed
			self.direction = 1
			self.flip = False
		#Cap nhat rectangle position

		
		# Nhay
		if (self.jump == True and self.in_air == False):
			self.vel_y = -11
			self.jump = False
			self.in_air = True
		self.vel_y += gravity

		if self.vel_y > 10:
			self.vel_y


		dy += self.vel_y
		#Kiem tra voi nen dat
		if self.rect.bottom + dy > 300:
			dy = 300 - self.rect.bottom
			self.in_air = False




		self.rect.x += dx
		self.rect.y += dy

	def update(self,x):
		self.update_animation(x)
		self.check_alive()
		if self.shoot_cooldown > 0:
			self.shoot_cooldown -=1

	def update_animation(self, x):
		ANIMATION_COOLDOWN = x
		#Cap nhat hinh anh theo i:
		self.image = self.animation_list[self.action][self.index]
		if pygame.time.get_ticks()- self.update_time > ANIMATION_COOLDOWN:
			self.update_time = pygame.time.get_ticks()
			self.index +=1
		#loop to reset back to the first animation
		if (self.index >= len(self.animation_list[self.action])):
			if self.action == 3:
				self.index = len(self.animation_list[self.action])-1
			else:
				self.index = 0 

	def update_action(self, new_action):
		if new_action != self.action:
			self.action = new_action
			self.index = 0
			self.update_time = pygame.time.get_ticks()

	def shoot (self):
		if self.shoot_cooldown == 0 and self.magazine > 0:
			self.shoot_cooldown = 20
			bullet = Bullet(self.rect.centerx +(0.6 * self.rect.size[0]*self.direction), self.rect.centery, self.direction)
			bullet_group.add(bullet)
			self.magazine -=1 
	def check_alive(self):
		if self.health <= 0:
			self.health = 2
			self.speed = 0
			self.alive = False
			self.update_action(3)

	def drawn (self):
		screen.blit(pygame.transform.flip(self.image,self.flip, False), self.rect)
	
class Bullet(pygame.sprite.Sprite):
	def __init__(self, x, y, direction):
		pygame.sprite.Sprite.__init__(self)
		self.speed = 100
		self.image = bullet_img
		self.rect = self.image.get_rect()
		self.rect.center = (x,y)
		self.direction = direction
	def update (self):
		#movement of bullet
		self.rect.x += (self.direction*self.speed)
		#check if bullet has gone off screen
		if self.rect.right < 0 or self.rect.left >800:
			self.kill()

		#check collided
		if pygame.sprite.spritecollide(player, bullet_group, False):
			if player.alive:
				player.health -= 5
				self.kill()
		if pygame.sprite.spritecollide(enemy, bullet_group, False):
				if enemy.alive:
					enemy.health -= 25
					print(enemy.health)
					self.kill()



class Grenade(pygame.sprite.Sprite):
	def __init__(self, x, y, direction):
		pygame.sprite.Sprite.__init__(self)
		self.speed = 10
		self.image = grenade_img
		self.rect = self.image.get_rect()
		self.rect.center = (x,y)
		self.direction = direction
		self.vel_y = -11
	def update(self):
		self.vel_y += gravity
		dx = self.speed * self.direction
		dy = self.vel_y

		if self.rect.bottom + dy >= 300:
			dy = 300 - self.rect.bottom
		if self.rect.right > 800 and self.rect.left < 0:
			self.speed = -1
		
		self.rect.x += self.speed
		self.rect.y += dy		

bullet_group = pygame.sprite.Group()
grenade_group = pygame.sprite.Group()


running = True
x = 200
y = 200

player = soldier("player",x,y,3, 5, 20,100)
enemy = soldier("enemy",400,200,3,5,20,100)
# enemy = soldier("Enemy",400,200,3,5)


while running:

	clock.tick(FPS)
	screen.fill(Cyan)
	# screen.blit(bg,(0,0))
	pygame.draw.line(screen, RED, (0,300),(800,300) )

	player.update(100)
	# enemy.update_animation(500)
	player.drawn()
	enemy.update(100)
	enemy.drawn()
	player.move(moving_left, moving_right)

	bullet_group.update()
	bullet_group.draw(screen)
	grenade_group.update()
	grenade_group.draw(screen)



	if (player.Alive == True):
		if shoot:
			player.shoot()
		if nade and player.grenade > 0:
			nade = Grenade(player.rect.centerx +(0.5 * player.rect.size[0]*player.direction), player.rect.top, player.direction)
			grenade_group.add(nade)
			nade = False
			player.grenade -= 1
			print(player.grenade)
		if (player.jump == True):
			player.update_action(2)#2: jump
		elif (moving_left == True) or (moving_right==True):
			player.update_action(1)#1: run
		else:
			player.update_action(0)#0: idle
		player.move(moving_left, moving_right)


	




	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = False
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_a:
				moving_left = True
			if event.key == pygame.K_d:
				moving_right = True
			if event.key == pygame.K_w:
				player.jump = True
			if event.key == pygame.K_SPACE:
				shoot = True
			if event.key == pygame.K_q:
				nade = True
			if event.key == pygame.K_ESCAPE:
				running = False

		if event.type == pygame.KEYUP:
			if event.key == pygame.K_a:
				moving_left = False
			if event.key == pygame.K_d:
				moving_right = False
			if event.key == pygame.K_SPACE:
				shoot = False
			if event.key == pygame.K_q:
				nade = False
			
	pygame.display.update()

pygame.quit()


Editor is loading...