#【メモ】 #3D座標は x = 0, y = 0が画面の中心で、z = 0が画面の手前。キャラクターのx, y座標はキャラクターの中心。 #大きさ調整は、自分のz座標を-10、スクリーンのz座標を0(自分とスクリーンの距離10)とした場合、キャラクターのz座標が20の場合には3((20+10)÷10)で割り、30の場合には4((30+10)÷10)で割る計算。 #キャラクターの座標は、自分とスクリーンの距離を10とした場合、キャラクターのz座標が30の場合には、画面の中心の座標から、xを4((30+10)÷10)で割ったものを足し、キャラクターの大きさの半分を4((30+10)÷10)で割ったものをxから引く。yも同様。 #視点の調整は「係数 * z」を x や y に足す(その x や y をさらに上記の通りz座標の奥行きに合わせて割り算する)。
def main(): """ メインルーチン """ #変数初期設定 a, b, c, d = 0, 0, 0, 0 x = 0 y = 100 z = 0 scroll_count = 0 adjust_x = 0 adjust_y = 0 frog_image = pygame.image.load("frog.gif") frog_size = 100 shadow_image = pygame.image.load("shadow.gif") shadow_size = 100
while True:
#画面塗りつぶし SURFACE.fill((0,0,0))
#ウィンドウ閉じるボタン for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit()
#キー入力判定 pressed = pygame.key.get_pressed() if pressed[K_LEFT] and x > -150: x -= 20 if pressed[K_RIGHT] and x < 150: x += 20 if pressed[K_UP] and y > -100: y -= 20 if pressed[K_DOWN] and y < 100: y += 20 if pressed[K_z] and z > -0: z -= 2 if pressed[K_x] and z < 200: z += 2 if pressed[K_c] and adjust_y > -10: adjust_y -= 0.2 if pressed[K_v] and adjust_y < 10: adjust_y += 0.2 if pressed[K_b] and adjust_x > -10: adjust_x -= 0.2 if pressed[K_n] and adjust_x < 10: adjust_x += 0.2
#背景スクロール scroll_count += 1 if scroll_count > 7: scroll_count = 0 for i in range(0-scroll_count, 200-scroll_count, 8): a, b = location_in_view(-200, -150, i, adjust_x, adjust_y) c, d = size_in_view(i, 400, 300) pygame.draw.rect(SURFACE, (150-int(i/1.5), 150-int(i/1.5), 180-int(i/1.5)), (a, b, c, d))
#影表示 c, d = size_in_view(z, shadow_size, shadow_size) shadow_image_3d = pygame.transform.scale(shadow_image, (c, d)) shadow_image.set_alpha(80) a, b = location_in_view(x, 145, z, adjust_x, adjust_y, shadow_size, shadow_size) SURFACE.blit(shadow_image_3d, (a, b))
#蛙表示 c, d = size_in_view(z, frog_size, frog_size) frog_image_3d = pygame.transform.scale(frog_image, (c, d)) a, b = location_in_view(x, y, z, adjust_x, adjust_y, frog_size, frog_size) print (a, b) SURFACE.blit(frog_image_3d, (a, b))