I think we can use functions to make our code look good.. I'm learning pygame module these days.and I saw lots of experts say you should use OOP. But I did everything with functions not using classes. Just using 650-750 code lines. I don't know maybe I'm fool. please help me guys.what should I do.Not only for pygame....
If classes are good:
I can use them in the future.
else :
I will only use functions.
so this is my code i wrote to my player::
###################################################player#################################################
player_pos = (190,450)
#idle position
idle_1 = pygame.image.load('TRAP\player\\idle\\i1.png').convert_alpha()
idle_2 = pygame.image.load('TRAP\player\\idle\\i2.png').convert_alpha()
idle_3 = pygame.image.load('TRAP\player\\idle\\i2.png').convert_alpha()
idle_4 = pygame.image.load('TRAP\player\\idle\\i1.png').convert_alpha()
player_idle = [idle_1,idle_2,idle_3,idle_4]
player_index = 0
player_1 = player_idle[player_index]
player_rect = idle_1.get_rect(topleft= player_pos, size = (20,38))
def player_idle_anime():
global player_idle,player_index,player_1,player_rect
player_index += 0.1
player_1 = player_idle[int(player_index)]
if player_index >= 3:
player_index = 0
screen.blit(player_1,player_rect)
#player left position
left_1 = pygame.image.load('TRAP\player\left_down\l1.png').convert_alpha()
left_2 = pygame.image.load('TRAP\player\left_down\l2.png').convert_alpha()
left_3 = pygame.image.load('TRAP\player\left_down\l1.png').convert_alpha()
left_4 = pygame.image.load('TRAP\player\left_down\l3.png').convert_alpha()
move_list=[left_1,left_2,left_3]
move_index = 0
def player_move_anime(sprite_list):
global move_index,player_1,player_rect
move_index += 1
player_1 = sprite_list[int(move_index)]
screen.blit(player_1,player_rect)
if move_index>= (len(sprite_list)-1):
move_index = 0
#player right position
right_1 = pygame.image.load('TRAP\player\\right_up\\r1.png').convert_alpha()
right_2 = pygame.image.load('TRAP\player\\right_up\\r2.png').convert_alpha()
right_3 = pygame.image.load('TRAP\player\\right_up\\r1.png').convert_alpha()
right_4 = pygame.image.load('TRAP\player\\right_up\\r3.png').convert_alpha()
right_index = 0
right_list = [right_1,right_2,right_3]
#player up pos
up1 = pygame.image.load('TRAP\player\\right_up\\r1.png').convert_alpha()
up2 = pygame.image.load('TRAP\player\\right_up\\r2.png').convert_alpha()
back_list = [up1,up2,]
#player down pos
down1 = pygame.image.load('TRAP\player\\left_down\\l1.png').convert_alpha()
down2 = pygame.image.load('TRAP\player\\left_down\\l2.png').convert_alpha()
down_list = [down1,down2]
hor_index = 0
def player_hor_move_anime(sprite_list):
global hor_index,player_1,player_rect,g
hor_index += 0.4
player_1 = sprite_list[int(hor_index)]
screen.blit(player_1,player_rect)
if hor_index>= (len(sprite_list)-1):
hor_index = 0
if g == 0:
player_1 = sprite_list[0]
def player_side_idle(player_image):
screen.blit(player_image,player_rect)
#make a rect with x_ps,y_ps, width,height
def make_rect(dimention):
tile = pygame.Rect((dimention))
return tile
#draw the rect you make before
def rect_draw(color,rect_name):
pygame.draw.rect(screen,(color),rect_name)
#make sure are there any collide
def collison(player,object):
coll = []
for tile in object:
if player.colliderect(tile):
coll.append(tile)
return coll
#the full collider machanism
def move(player,object):
global g,v
player.x += v
coll = collison(player,object)
for tile in coll:
if v > 0:
player.right = (tile.left)
if v < 0:
player.left = tile.right
player.y += g
coll = collison(player,object)
for tile in coll:
if g > 0 :
player.bottom = tile.top
if g < 0:
player.top = tile.bottom
return player