I'm trying to make a game on pygame, it will be 2-d survival. i want to draw all my objects one by one. for example, if my 'y' is lower than the object's, then the object is drawn first and I'm on top of it. here's my main code:
import pygame
import random
from models.maps import *
from models.Basic import *
from models.Me import *
from models.Enemies import add_enemy,create_enemy
from models.Watch import Watch
from models.Objects import *
pygame.init()
pygame.display.set_caption('Dino Islands')
watch=Watch()
#ANIMATION WATER
water_choice=random.randint(-2,0)
water_animation_side='down'
water_animation_count=0
water_animation_time=pygame.USEREVENT+2
pygame.time.set_timer(water_animation_time,500)
def water_animation(its_x,its_y,id):
global water_animation_side,water_animation_count
sprite_image = sprites.get(id)['image']
if water_animation_side=='up':
water_animation_count-=0.002
else:
water_animation_count+=0.002
if water_animation_count<-9:
water_animation_side='down'
elif water_animation_count>9:
water_animation_side='up'
if sprite_image:
screen.blit(sprite_image[water_choice+1], ((its_x - camera_x)* TILE_SIZE, (its_y - camera_y) * TILE_SIZE-TILE_SIZE/2+water_animation_count+random.randint(-10,10)/10))
screen.blit(sprite_image[water_choice], ((its_x - camera_x)* TILE_SIZE, (its_y - camera_y) * TILE_SIZE+water_animation_count+water_animation_count+random.randint(-10,10)/10))
#DRAWING CURRENT MAP
def draw_map(my_map):
for y in range(int(camera_y), int(camera_y) + 9):
for x in range(int(camera_x), int(camera_x) + 14):
if -1 <= y < SIZE_H and 0 <= x < SIZE_W:
sprite_id = my_map[y][x]
if sprite_id!=3:
sprite_image = sprites.get(sprite_id)['image']
if sprite_image:
screen.blit(sprite_image, ((x - camera_x)* TILE_SIZE, (y - camera_y) * TILE_SIZE))
elif sprite_id==3:
water_animation(x,y,sprite_id)
running = True
while running:
keys=pygame.key.get_pressed()
m_click=pygame.mouse.get_pressed()
m_pos=pygame.mouse.get_pos()
clock.tick(60)
screen.fill((52, 152, 219))
#Main Game
if window=='game':
camera_x =max(2,min((camera_x-(camera_x-player.x)/30)-0.2,SIZE_W-2))
camera_y =max(2,min((camera_y-(camera_y-player.y)/30)-0.1,SIZE_H-2))
draw_map(lvl_1)
#ALL THE OBJECTS IN THE LIST (INCLUDE ME)
for object in objects:
object.draw(screen,camera_x,camera_y)
if player.image_cross_rect.colliderect(object.rect) and object.type=='tree':
object.hp-=player.skills['attack']
if object.type=='enemy':
object.move(player.x,player.y,object.speed)
if abs(object.x-player.x)<3 and abs(object.y-player.y)<3:
object.sees_player=True
else:
object.sees_player=False
if object.hp<=0:
objects.remove(object)
if player.rect.colliderect(object.rect):
player.hp-=object.attack
if object.type=='tree':
pass
drawing_y+=1
if drawing_y>HEIGHT:
drawing_y=0
player.control()
watch.show_time(screen)
inventory.draw(screen,(WIDTH-inventory.ile*inventory.scale)/2,HEIGHT-inventory.image.get_height()-10)
player.draw_ui(screen)
# if player.hp<=0:
# window='game_over'
pygame.display.flip()
for event in pygame.event.get():
if event.type==player.animation_count:
if player.animation_image==0:
player.animation_image=1
else:
player.animation_image=0
if player.position=='idle':
if player.direction=='right':
player.image=animation_player[player.animation_image]
if player.direction=='left':
player.image=animation_player[player.animation_image+2]
if player.direction=='up':
player.image=animation_player[player.animation_image+4]
if player.direction=='down':
player.image=animation_player[player.animation_image+6]
elif player.position=='walk':
if player.direction=='right':
player.image=animation_player[player.animation_image+8]
if player.direction=='left':
player.image=animation_player[player.animation_image+10]
if player.direction=='up':
player.image=animation_player[player.animation_image+12]
if player.direction=='down':
player.image=animation_player[player.animation_image+14]
if event.type==water_animation_time:
water_choice=random.randint(-2,0)
if event.type==add_enemy:
create_enemy()
if event.type==add_tree:
create_trees()
if event.type==pygame.MOUSEBUTTONDOWN:
if event.button==0:
print(1)
if event.button==5:
if player.item_in_hand<inventory.ile-1:
player.item_in_hand+=1
else:
player.item_in_hand=0
if event.button==4:
if player.item_in_hand>0:
player.item_in_hand-=1
else:
player.item_in_hand=inventory.ile-1
if event.type == pygame.QUIT:
running = False
pygame.quit()
But i even don't know how to make it. please help
I tried creating a variable "drawing_y" and checking if the drawy of the object is equal to "drawing_y". but it was wrong, the objects just were not drawn