I’m still having issues displaying a tmx map into python, I have a code that can display it onto a game, but I want to do it so I can display the map into another game. The code I used tto try all of this was made by KidsCanCode on Youtube : https://www.youtube.com/watch?v=QIXyj3WeyZM
I can’t manage to transfer this type of code to our project, which is this one :
“screen”
import pygame
class Screen:
def __init__(self,width,height):
self.width=width
self.height=height
pygame.display.set_caption("balls")
self.ecran=pygame.display.set_mode((width,height))
def refresh(self):
pygame.display.flip()
def clear(self):
self.ecran.fill((0,0,0))
def update(self):
self.clear()
self.refresh()
def draw_rect(self,x,y,color):
return pygame.draw.rect(self.ecran,color,(x,y,20,20))
def draw_attack(self,x,y,color):
return pygame.draw.rect(self.ecran,color,(x-100,y-100,200,200),1)
# a supprimer_plus_tard c'est pour t'aider
def draw_aggro(self,x,y,color):
pygame.draw.circle(self.ecran,color,(x+10,y+10),150,1)
def draw_dash(self,x,y,direction,color):
if direction=='d':
return pygame.draw.rect(self.ecran,color,(x+80,y,20,20))
if direction=='g':
return pygame.draw.rect(self.ecran,color,(x-80,y,20,20))
if direction=='h':
return pygame.draw.rect(self.ecran,color,(x,y-80,20,20))
if direction=='b':
return pygame.draw.rect(self.ecran,color,(x,y+80,20,20))
“joueur”
import pygame
from screen import *
class Player:
def __init__(self,x,y,screen):
self.hp=20
self.stats={'hp':20, 'attack':10, 'defense':10, 'crit_rate' :10, 'crit_damage':1.5, 'accuracy':1}
self.x=x
self.y=y
self.invi=False
self.cd=0
self.spend=0
self.player=screen.draw_rect(self.x,self.y,'grey')
self.atck=None
self.dir='d'
def en_vie(self):
if self.hp>0:
return True
return False
def start_invis(self):
self.invi=True
self.spend=0
def add_invis(self,spent):
self.spend+=spent
def check_invis(self):
if self.spend>=15:
self.spend=0
self.invi=False
return True
return False
def degats_recu(self,damage):
if self.check_invis()==False:
return None
else:
if self.hp>damage:
self.start_invis()
self.hp-=damage
else:
self.hp=0
def move(self,largeur,hauteur,screen,event):
if event.key==pygame.K_RIGHT:
if self.x+30<=largeur:
self.x+=10
self.dir='d'
elif event.key==pygame.K_LEFT:
if self.x-10>=0:
self.x-=10
self.dir='g'
elif event.key==pygame.K_UP:
if self.y-10>=0:
self.y-=10
self.dir='h'
elif event.key==pygame.K_DOWN:
if self.y+30<=hauteur:
self.y+=10
self.dir='b'
if self.invi==True:
self.player=screen.draw_rect(self.x,self.y,(255, 196, 99))
else:
self.player=screen.draw_rect(self.x,self.y,'grey')
def cd_start_dash(self):
self.cd_dash=0
def cd_add(self,add):
self.cd_dash+=add
def cd_dash(self):
if self.cd_dash<3000:
return False
else:
self.cd_dash=0
return True
def dash(self,largeur,hauteur,screen):
if self.dir=='d':
if self.x+100<=largeur:
self.x+=80
elif self.dir=='g':
if self.x-80>=0:
self.x-=80
elif self.dir=='h':
if self.y-80>=0:
self.y-=80
elif self.dir=='b':
if self.y+100<=hauteur:
self.y+=80
if self.invi==True:
self.player=screen.draw_dash(self.x,self.y,self.dir,(255, 196, 99))
else:
self.player=screen.draw_dash(self.x,self.y,self.dir,'grey')
def afficher_joueur(self,screen):
if self.invi==True:
self.player=screen.draw_rect(self.x,self.y,(255, 196, 99))
else:
self.player=screen.draw_rect(self.x,self.y,'grey')
def attack(self,screen):
self.atck=screen.draw_attack(self.x,self.y,'green')
def affiche_attack(self,screen):
self.atck=screen.draw_attack(self.x,self.y,'green')
def get_coords(self):
return (self.x,self.y)
def get_hp(self):
return self.hp
“health_bar”
from screen import *
import pygame,sys
class Health:
def __init__(self,hp_max,hp,screen):
self.hp_max=hp_max
self.current_hp=hp
self.width=150
self.height=35
self.hp_bar=pygame.Surface((self.width,self.height))
self.back=pygame.Surface((160,45))
def draw_bar(self,screen):
screen.ecran.blit(self.hp_bar,(20,20))
self.hp_bar.fill('black')
ratio=self.ratio_hp()
coord=(0,0,self.width*ratio,self.height)
self.hp_bar.fill('red',coord)
def bar_death(self,screen):
screen.ecran.blit(self.hp_bar,(20,20))
self.hp_bar.fill('black')
screen.refresh()
def update_hp(self,new_hp):
self.current_hp=new_hp
def ratio_hp(self):
return (self.current_hp/self.hp_max)
def background(self,screen):
screen.ecran.blit(self.back,(15,15))
self.back.fill('white')
“ennemy”
import pygame,random
from screen import *
from joueur import *
from math import sqrt
class Ennemi:
def __init__(self,x,y,screen):
self.hp=10
self.y=x
self.x=y
self.mob=screen.draw_rect(self.x,self.y,'blue')
self.atck=screen.draw_attack(self.x,self.y,'green')
def degats_recu(self,damage):
if self.hp>damage:
self.hp-=damage
return None
self.hp=0
return None
def distance(self,x0,y0,x1,y1):
return sqrt((x0-x1)**2+(y0-y1)**2)
def alive(self):
if self.hp>0:
return True
return False
def afficher_mob(self,screen):
if self.alive():
self.mob=screen.draw_rect(self.x,self.y,'blue')
#screen.draw_aggro(self.x,self.y,'red')
else:
self.mob=screen.draw_rect(self.x,self.y,'pink')
def roam(self,largeur,hauteur,screen):
if self.alive():
choix=random.randint(0,75)
if choix==0:
if self.x+30<=largeur:
self.x+=10
elif choix==1:
if self.x-10>=0:
self.x-=10
elif choix==2:
if self.y-10>=0:
self.y-=10
elif choix==3:
if self.y+30<=hauteur:
self.y+=10
self.mob=screen.draw_rect(self.x,self.y,'blue')
def get_coords(self):
return (self.x,self.y)
def player_inrange(self,player):
coords=player.get_coords()
if self.distance(self.x,self.y,coords[0],coords[1])<150:
return True
def player_inrange_attack(self,player):
coords=player.get_coords()
if self.distance(self.x,self.y,coords[0],coords[1])<100:
return True
def attack(self,screen):
if self.alive():
self.atck=screen.draw_attack(self.x,self.y,'green')
else:
self.atck=None
def aggro_player(self,player,largeur,hauteur,screen):
if self.alive():
coords=player.get_coords()
x,y=coords[0],coords[1]
if self.x<x:
if self.x+30<=largeur:
self.x+=0.5
elif self.x>x:
if self.x-10>=0:
self.x-=0.5
if self.y>y:
if self.y-10>=0:
self.y-=0.5
elif self.y<y:
if self.y+30<=hauteur:
self.y+=0.5
self.mob=screen.draw_rect(self.x,self.y,'blue')
“main”
import pygame,sys,time
from joueur import *
from screen import *
from ennemy import *
from health_bar import *
ecran=Screen(1280,720)
player=Player(ecran.width//2,ecran.height//2,ecran)
mob_1=Ennemi(100,100,ecran)
mob_2=Ennemi(100,1180,ecran)
liste_mob=[mob_1,mob_2]
hp_bar=Health(player.stats['hp'],player.hp,ecran)
class Game:
def __init__(self,width,height):
pygame.init()
self.width=width
self.height=height
self.clock=pygame.time.Clock()
def overlap_player_mob(self,mob):
if mob.alive():
if player.player.colliderect(mob.mob):
if player.check_invis()==True:
self.degats(5)
def overlap_attack(self,mob):
if mob.atck:
if player.player.colliderect(mob.atck):
if player.check_invis()==True:
self.degats(5)
def attack_overlapmob(self,mob):
if player.atck:
if mob.mob.colliderect(player.atck):
self.degats_mob(5,mob)
def degats(self,damage):
player.degats_recu(damage)
hp_bar.update_hp(player.hp)
def degats_mob(self,damage,mob):
mob.degats_recu(damage)
def fin_jeu(self):
ecran.clear()
police=pygame.font.Font('Font/Minecrafter.ttf',120)
texte=police.render('Game over',1,(255,0,0))
ecran.ecran.blit(texte,(240,320))
ecran.refresh()
def jeu(self):
start=time.time()
self.clock.tick(0)
pygame.key.set_repeat(15)
while player.en_vie():
ecran.clear()
hp_bar.background(ecran)
hp_bar.draw_bar(ecran)
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key==pygame.K_x:
player.attack(ecran)
if event.key==pygame.K_c:
if player.cd_dash():
player.dash(self.width,self.height,ecran)
player.cd_start()
else:
player.move(self.width,self.height,ecran,event)
for mob in liste_mob:
mob.afficher_mob(ecran)
if player.atck:
self.attack_overlapmob(mob)
if not mob.player_inrange(player):
mob.roam(self.width,self.height,ecran)
else:
mob.aggro_player(player,self.width,self.height,ecran)
if mob.player_inrange_attack(player):
mob.attack(ecran)
self.overlap_attack(mob)
self.overlap_player_mob(mob)
player.afficher_joueur(ecran)
end=time.time()
player.add_invis(end-start)
player.cd_add(end-start)
ecran.refresh()
self.fin_jeu()
gaming=Game(ecran.width,ecran.height)
ecran.refresh()
gaming.jeu()
I tried a lot of ways to display the map, none of them works for this project.. I managed to display my own map onto KidsCanCode’s project, but that wasn’t hard, here I’m stuck and I can’t manage to find any help..