Skip to content

Commit

Permalink
Fix bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
KLPig committed May 23, 2024
1 parent 0e86ea1 commit d3e02ea
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 36 deletions.
3 changes: 2 additions & 1 deletion underpython/animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, pos=(0, 0), scale=1, tpf=3):
self.instant = ('NULL', 0)
self.pos = pos
self.nxt: dict[str, str] = {}
self.tpf = 3
self.tpf = tpf
self.timer = 3

def __call__(self, *args, **kwargs):
Expand Down Expand Up @@ -47,6 +47,7 @@ def find_ani_name(self, name: str) -> str:

def _update_frame(self):
name, no = self.instant
name = self.find_ani_name(name)
if no >= len(self.animations[name]) - 1:
self.change_animation(self.nxt[name])
else:
Expand Down
126 changes: 120 additions & 6 deletions underpython/attacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Attack:
enable_ul = True
is_attack = True

def calc_dmg(self, attacker):
return max(0, attacker.at * 5 - game.GAME.player.df * 3)
Expand All @@ -21,10 +22,12 @@ def __init__(self, position: tuple[int, int], graphic: pg.surface.Surface, rotat
self.remove = False
self.arr = pg.PixelArray(graphic)
self.org = pg.transform.scale_by(graphic, 3)
self.rot = 0
self.rot = rotation
self.set_img(graphic)
self.dis: pg.surface.Surface = self.org
self.rect = self.org.get_rect()
self.attr = {}
self.img = graphic
self.tick = 0
for k, v in kwargs.items():
self.attr[k] = v
Expand Down Expand Up @@ -81,6 +84,7 @@ def set_rotation(self, rotation: float):
self.rect.center = int(x), int(y)

def set_img(self, img: pg.Surface):
self.img = img
self.org = pg.transform.scale_by(img, 3)
self.set_rotation(self.rot)

Expand Down Expand Up @@ -132,11 +136,19 @@ def update(self):
self.attacks.remove(atk)
self.on_attack_removed(atk)
continue
if atk.collide_point(game.GAME.ui.souls.get_now().pos) and not game.GAME.player.wd:
game.GAME.player.hurt(atk.calc_dmg(self.attacker))
pos = game.GAME.ui.souls.get_now().pos
if type(game.GAME.ui.souls.get_now()) is CyanSoul:
pos = game.GAME.ui.souls.get_now().dis_pos
if atk.collide_point(pos) and not game.GAME.player.wd and atk.is_attack:
dmg = atk.calc_dmg(self.attacker)
_d = game.GAME.player.on_attacked(self.attacker, dmg)
if _d is not None:
dmg = _d
game.GAME.player.hurt(dmg)
if atk.enable_ul:
game.GAME.player.st_wd()
atk.on_action()
atk.set_img(atk.img)
atk.tick += 1
atk.dis.unlock()
game.GAME.displayer().blit(atk.dis, atk.rect)
Expand Down Expand Up @@ -248,13 +260,115 @@ def update(self):
self.move_pos((spd, 0))


class CyanSoul(Soul):
color = (0, 255, 255)
w = 100
h = 100

def __init__(self):
self.dis_pos = (0, 0)
super().__init__()

def update(self):
s = game.GAME.ui.soul_rect.rect
d = game.GAME.displayer()
self.set_pos(self.pos)
_x, _y = self.pos
self.pos = (round((_x - s.left - self.w // 2) / self.w) * self.w + s.left + self.w // 2,
round((_y - s.top - self.h // 2) / self.h) * self.h + s.top + self.h // 2)
x, y = self.pos
dx, dy = self.dis_pos
if abs(dx - x) < 10:
dx = x
else:
dx -= (dx - x) / 2
if abs(dy - y) < 10:
dy = y
else:
dy -= (dy - y) / 2
self.dis_pos = dx, dy
self.rect.center = dx, dy
if game.GAME.player.wd:
game.GAME.displayer().blit(self.unabled, self.rect)
else:
game.GAME.displayer().blit(self.surface, self.rect)
for x in range(s.left + self.w, s.right - self.w + 1, self.w):
pg.draw.line(d, (0, 255, 255), (x, s.top), (x, s.bottom), width=3)
for y in range(s.top + self.h, s.bottom - self.h + 1, self.h):
pg.draw.line(d, (0, 255, 255), (s.left, y), (s.right, y), width=3)
keys = game.GAME.key_events
if pg.K_UP in keys:
self.move_pos((0, -self.h))
elif pg.K_DOWN in keys:
self.move_pos((0, self.h))
if pg.K_LEFT in keys:
self.move_pos((-self.w, 0))
elif pg.K_RIGHT in keys:
self.move_pos((self.w, 0))


class BlueSoul(Soul):
color = (0, 0, 255)
direction = 0

def __init__(self):
super().__init__()
self.g = 0

def collide(self) -> bool:
s = game.GAME.ui.soul_rect.rect
if self.direction == 0:
dist = self.rect.bottom - s.bottom
elif self.direction == 1:
dist = self.rect.left - s.left
elif self.direction == 2:
dist = s.top - self.rect.top
else:
dist = s.right - self.rect.right
dist = abs(dist)
return dist < 20

def set_direction(self, _dir):
self.direction = _dir
self.g = 0

def update(self):
s = game.GAME.ui.soul_rect.rect
d = [(0, 1), (1, 0), (0, -1), (-1, 0)]
key = pg.key.get_pressed()
if key[pg.K_LEFT]:
ax, ay = d[(self.direction + 3) % 4]
self.move_pos((ax * 12, ay * 12))
elif key[pg.K_RIGHT]:
ax, ay = d[(self.direction + 1) % 4]
self.move_pos((ax * 12, ay * 12))
if key[pg.K_UP] and (self.collide() or self.g >= 0):
if self.collide():
self.g = 40
else:
if self.g >= 0 and not self.collide():
self.g = -5
elif self.collide():
self.g = 0
self.g -= 5
ax, ay = d[(self.direction + 2) % 4]
self.move_pos((ax * self.g, ay * self.g))
if game.GAME.player.wd:
game.GAME.displayer().blit(self.unabled, self.rect)
else:
game.GAME.displayer().blit(self.surface, self.rect)


class Souls:
def __init__(self):
self.souls = [RedSoul()]
self.souls = [RedSoul(), CyanSoul(), BlueSoul()]
self.now = 0

def get_now(self) -> RedSoul | Soul:
def get_now(self) -> RedSoul | CyanSoul | BlueSoul | Soul:
return self.souls[self.now]

def set_now(self, now):
def set_now(self, now, set_pos=True):
pos = self.get_now().pos
self.now = max(0, min(len(self.souls) - 1, now))
if set_pos:
self.get_now().pos = pos
18 changes: 17 additions & 1 deletion underpython/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
def empty_function(): pass


def function_of(cmds: list[str]):
def tmp():
for c in cmds:
eval(c)
return tmp


# Errors

class UnderPythonError(Exception):
Expand Down Expand Up @@ -46,7 +53,16 @@ def __init__(self, data: int):


class GameMethod(Constant):
pass
def __add__(self, other: Constant | None):
if other is None or other.data == 4:
return type(self)(self.data)
elif self.data == 4 or self.data == other.data:
return type(self)(other.data)
else:
return type(self)(4)

def __iadd__(self, other: Constant | None):
self.data = (self + other).data


PACIFIST_ROUTE = GameMethod(2)
Expand Down
20 changes: 17 additions & 3 deletions underpython/displayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


class Displayer:
size = (5000, 1000)

def __init__(self):
self.camera = (0, 0, 0)
if not pg.display.get_active():
Expand All @@ -19,15 +21,18 @@ def set_window(self):

def clear(self):
for i in range(len(self.surfaces)):
self.surfaces[i] = pg.surface.Surface((1280, 960))
self.surfaces[i] = pg.surface.Surface(self.size)

def _update(self):
self.window.fill((0, 0, 0))
x, y, r = self.camera
for surface in self.surfaces:
scale = min(self.window.get_width() / surface.get_width(),
self.window.get_height() / surface.get_height())
scale = min(self.window.get_width() / 1280,
self.window.get_height() / 960)
r_surf = pg.transform.scale_by(surface, scale)
r_surf_p = pg.PixelArray(r_surf)
r_surf = r_surf_p[-x:self.window.get_width() - x, -y:self.window.get_height() - y].make_surface()
del r_surf_p
r_surf = pg.transform.rotate(r_surf, r)
r_surf_rect = r_surf.get_rect()
r_surf_rect.center = (self.window.get_width() / 2 + x,
Expand Down Expand Up @@ -327,6 +332,7 @@ def __init__(self, save_button: bool = False):
self.speech_fonts = {name: font.Font() for name in self.names}
self.speeches = []
self.save = save_button
self.bp_dialog: UI.dialoger | None = None

def _attack_bar_shower(self, process: int, swap: bool = False):
r = self.soul_rect.rect
Expand Down Expand Up @@ -399,6 +405,8 @@ def _update(self):

gg = game.GAME
if gg.state == 'SELECT':
if self.bp_dialog is None and game.GAME.before_player_dialog is not None:
self.bp_dialog = self.dialoger(game.GAME.before_player_dialog, [], {})
self.soul_rect.exp_rect = pg.rect.Rect(40, 450, 1200, 300)
if self.buttons[self.selected].instant[0] == 'idle':
self.buttons[self.selected].change_animation('selected')
Expand Down Expand Up @@ -447,11 +455,15 @@ def _update(self):
self._state = 'item_e'
self.choose(gg.inventory.inventory, 'item_e')
else:
game.GAME.player.on_mercy()
for m in gg.monsters:
if m.spare_able:
m.defeat = base.PACIFIST_ROUTE
self._state = 'spare_ee'
else:
if self.bp_dialog is not None:
del self.bp_dialog
self.bp_dialog = None
for button in self.buttons:
button.change_animation('idle')

Expand Down Expand Up @@ -586,6 +598,8 @@ def _update(self):
elif len(sp.l_left):
self.speeches.append(self.speech(sp.font.name, sp.pos, sp.l_left[0], [], sp.arg))

if self.bp_dialog is not None:
self.bp_dialog.update()
if self._dialog is not None:
if self._dialog.end:
if type(self._dialog) is self.selector:
Expand Down
4 changes: 4 additions & 0 deletions underpython/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def render(self, surface: pg.Surface, pos, text: str, scale=5):
width = sum([self.chars[s].get_width() * scale for s in text]) + len(text) * 5 - 5
left -= width // 2
for s in text:
if s in ['q', 'y', 'p', 'g', 'j']:
y = pos[1] + 12
else:
y = pos[1]
surf = pg.transform.scale_by(self.chars[s], scale)
surf_r = surf.get_rect()
surf_r.bottomleft = left, y
Expand Down
8 changes: 7 additions & 1 deletion underpython/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, _player: player.Player, monsters: list[monster.Monster], wave
self.route: base.GameMethod | None = None
self.subrun = False
self.game_success = False
self.before_player_dialog: str | None = None

def _load_graphics(self):
path = os.path.join(self.rp, 'images')
Expand All @@ -60,7 +61,12 @@ def build(self):
self._load_sounds()
self.displayer.set_window()

def go(self, subrun=False):
def go(self, subrun=False, name=''):
print('Process: ', end='')
if subrun:
print('subprocess', name)
else:
print('root process', sys.modules['__main__'].__name__, end='')
self.subrun = subrun
self._loop()

Expand Down
38 changes: 23 additions & 15 deletions underpython/overworld/game.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import importlib
import os.path
from . import maps, player, ui
from underpython import displayer, base, chanel
from underpython import displayer, base, chanel, game
import pygame as pg
import sys
import os
import random


def load_image(res_path: str, name: str) -> pg.Surface:
return pg.image.load(os.path.join(os.path.join(res_path, 'images'), name + '.png'))


class Game:
Ts = 30

Expand All @@ -16,7 +20,6 @@ def __init__(self, resource_path: str, _player: player.Player, _map: maps.Map):
self.dis = displayer.Displayer()
self.map = _map
self.rp = resource_path
self.chara = []
self.player = _player
self.dis_camera = (0, 0)
self.tick = 0
Expand All @@ -42,6 +45,8 @@ def _update(self):
if not self.ui.pause:
self.player._move()
self.map.get_now()._update()
for c in self.map.get_now().chara:
c._update()
self.player._update()
self.ui.update()
self.dis._update()
Expand Down Expand Up @@ -89,21 +94,24 @@ def run_game(self, name):
pg.display.update()
while p.get_busy():
pass
game = importlib.import_module(name)
importlib.reload(game)
game.GAME.player.write_data(self.player.data)
game.GAME.inventory.write_data(self.player.inv)
game.GAME.go(True)
while not game.GAME.game_success:
importlib.reload(game)
game.GAME.player.write_data(self.player.data)
game.GAME.inventory.write_data(self.player.inv)
game.GAME.go(True)
self.player.data.write_data(game.GAME.player)
self.player.inv.write_data(game.GAME.inventory)
_game = importlib.import_module(name)
importlib.reload(_game)
gg = game.GAME
gg.player.write_data(self.player.data)
gg.inventory.write_data(self.player.inv)
gg.go(True, _game.__name__)
while not gg.game_success:
importlib.reload(_game)
gg.player.write_data(self.player.data)
gg.inventory.write_data(self.player.inv)
gg.go(True)
self.player.data.write_data(gg.player)
self.player.inv.write_data(gg.inventory)
chanel.MChanel.stop()
del game.GAME.player, game.GAME.inventory, game.GAME
self.st = pg.time.get_ticks() - self.Ts * (self.tick + 1)
return gg.route




def set_game(_game: Game):
Expand Down
Loading

0 comments on commit d3e02ea

Please sign in to comment.