1+ import random
2+ from math import *
3+ import sys
4+ sys .path .append ('/Games/th-umb' )
5+ class Logic :
6+ def __init__ (self ,g ,e ,s ):self .gLogic = g ;self .entities = e ;self .sprites = s
7+ @micropython .native
8+ def entityChecker (self ,invSlot ):
9+ for entity in self .entities :
10+ if entity [2 ]== 0 :self .fish (self .entities .index (entity ))
11+ elif entity [2 ]== 1 :self .bull (self .entities .index (entity ))
12+ elif entity [2 ]== 2 :self .trader (self .entities .index (entity ),invSlot )
13+ elif entity [2 ]== 3 :self .zombie (self .entities .index (entity ))
14+ elif entity [2 ]== 4 :self .seed (self .entities .index (entity ))
15+ @micropython .native
16+ def seed (self ,i ):
17+ entity = self .entities [i ];entity [3 ][1 ]+= 1
18+ if entity [3 ][1 ]> 2 :entity [3 ][1 ]= 0 ;entity [3 ][0 ]+= 1
19+ if entity [3 ][0 ]> 10 :self .gLogic .world [entity [1 ]* 20 + entity [0 ]]= random .randint (1 ,2 );self .entities .remove (entity )
20+ xTree = entity [0 ]* 6 + self .gLogic .cInt [0 ];yTree = entity [1 ]* 8 + self .gLogic .cInt [1 ];self .gLogic .display .drawFilledRectangle (xTree ,yTree ,5 ,7 ,0 );self .gLogic .display .drawText ('s' ,xTree ,yTree ,2 )
21+ @micropython .native
22+ def zombie (self ,i ):
23+ entity = self .entities [i ];zombie_sprite = self .sprites .zombie
24+ if - 1 >= zombie_sprite .x >= 72 or - 1 >= zombie_sprite .y >= 40 :return
25+ radius = [self .gLogic .pInt [0 ]- entity [0 ],self .gLogic .pInt [1 ]- entity [1 ]]
26+ if entity [3 ]> 0 :entity [3 ]-= 1
27+ newTx = newTy = 0
28+ if 5 > radius [0 ]> - 5 and 5 > radius [1 ]> - 5 :
29+ newTx = entity [0 ];newTy = entity [1 ]
30+ if radius [0 ]> 0 :newTx += 1
31+ elif radius [0 ]< - 0 :newTx -= 1
32+ if radius [1 ]> 0 :newTy += 1
33+ elif radius [1 ]< - 0 :newTy -= 1
34+ ww = 20 ;wh = len (self .gLogic .world )// ww
35+ if 0 <= newTx < ww and 0 <= newTy < wh :
36+ if entity [3 ]<= 0 and self .gLogic .world [newTy * ww + newTx ]not in {1 ,2 ,3 ,5 ,7 ,8 ,12 ,13 ,14 ,16 ,17 ,21 }:entity [0 ]= newTx ;entity [1 ]= newTy ;entity [3 ]= 5
37+ if radius [0 ]== 0 and radius [1 ]== 0 :self .gLogic .hp ()
38+ zombie_sprite .x = entity [0 ]* 6 + self .gLogic .cInt [0 ];zombie_sprite .y = entity [1 ]* 8 + self .gLogic .cInt [1 ];self .gLogic .display .drawSprite (zombie_sprite )
39+ @micropython .native
40+ def trader (self ,i ,invSlot ):
41+ entity = self .entities [i ];trader_sprite = self .sprites .trader
42+ if - 1 >= trader_sprite .x >= 72 or - 1 >= trader_sprite .y >= 40 :return
43+ radius = [self .gLogic .pInt [0 ]- entity [0 ],self .gLogic .pInt [1 ]- entity [1 ]];entity [3 ]= 0
44+ if entity [4 ]> 0 :entity [4 ]-= 1
45+ newTx = newTy = 0
46+ if invSlot != 0 :
47+ if (5 > radius [0 ]> - 5 and 5 > radius [1 ]> - 5 )and invSlot [0 ]in {5 ,6 }:
48+ entity [3 ]= 1 ;newTx = entity [0 ];newTy = entity [1 ]
49+ if radius [0 ]> 1 :newTx += 1
50+ elif radius [0 ]< - 1 :newTx -= 1
51+ if radius [1 ]> 1 :newTy += 1
52+ elif radius [1 ]< - 1 :newTy -= 1
53+ ww = 20 ;wh = len (self .gLogic .world )// ww
54+ if 0 <= newTx < ww and 0 <= newTy < wh :
55+ if entity [4 ]<= 0 and self .gLogic .world [newTy * ww + newTx ]not in {1 ,2 ,3 ,5 ,7 ,8 ,12 ,13 ,14 ,16 ,17 ,21 }:entity [0 ]= newTx ;entity [1 ]= newTy ;entity [4 ]= 5
56+ if (radius [0 ]== 0 and radius [1 ]== 0 )and invSlot [0 ]in {5 ,6 }:
57+ invSlot [1 ]-= 1 ;rand = random .randint (1 ,8 )
58+ for i in range (random .randint (1 ,2 )):
59+ if rand not in {5 ,6 }:self .gLogic .mine_place (rand )
60+ self .gLogic .pInt = [random .randint (self .gLogic .pInt [0 ]- 1 ,self .gLogic .pInt [0 ]+ 1 ),random .randint (self .gLogic .pInt [1 ]- 1 ,self .gLogic .pInt [1 ]+ 1 )]
61+ trader_sprite .x = entity [0 ]* 6 + self .gLogic .cInt [0 ];trader_sprite .y = entity [1 ]* 8 + self .gLogic .cInt [1 ];trader_sprite .setFrame (entity [3 ]);self .gLogic .display .drawSprite (trader_sprite )
62+ @micropython .native
63+ def bull (self ,i ):
64+ entity = self .entities [i ];bull_sprite = self .sprites .bull
65+ if - 1 >= bull_sprite .x >= 72 or - 1 >= bull_sprite .y >= 40 :return
66+ if entity [3 ]== 0 :
67+ player_x ,player_y = self .gLogic .pInt [0 ],self .gLogic .pInt [1 ];bull_x ,bull_y = entity [0 ],entity [1 ];directions = [(1 ,0 ),(- 1 ,0 ),(0 ,1 ),(0 ,- 1 )]
68+ for (dx ,dy )in directions :
69+ for dist in range (1 ,5 ):
70+ new_x = bull_x + dx * dist ;new_y = bull_y + dy * dist
71+ if new_x == player_x and new_y == player_y :entity [4 ]= [dx ,dy ];entity [3 ]= 1 ;break
72+ elif entity [3 ]== 1 :
73+ move_x ,move_y = entity [4 ][0 ],entity [4 ][1 ];new_bull_x = entity [0 ]+ move_x ;new_bull_y = entity [1 ]+ move_y
74+ if (new_bull_x < 0 or new_bull_x >= 20 )or (new_bull_y < 0 or new_bull_y >= 10 ):entity [3 ]= 0 ;return
75+ if self .gLogic .world [new_bull_y * 20 + new_bull_x ]not in {1 ,2 ,3 ,5 ,7 ,8 ,12 ,13 ,14 ,16 ,17 ,21 }:entity [0 ],entity [1 ]= new_bull_x ,new_bull_y
76+ else :entity [3 ]= 0
77+ if entity [0 ]== self .gLogic .pInt [0 ]and entity [1 ]== self .gLogic .pInt [1 ]:self .gLogic .hp ()
78+ bull_sprite .x = entity [0 ]* 6 + self .gLogic .cInt [0 ];bull_sprite .y = entity [1 ]* 8 + self .gLogic .cInt [1 ];bull_sprite .setFrame (entity [3 ]);self .gLogic .display .drawSprite (bull_sprite )
79+ @micropython .native
80+ def fish (self ,i ):
81+ entity = self .entities [i ];entity [3 ]-= 1 ;fish_sprite = self .sprites .fish
82+ if - 1 >= fish_sprite .x >= 72 or - 1 >= fish_sprite .y >= 40 :return
83+ if entity [3 ]<= 0 :
84+ entity [3 ]= 10 ;directions = [(1 ,0 ),(- 1 ,0 ),(0 ,1 ),(0 ,- 1 )];move = []
85+ for (dx ,dy )in directions :
86+ new_x = entity [0 ]+ dx ;new_y = entity [1 ]+ dy
87+ if 0 <= new_x <= 19 and 0 <= new_y <= 9 and self .gLogic .world [new_y * 20 + new_x ]== 3 :move .append ((new_x ,new_y ))
88+ if move :self .gLogic .counting ();random .seed (self .gLogic .counter );rand_move = random .choice (move );entity [0 ],entity [1 ]= rand_move
89+ fish_sprite .x = entity [0 ]* 6 + self .gLogic .cInt [0 ];fish_sprite .y = entity [1 ]* 8 + self .gLogic .cInt [1 ];fish_sprite .setFrame (1 if entity [3 ]> 7 else 0 );self .gLogic .display .drawSprite (fish_sprite )
0 commit comments