food turns yellow then black as it spoils, checks food amount and adds more to equal 10, randomizes position with seed
This commit is contained in:
parent
56857e24b8
commit
fc171cd523
19
main.py
19
main.py
@ -1,6 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
import random
|
||||||
|
|
||||||
from world.world import World, Position
|
from world.world import World, Position
|
||||||
from world.render_objects import DebugRenderObject, FoodObject
|
from world.render_objects import DebugRenderObject, FoodObject
|
||||||
@ -226,7 +227,9 @@ def main():
|
|||||||
world = World()
|
world = World()
|
||||||
|
|
||||||
world.add_object(DebugRenderObject(Position(0,0)))
|
world.add_object(DebugRenderObject(Position(0,0)))
|
||||||
world.add_object(FoodObject(Position(100, 0)))
|
|
||||||
|
# setting the seed to 67 >_<
|
||||||
|
random.seed(67)
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
@ -271,6 +274,14 @@ def main():
|
|||||||
print("Tick logic executed")
|
print("Tick logic executed")
|
||||||
world.tick_all()
|
world.tick_all()
|
||||||
|
|
||||||
|
# gets every object in the world and returns amount of FoodObjects
|
||||||
|
objects = world.get_objects()
|
||||||
|
food = len([obj for obj in objects if isinstance(obj, FoodObject)])
|
||||||
|
print(f"Food count: {food}")
|
||||||
|
if food < 10:
|
||||||
|
for i in range(10 - food):
|
||||||
|
world.add_object(FoodObject(Position(random.randint(-200, 200), random.randint(-200, 200))))
|
||||||
|
|
||||||
# Calculate TPS every second
|
# Calculate TPS every second
|
||||||
if current_time - last_tps_time >= 1.0:
|
if current_time - last_tps_time >= 1.0:
|
||||||
actual_tps = tick_counter
|
actual_tps = tick_counter
|
||||||
@ -303,6 +314,12 @@ def main():
|
|||||||
tps_rect.bottomright = (SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10)
|
tps_rect.bottomright = (SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10)
|
||||||
screen.blit(tps_text, tps_rect)
|
screen.blit(tps_text, tps_rect)
|
||||||
|
|
||||||
|
# Render camera position and speed in bottom left
|
||||||
|
cam_text = font.render(f"Camera: ({camera.x:.2f}, {camera.y:.2f}), Speed: {camera.speed:.2f}", True, WHITE)
|
||||||
|
cam_rect = cam_text.get_rect()
|
||||||
|
cam_rect.bottomleft = (10, SCREEN_HEIGHT - 10)
|
||||||
|
screen.blit(cam_text, cam_rect)
|
||||||
|
|
||||||
# Update display
|
# Update display
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(180)
|
clock.tick(180)
|
||||||
|
|||||||
@ -2,6 +2,13 @@ from world.world import Position
|
|||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
# returns desired yellow value for food decay
|
||||||
|
def food_decay_yellow(decay):
|
||||||
|
if decay < 128:
|
||||||
|
return decay
|
||||||
|
else:
|
||||||
|
return 255 - decay
|
||||||
|
|
||||||
class DebugRenderObject:
|
class DebugRenderObject:
|
||||||
def __init__(self, position: Position):
|
def __init__(self, position: Position):
|
||||||
self.position = position
|
self.position = position
|
||||||
@ -22,9 +29,8 @@ class FoodObject:
|
|||||||
self.decay += 1
|
self.decay += 1
|
||||||
|
|
||||||
if self.decay > 255:
|
if self.decay > 255:
|
||||||
self.decay = 0
|
self.decay = 0 # eventually will raise a destroy flag
|
||||||
|
|
||||||
def render(self, camera, screen):
|
def render(self, camera, screen):
|
||||||
if camera.is_in_view(*self.position.get_position()):
|
if camera.is_in_view(*self.position.get_position()):
|
||||||
pygame.draw.circle(screen, (255-self.decay,self.decay,0), camera.world_to_screen(*self.position.get_position()), 5 * camera.zoom)
|
pygame.draw.circle(screen, (255-self.decay,food_decay_yellow(self.decay),0), camera.world_to_screen(*self.position.get_position()), 5 * camera.zoom)
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,9 @@ class World:
|
|||||||
def add_object(self, new_object):
|
def add_object(self, new_object):
|
||||||
self.objects.append(new_object)
|
self.objects.append(new_object)
|
||||||
|
|
||||||
|
def get_objects(self):
|
||||||
|
return self.objects
|
||||||
|
|
||||||
class Position:
|
class Position:
|
||||||
def __init__(self, x, y):
|
def __init__(self, x, y):
|
||||||
self.x = x
|
self.x = x
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user