fps independant

This commit is contained in:
Waylon Walker 2023-01-12 20:51:47 -06:00
parent a2ffbd4123
commit 21b631ec2a
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
2 changed files with 83 additions and 44 deletions

View file

@ -40,7 +40,7 @@ class MouseSprite:
if self.hotbar.selected.type is None: if self.hotbar.selected.type is None:
pygame.draw.rect(self.surf, (255, 0, 0), self.rect) pygame.draw.rect(self.surf, (255, 0, 0), self.rect)
else: else:
self.img = self.game.get_img(self.hotbar.selected.type) self.img = self.game.get_img(self.hotbar.selected.type).convert()
self.surf.blit( self.surf.blit(
pygame.transform.scale(self.img, (64, 64)), pygame.transform.scale(self.img, (64, 64)),
self.get_nearest_block_pos(), self.get_nearest_block_pos(),
@ -284,6 +284,22 @@ class DebugMenu(Menu):
), ),
(10, 95), (10, 95),
) )
self.surf.blit(
self.font.render(
f"fullscreen: {pygame.FULLSCREEN}",
True,
(255, 255, 255),
),
(10, 110),
)
self.surf.blit(
self.font.render(
f"elapsed: {self.game.elapsed}",
True,
(255, 255, 255),
),
(10, 125),
)
class LightSource: class LightSource:
@ -304,10 +320,11 @@ class Leaf:
self.sx, self.sy = center self.sx, self.sy = center
self.img = pygame.transform.scale( self.img = pygame.transform.scale(
self.game.get_img("leaf"), (4, 4) self.game.get_img("leaf"), (4, 4)
).convert_alpha() ) # .convert_alpha()
self.lifespan = lifespan self.lifespan = lifespan
self.r = random.randint(0, 360) self.r = random.randint(0, 360)
self.x, self.y = [int(i) + random.randint(-8, 8) for i in self.center] self.x, self.y = [int(i) + random.randint(-8, 8) for i in self.center]
self.speed = 3
self.restart() self.restart()
def restart(self): def restart(self):
@ -323,13 +340,13 @@ class Leaf:
) )
if self.y < self.sy + 40: if self.y < self.sy + 40:
self.y += random.randint(0, 5) / 4 self.y += random.randint(0, 5) / 4 * self.speed * self.game.elapsed
self.x += random.randint(-15, 5) / 10 self.x += random.randint(-15, 5) / 10 * self.speed * self.game.elapsed
self.r += random.randint(-10, 10) self.r += random.randint(-10, 10) * self.speed / 3 * self.game.elapsed
elif self.y < self.sy + 45: elif self.y < self.sy + 45:
self.y += random.randint(-2, 5) / 10 self.y += random.randint(-2, 5) / 10 * self.speed * self.game.elapsed
self.x += random.randint(-18, 2) / 10 self.x += random.randint(-18, 2) / 10 * self.speed * self.game.elapsed
self.r += random.randint(-10, 25) self.r += random.randint(-10, 25) * self.speed * self.game.elapsed
else: else:
self.restart() self.restart()
if self.x > self.sx + 100: if self.x > self.sx + 100:
@ -357,16 +374,20 @@ class Bee:
class Creeper(Game): class Creeper(Game):
def __init__(self, debug=False): def __init__(self, debug=False):
super().__init__() super().__init__()
self.inventory = {} pygame.mouse.set_visible(False)
self.inventory = {"plank-bottom": 1, "plank-top": 1}
self._imgs = {}
self.blocks = {}
self.camera = (0, 0) self.camera = (0, 0)
self.day_len = 1000 * 60 self.day_len = 1000 * 60
self.background = pygame.Surface(self.screen.get_size()) self.background = pygame.Surface(self.screen.get_size())
self.foreground = pygame.Surface(self.screen.get_size()) self.foreground = pygame.Surface(self.screen.get_size())
self.build = pygame.Surface(self.screen.get_size()) self.build = pygame.Surface(self.screen.get_size())
self.darkness = pygame.Surface(self.screen.get_size()).convert_alpha() # self.darkness = pygame.Surface(self.screen.get_size())
self.axe_sound = pygame.mixer.Sound(ASSETS / "sounds/axe.mp3") self.axe_sound = pygame.mixer.Sound(ASSETS / "sounds/axe.mp3")
self.walking_sound = pygame.mixer.Sound(ASSETS / "sounds/walking.mp3") self.walking_sound = pygame.mixer.Sound(ASSETS / "sounds/walking.mp3")
self.walking_sound_is_playing = False self.walking_sound_is_playing = False
self.speed = 5
self.background.fill((0, 255, 247)) self.background.fill((0, 255, 247))
self.x, self.y = [i / 2 for i in self.screen.get_size()] self.x, self.y = [i / 2 for i in self.screen.get_size()]
@ -395,20 +416,21 @@ class Creeper(Game):
self.trees = [] self.trees = []
x_range = [ x_range = [
self.screen.get_size()[0] * 0.1, 0,
self.screen.get_size()[0] * 0.9, self.screen.get_size()[0],
] ]
y_range = [ y_range = [
self.screen.get_size()[1] * 0.35, self.screen.get_size()[1] * 0.35,
self.screen.get_size()[1] * 0.5, self.screen.get_size()[1] * 0.5,
] ]
for i in range(10): for i in range(30):
x = random.randint(*x_range) x = random.randint(*x_range)
y = random.randint(*y_range) y = random.randint(*y_range)
scale = random.randint(42, 86) scale = random.randint(42, 86)
self.trees.append( self.trees.append(
TreeSprite( TreeSprite(
game=self,
tree=random.choice(self.tree_imgs), tree=random.choice(self.tree_imgs),
x=x, x=x,
y=y, y=y,
@ -433,6 +455,8 @@ class Creeper(Game):
self.controller_debug_open_debounce = 1 self.controller_debug_open_debounce = 1
self.controller_main_open_debounce = 1 self.controller_main_open_debounce = 1
self.debounce_walk_img = 0
self.inventory_menu = Menu(self, title="inventory") self.inventory_menu = Menu(self, title="inventory")
self.debug_menu = DebugMenu(self) self.debug_menu = DebugMenu(self)
self.debug_menu.is_open = debug self.debug_menu.is_open = debug
@ -461,8 +485,12 @@ class Creeper(Game):
return True return True
def place_block(self): def place_block(self):
if self.hotbar.selected.type is None: # if self.hotbar.selected.type is None:
return False # return False
self.blocks[str(self.mouse_box.get_nearest_block_pos())] = Block(
self.hotbar.selected.type, self.mouse_box.get_nearest_block_pos()
)
print(self.blocks)
def process_deaths(self): def process_deaths(self):
for i, tree in enumerate(copy(self.trees)): for i, tree in enumerate(copy(self.trees)):
@ -478,13 +506,13 @@ class Creeper(Game):
def normal_keys(self): def normal_keys(self):
keys = self.keys keys = self.keys
if keys[pygame.K_a] or keys[pygame.K_LEFT]: if keys[pygame.K_a] or keys[pygame.K_LEFT]:
self.x -= 10 self.x -= 10 * self.speed * self.elapsed
if keys[pygame.K_d] or keys[pygame.K_RIGHT]: if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
self.x += 10 self.x += 10 * self.speed * self.elapsed
if keys[pygame.K_w] or keys[pygame.K_UP]: if keys[pygame.K_w] or keys[pygame.K_UP]:
self.y -= 10 self.y -= 10 * self.speed * self.elapsed
if keys[pygame.K_s] or keys[pygame.K_DOWN]: if keys[pygame.K_s] or keys[pygame.K_DOWN]:
self.y += 10 self.y += 10 * self.speed * self.elapsed
if keys[pygame.K_k]: if keys[pygame.K_k]:
self.hotbar.next(1) self.hotbar.next(1)
if keys[pygame.K_j]: if keys[pygame.K_j]:
@ -566,18 +594,18 @@ class Creeper(Game):
for i in range(hats): for i in range(hats):
hat = joystick.get_hat(i) hat = joystick.get_hat(i)
if hat[0] == 1: if hat[0] == 1:
self.x += 10 self.x += 10 * self.speed * self.elapsed
if hat[0] == -1: if hat[0] == -1:
self.x -= 10 self.x -= 10 * self.speed * self.elapsed
if hat[1] == -1: if hat[1] == -1:
self.y += 10 self.y += 10 * self.speed * self.elapsed
if hat[1] == 1: if hat[1] == 1:
self.y -= 10 self.y -= 10 * self.speed * self.elapsed
if abs(joystick.get_axis(0)) > 0.2: if abs(joystick.get_axis(0)) > 0.2:
self.x += joystick.get_axis(0) * 10 self.x += joystick.get_axis(0) * 10 * self.speed * self.elapsed
if abs(joystick.get_axis(1)) > 0.2: if abs(joystick.get_axis(1)) > 0.2:
self.y += joystick.get_axis(1) * 10 self.y += joystick.get_axis(1) * 10 * self.speed * self.elapsed
if abs(joystick.get_axis(3)) > 0.2 and abs(joystick.get_axis(4)) > 0.2: if abs(joystick.get_axis(3)) > 0.2 and abs(joystick.get_axis(4)) > 0.2:
pygame.mouse.set_pos( pygame.mouse.set_pos(
@ -655,15 +683,22 @@ class Creeper(Game):
) and not self.walking_sound_is_playing: ) and not self.walking_sound_is_playing:
self.walking_sound.play() self.walking_sound.play()
self.walking_sound_is_playing = True self.walking_sound_is_playing = True
if self.last_x != self.x or self.last_y != self.y: if (
self.last_x != self.x or self.last_y != self.y
) and self.debounce_walk_img < 0:
self.creeper = next(self.creepers) self.creeper = next(self.creepers)
self.debounce_walk_img = 0.15
self.last_x = self.x self.last_x = self.x
self.last_y = self.y self.last_y = self.y
def game(self): def game(self):
# self.camera = (self.camera[0] + 1, self.camera[1]) # self.camera = (self.camera[0] + 1, self.camera[1])
self.debounce_walk_img -= self.elapsed
self.screen.blit(self.background, self.camera) self.screen.blit(self.background, self.camera)
for block in self.blocks.values():
self.screen.blit(block.img, block.pos)
self.background.fill((0, 255, 247)) self.background.fill((0, 255, 247))
self.process_deaths() self.process_deaths()
for tree in self.trees: for tree in self.trees:
@ -683,20 +718,20 @@ class Creeper(Game):
light_level = pygame.time.get_ticks() % self.day_len light_level = pygame.time.get_ticks() % self.day_len
light_level = abs(light_level * (255 * 2 / self.day_len) - 255) light_level = abs(light_level * (255 * 2 / self.day_len) - 255)
self.darkness.fill((light_level, light_level, light_level)) # self.darkness.fill((light_level, light_level, light_level))
if self.light_power < 500: # if self.light_power < 500:
self.light_power = min(self.light_power**1.1, 500) # self.light_power = min(self.light_power**1.1, 500)
self.darkness.blit( # self.darkness.blit(
pygame.transform.smoothscale( # pygame.transform.smoothscale(
self.spot, [self.light_power, self.light_power] # self.spot, [self.light_power, self.light_power]
), # ),
(self.x - self.light_power / 2, self.y - self.light_power / 2), # (self.x - self.light_power / 2, self.y - self.light_power / 2),
) # )
self.screen.blit( # self.screen.blit(
self.darkness, # pygame.transform.scale(self.darkness, self.screen.get_size()).convert(),
(0, 0), # (0, 0),
special_flags=pygame.BLEND_RGBA_MULT, # special_flags=pygame.BLEND_MULT,
) # )
self.hotbar.draw() self.hotbar.draw()
self.debug_menu.draw() self.debug_menu.draw()

View file

@ -10,11 +10,14 @@ class Game:
self.screen_size = (854, 480) self.screen_size = (854, 480)
self.screen_size = (1280, 800) self.screen_size = (1280, 800)
self.screen_size = (1920, 1080) self.screen_size = (1920, 1080)
self.screen = pygame.display.set_mode(self.screen_size) # pygame.display.set_mode(resolution, flags, bpp)
flags = pygame.DOUBLEBUF
self.screen = pygame.display.set_mode(self.screen_size, flags)
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.running = True self.running = True
self.surfs = [] self.surfs = []
self.elapsed = 0
def should_quit(self): def should_quit(self):
for event in self.events: for event in self.events:
@ -25,7 +28,8 @@ class Game:
... ...
def reset_screen(self): def reset_screen(self):
self.screen.fill((0, 0, 0)) ...
# self.screen.fill((0, 0, 0))
def run(self): def run(self):
while self.running: while self.running:
@ -37,7 +41,7 @@ class Game:
for surf in self.surfs: for surf in self.surfs:
pygame.blit(surf) pygame.blit(surf)
pygame.display.update() pygame.display.update()
self.clock.tick(30) self.elapsed = self.clock.tick(60) / 100
pygame.quit() pygame.quit()