Commits

Anonymous committed 991df99

Added bug entity and dumbest possible AI for it and a bug kill condition.

  • Participants
  • Parent commits 8daff13

Comments (0)

Files changed (1)

File roguelike/main.py

 import pygame
 import math
 import sys
+import random
 
 
 
 on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_RIGHT, lambda a: proc_event(pygame.event.Event(EVENT_IN_EAST)))
 on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_LEFT, lambda a: proc_event(pygame.event.Event(EVENT_IN_WEST)))
 
+EVENT_PLAYER_MOVED = 1004
+
 previous_hat = (0, 0)
 def hat_to_direction4(event):
 	global camera, previous_hat
 	#| pygame.DOUBLEBUF
 	#| pygame.HWSURFACE
 )
-pygame.display.set_caption('Roguelike v0.0.2')
+pygame.display.set_caption('Roguelike v0.0.3')
 
 
 
 
 
 
-TILE_W = 1
-TILE_L = .8
-TILE_H = .4
-
-
-# tile: (surface, (reg_x, reg_y), reg_width, filler, can_stand_on)
-tile_gray = (pygame.image.load('Plain Block.png').convert_alpha(), (50, 90), 100, True, True)
-tile_dirt = (pygame.image.load('Brown Block.png').convert_alpha(), (50, 90), 100, True, True)
-tile_grass = (pygame.image.load('Grass Block.png').convert_alpha(), (50, 90), 100, True, True)
-tile_water = (pygame.image.load('Water Block.png').convert_alpha(), (50, 90), 100, True, True)
-tile_rock = (pygame.image.load('Rock.png').convert_alpha(), (50, 90), 100, False, True)
-tile_tree = (pygame.image.load('Tree Tall.png').convert_alpha(), (50, 90), 100, False, False)
-tile_player = (pygame.image.load('Character Cat Girl.png').convert_alpha(), (50, 90), 100, False, False)
-
-shadow_e = (pygame.image.load('Shadow East.png'), (50, 90), 100)
-shadow_w = (pygame.image.load('Shadow West.png'), (50, 90), 100)
-shadow_s = (pygame.image.load('Shadow South.png'), (50, 90), 100)
-shadow_n = (pygame.image.load('Shadow North.png'), (50, 90), 100)
-shadow_side_w = (pygame.image.load('Shadow Side West.png'), (50, 90), 100)
-shadow_se = (pygame.image.load('Shadow South East.png'), (50, 90), 100)
-shadow_nw = (pygame.image.load('Shadow North West.png'), (50, 90), 100)
-shadow_sw = (pygame.image.load('Shadow South West.png'), (50, 90), 100)
-shadow_ne = (pygame.image.load('Shadow North East.png'), (50, 90), 100)
-
 
 #camera = (0, 0, 6, 4) # (left, top, width, height)
 camera = (0, 0, 7) # (centerX, centerY, height) ~~~ width depends on screen
 def zoom_camera(delta):
 	global camera
 	camera = (camera[0], camera[1], max(1, camera[2] + delta))
+TILE_W = 1
+TILE_L = .8
+TILE_H = .4
 
-blocks_changed = False
+
+# tile: (surface, (reg_x, reg_y), reg_width, is_filler)        , can_stand_on, can_climb)
+tile_gray = (pygame.image.load('Plain Block.png').convert_alpha(), (50, 90), 100, True)#, True, True)
+tile_dirt = (pygame.image.load('Brown Block.png').convert_alpha(), (50, 90), 100, True)#, True, True)
+tile_grass = (pygame.image.load('Grass Block.png').convert_alpha(), (50, 90), 100, True)#, True, True)
+tile_water = (pygame.image.load('Water Block.png').convert_alpha(), (50, 90), 100, True)#, True, True)
+tile_rock = (pygame.image.load('Rock.png').convert_alpha(), (50, 90), 100, False)#, True, False)
+tile_tree = (pygame.image.load('Tree Tall.png').convert_alpha(), (50, 90), 100, False)#, False, False)
+tile_player = (pygame.image.load('Character Cat Girl.png').convert_alpha(), (50, 90), 100, False)#, False, False)
+tile_bug = (pygame.image.load('Enemy Bug.png').convert_alpha(), (50, 90), 100, False)#, False, False)
+
+shadow_e = (pygame.image.load('Shadow East.png'), (50, 90), 100)
+shadow_w = (pygame.image.load('Shadow West.png'), (50, 90), 100)
+shadow_s = (pygame.image.load('Shadow South.png'), (50, 90), 100)
+shadow_n = (pygame.image.load('Shadow North.png'), (50, 90), 100)
+shadow_side_w = (pygame.image.load('Shadow Side West.png'), (50, 90), 100)
+shadow_se = (pygame.image.load('Shadow South East.png'), (50, 90), 100)
+shadow_nw = (pygame.image.load('Shadow North West.png'), (50, 90), 100)
+shadow_sw = (pygame.image.load('Shadow South West.png'), (50, 90), 100)
+shadow_ne = (pygame.image.load('Shadow North East.png'), (50, 90), 100)
+
+
+#def get_blocks(predicate):
+#	for block in blocks:
+#		if predicate(block):
+#			yield block
+#def get_blocks_at(position):
+#	yield from get_blocks(lambda a: a[0] == position)
+
+
+"""def is_filler_at(position):
+	if is_block_at(position):
+		if position_to_block[position][1][3]:
+			return True
+	return False
+def is_block_at(position):
+	return position in position_to_block
+
+def move_block(block, delta):
+	block[0] = sum3d(block[0], delta)
+	
+	global blocks_changed
+	blocks_changed = True
+
+def can_stand_on(position):
+	if position in position_to_block:
+		if position_to_block[position][1][4]:
+			return True
+	return False
+def can_climb(position):
+	if not position in position_to_block: return False
+	
+	return position_to_block[position][1][5]
+###"""
+
+
 blocks = [] # contains: ((x, y, z), tile)
 position_to_block = {}
+#blocks_changed = True
+def update_blocks():
+	#global blocks_changed
+	blocks.sort(key=lambda a: a[0][2] * 10000000 + a[0][1])
+	position_to_block.clear()
+	for block in blocks:
+		position_to_block[block[0]] = block
+	#blocks_changed = False
+
+def remove_block_at(position):
+	global blocks
+	for i, block in enumerate(blocks):
+		if block[0] == position:
+			blocks = blocks[:i] + blocks[i + 1:]
+			break
+	del position_to_block[position]
+	#blocks.remove(value)
+	
+def is_block_at(position):
+	return position in position_to_block
+
+def move_block_to(block, dest):
+	block[0] = dest
+	
+	#global blocks_changed
+	#blocks_changed = True
+	update_blocks()
+	
+def move_block(block, delta):
+	move_block_to(block, sum3d(block[0], delta))
+
+def get_block_type_at(position):
+	if position in position_to_block:
+		return position_to_block[position][2]
+	else:
+		return None
+	
+def move_player(block, delta):
+	dest = sum3d(block[0], delta)
+	above = sum3d(dest, (0, 1, 0))
+	#below = sum3d(dest, (0, -1, 0))
+	
+	if is_block_at(dest):
+		if is_block_at(above):
+			return
+		elif get_block_type_at(dest) == BlockType.FILLER:
+			move_block(block, sum3d(delta, (0, 1, 0)))
+			
+			proc_event(pygame.event.Event(EVENT_PLAYER_MOVED))
+		return
+	else:
+		for i in range(1, 5):
+			below = sum3d(dest, (0, -i, 0))
+			if get_block_type_at(below) == None:
+				continue
+			if get_block_type_at(below) in (BlockType.FILLER, BlockType.ROCK):
+				move_block(block, sum3d(delta, (0, 1 - i, 0)))
+				proc_event(pygame.event.Event(EVENT_PLAYER_MOVED))
+			if get_block_type_at(below) in (BlockType.BUG,):
+				remove_block_at(below)
+				
+				move_block(block, sum3d(delta, (0, -i, 0)))
+				proc_event(pygame.event.Event(EVENT_PLAYER_MOVED))
+			return
+		
+def move_bug(block, delta):
+	dest = sum3d(block[0], delta)
+	above = sum3d(dest, (0, 1, 0))
+	#below = sum3d(dest, (0, -1, 0))
+	
+	if is_block_at(dest):
+		#if is_block_at(above):
+		#	return
+		#elif get_block_type_at(dest) == BlockType.FILLER:
+		#	move_block(block, sum3d(delta, (0, 1, 0)))
+		#	
+		#	#proc_event(pygame.event.Event(EVENT_PLAYER_MOVED))
+		return
+	else:
+		
+		#for i in range(1, 5):
+		below = sum3d(dest, (0, -1, 0))
+		if get_block_type_at(below) == None:
+			#continue
+			return
+		if get_block_type_at(below) in (BlockType.FILLER, BlockType.ROCK, BlockType.BUG):
+			move_block(block, delta)
+			#proc_event(pygame.event.Event(EVENT_PLAYER_MOVED))
+		return
+
+def ai_bug(block):
+	move_bug(block, random.choice(((1, 0, 0), (-1, 0, 0), (0, 0, 1), (0, 0, -1))))
+
+on_event(lambda a: a.type == EVENT_PLAYER_MOVED, lambda a: ai_bug(block_bug))
+
+#def move_player_and_camera(block, delta):
+#	if move_player(block, delta):
+#		center_camera_on_block(block)
+def center_camera_on_block(block):
+	move_camera_to((block[0][0], block[0][2]))
+on_event(lambda a: a.type == EVENT_PLAYER_MOVED, lambda a: center_camera_on_block(block_player))
+
+class BlockType:
+	PLAYER, BUG, ROCK, TREE, FILLER = range(5)
+
+
+blocks.append([(1, 1, 2), tile_gray, BlockType.FILLER])
+blocks.append([(1, 1, 1), tile_gray, BlockType.FILLER])
+blocks.append([(1, 0, 1), tile_gray, BlockType.FILLER])
+blocks.append([(1, 0, 2), tile_gray, BlockType.FILLER])
+blocks.append([(0, 0, 0), tile_gray, BlockType.FILLER])
+blocks.append([(1, 0, 0), tile_gray, BlockType.FILLER])
+blocks.append([(3, 0, 2), tile_gray, BlockType.FILLER])
+blocks.append([(2, 0, 1), tile_dirt, BlockType.FILLER])
+blocks.append([(2, 0, 2), tile_dirt, BlockType.FILLER])
+blocks.append([(2, 1, 1), tile_grass, BlockType.FILLER])
+blocks.append([(2, 1, 2), tile_grass, BlockType.FILLER])
+blocks.append([(1, 0, 3), tile_grass, BlockType.FILLER])
+blocks.append([(1, 0, 4), tile_water, BlockType.FILLER])
+blocks.append([(0, 0, 4), tile_water, BlockType.FILLER])
+blocks.append([(1, 0, 5), tile_dirt, BlockType.FILLER])
+blocks.append([(0, -1, 5), tile_dirt, BlockType.FILLER])
+blocks.append([(0, 0, -2), tile_dirt, BlockType.FILLER])
+blocks.append([(0, 0, 2), tile_dirt, BlockType.FILLER])
+blocks.append([(0, 1, 0), tile_rock, BlockType.ROCK])
+blocks.append([(2, 2, 1), tile_tree, BlockType.TREE])
+blocks.append([(0, 1, -1), tile_gray, BlockType.FILLER])
+blocks.append([(-1, 2, -1), tile_gray, BlockType.FILLER])
+blocks.append([(-1, 1, -1), tile_gray, BlockType.FILLER])
+blocks.append([(-1, 0, -1), tile_gray, BlockType.FILLER])
+blocks.append([(-1, -1, 0), tile_dirt, BlockType.FILLER])
+blocks.append([(-1, -1, 1), tile_water, BlockType.FILLER])
+blocks.append([(0, -1, 1), tile_water, BlockType.FILLER])
+blocks.append([(2, 1, -1), tile_grass, BlockType.FILLER])
+blocks.append([(2, 0, 0), tile_dirt, BlockType.FILLER])
+blocks.append([(3, 0, 0), tile_dirt, BlockType.FILLER])
+blocks.append([(1, 0, -1), tile_dirt, BlockType.FILLER])
+blocks.append([(3, 1, 1), tile_gray, BlockType.FILLER])
+blocks.append([(3, 2, 1), tile_dirt, BlockType.FILLER])
+blocks.append([(3, 3, 1), tile_grass, BlockType.FILLER])
+blocks.append([(3, 2, 2), tile_grass, BlockType.FILLER])
+blocks.append([(3, 1, 2), tile_dirt, BlockType.FILLER])
+blocks.append([(3, 1, -1), tile_dirt, BlockType.FILLER])
+
+block_player = [(1, 1, 0), tile_player, BlockType.PLAYER]
+center_camera_on_block(block_player)
+blocks.append(block_player)
+
+block_bug = [(1, 1, -1), tile_bug, BlockType.BUG]
+blocks.append(block_bug)
+
+update_blocks()
+
+
+
+scale_cache = {} # { surface: ((x, y), result_surface) }
+def scale_surface_to(surface, dest_size):
+	#return pygame.transform.smoothscale(surface, dest_size)
+	#return pygame.transform.scale(surface, dest_size)
+	if surface in scale_cache:
+		#print('Found ' + str(surface))
+		if scale_cache[surface][0] == dest_size:
+			#print('Found ' + str(dest_size))
+			return scale_cache[surface][1]
+		else:
+			print('Did not find ' + str(dest_size))
+	scale_cache[surface] = (dest_size, pygame.transform.smoothscale(surface, dest_size))
+	return scale_cache[surface][1]
+
 def draw_tile(tile, position):
 	tile_surface = tile[0]
 	tile_reg_width = tile[2]
 	dest_w = scale_world2screen * scale_image2tile
 	dest_h = dest_w * tile_surface.get_height() / tile_surface.get_width()
 	surface_screen.blit(
-		pygame.transform.smoothscale(tile_surface, int2d((dest_w, dest_h))),
+		#pygame.transform.smoothscale(tile_surface, int2d((dest_w, dest_h))),
+		scale_surface_to(tile_surface, int2d((dest_w, dest_h))),
 		sum2d(
 			### positioning before camera motion
 			dif2d(
 		)
 	)
 def draw_environment(event):
-	global blocks_changed
-	if blocks_changed:
-		blocks.sort(key=lambda a: a[0][2] * 10000000 + a[0][1])
-		position_to_block.clear()
-		for block in blocks:
-			position_to_block[block[0]] = block
-		blocks_changed = False
+	def is_filler_at(position):
+		if not is_block_at(position): return False
+		return position_to_block[position][1][3]
+	
+	#if blocks_changed:
+	#	update_blocks()
 	
 	for block in blocks:
 		tile = block[1]
 	#pygame.draw.rect(surface_screen, colorHud, pygame.Rect(300 - 2, 200 - 2, 4, 4)) # centered guide dot
 on_event(lambda a: a.type == EVENT_DRAW, draw_environment)
 
-#def get_blocks(predicate):
-#	for block in blocks:
-#		if predicate(block):
-#			yield block
-#def get_blocks_at(position):
-#	yield from get_blocks(lambda a: a[0] == position)
-def is_filler_at(position):
-	if is_block_at(position):
-		if position_to_block[position][1][3]:
-			return True
-	return False
-def is_block_at(position):
-	return position in position_to_block
 
-def move_block(block, delta):
-	block[0] = sum3d(block[0], delta)
-	
-	global blocks_changed
-	blocks_changed = True
 
-def can_stand_on(position):
-	if position in position_to_block:
-		if position_to_block[position][1][4]:
-			return True
-	return False
-def move_player(block, delta):
-	dest = sum3d(block[0], delta)
-	above = sum3d(dest, (0, 1, 0))
-	below = sum3d(dest, (0, -1, 0))
-	
-	if is_block_at(dest):
-		if is_block_at(above):
-			return False
-		elif can_stand_on(dest):
-			move_block(block, sum3d(delta, (0, 1, 0)))
-			return True
-		else:
-			return False
-	else:
-		if is_block_at(below):
-			if can_stand_on(below):
-				move_block(block, delta)
-				return True
-			else:
-				return False
-		
-		### just using an arbitrary search depth for now
-		for i in range(2, 10):
-			if is_block_at(sum3d(dest, (0, -i, 0))):
-				if can_stand_on(sum3d(dest, (0, -i, 0))):
-					move_block(block, sum3d(delta, (0, 1 - i, 0)))
-					return True
-				else:
-					return False
-		return False
 
-def move_player_and_camera(block, delta):
-	if move_player(block, delta):
-		move_camera_to((block[0][0], block[0][2]))
-
-
-
-blocks.append([(1, 1, 2), tile_gray])
-blocks.append([(1, 1, 1), tile_gray])
-blocks.append([(1, 0, 1), tile_gray])
-blocks.append([(1, 0, 2), tile_gray])
-blocks.append([(0, 0, 0), tile_gray])
-blocks.append([(1, 0, 0), tile_gray])
-blocks.append([(3, 0, 2), tile_gray])
-blocks.append([(2, 0, 1), tile_dirt])
-blocks.append([(2, 0, 2), tile_dirt])
-blocks.append([(2, 1, 1), tile_grass])
-blocks.append([(2, 1, 2), tile_grass])
-blocks.append([(1, 0, 3), tile_grass])
-blocks.append([(1, 0, 4), tile_water])
-blocks.append([(0, 0, 4), tile_water])
-blocks.append([(1, 0, 5), tile_dirt])
-blocks.append([(0, -1, 5), tile_dirt])
-blocks.append([(0, 0, -2), tile_dirt])
-blocks.append([(0, 0, 2), tile_dirt])
-blocks.append([(0, 1, 0), tile_rock])
-blocks.append([(2, 2, 1), tile_tree])
-blocks.append([(0, 1, -1), tile_gray])
-blocks.append([(-1, 2, -1), tile_gray])
-blocks.append([(-1, 1, -1), tile_gray])
-blocks.append([(-1, 0, -1), tile_gray])
-blocks.append([(-1, -1, 0), tile_dirt])
-blocks.append([(-1, -1, 1), tile_water])
-blocks.append([(0, -1, 1), tile_water])
-blocks.append([(2, 1, -1), tile_grass])
-blocks.append([(2, 0, 0), tile_dirt])
-blocks.append([(3, 0, 0), tile_dirt])
-blocks.append([(1, 0, -1), tile_dirt])
-blocks.append([(3, 1, 1), tile_gray])
-blocks.append([(3, 2, 1), tile_dirt])
-blocks.append([(3, 3, 1), tile_grass])
-blocks.append([(3, 2, 2), tile_grass])
-blocks.append([(3, 1, 2), tile_dirt])
-
-block_player = [(0, 1, 0), tile_player]
-blocks.append(block_player)
-
-blocks_changed = True
-
-
-
-on_event(lambda a: a.type == EVENT_IN_NORTH, lambda a: move_player_and_camera(block_player, (0, 0, -1)))
-on_event(lambda a: a.type == EVENT_IN_SOUTH, lambda a: move_player_and_camera(block_player, (0, 0, 1)))
-on_event(lambda a: a.type == EVENT_IN_EAST, lambda a: move_player_and_camera(block_player, (1, 0, 0)))
-on_event(lambda a: a.type == EVENT_IN_WEST, lambda a: move_player_and_camera(block_player, (-1, 0, 0)))
+on_event(lambda a: a.type == EVENT_IN_NORTH, lambda a: move_player(block_player, (0, 0, -1)))
+on_event(lambda a: a.type == EVENT_IN_SOUTH, lambda a: move_player(block_player, (0, 0, 1)))
+on_event(lambda a: a.type == EVENT_IN_EAST, lambda a: move_player(block_player, (1, 0, 0)))
+on_event(lambda a: a.type == EVENT_IN_WEST, lambda a: move_player(block_player, (-1, 0, 0)))
 
 on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_EQUALS, lambda a: zoom_camera(-1))
 on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_MINUS, lambda a: zoom_camera(1))