Code: Alles auswählen
#Worlds
#World
class World(object):
def __init__(self, game, name, color, xSize, ySize):
self.game = game
self.name = name
self.color = color
self.xsize = xSize
self.ysize = ySize
self.background = pygame.surface.Surface((self.game.screenX, self.game.screenY)).convert()
self.background.fill(self.color)
self.entities = {}
self.border = {}
self.entity_id = 0
def addEntity(self, entity):
self.entities[self.entity_id] = entity
entity.id = self.entity_id
self.entity_id += 1
def addBorderEntity(self, entity):
self.border[self.entity_id] = entity
entity.id = self.entity_id
self.entity_id += 1
def removeEntity(self, entity):
del self.entities[entity.id]
def getEntity(self, entity_id):
if entity_id in self.entities:
return self.entities[entity_id]
else:
return None
def process(self, time_passed):
time_passed_seconds = time_passed / 1000.0
for entity in self.entities.values():
entity.process(time_passed_seconds)
def render(self, surface, x, y, xSize, ySize):
xblit = xSize / 2 -48
yblit = ySize / 2 -48
if self.game.player.location.x < xSize / 2:
surfaceX = xblit - self.game.player.location.x + 48
surfaceXS = xSize
elif self.game.player.location.x > self.xsize - xSize / 2:
surfaceX = 0
surfaceXS = xSize - (xSize / 2 - (self.xsize -self.game.player.location.x))
else:
surfaceX = 0
surfaceXS = xSize
if self.game.player.location.y < ySize / 2:
surfaceY = yblit - self.game.player.location.y + 48
surfaceYS = xSize
elif self.game.player.location.y > self.ysize - ySize / 2:
surfaceY = 0
surfaceYS = ySize - (ySize / 2 - (self.ysize - self.game.player.location.y))
else:
surfaceY = 0
surfaceYS = ySize
if self.game.player.location.x > self.xsize - xSize - 100 or self.game.player.location.y > self.ysize - ySize - 100:
self.background = pygame.surface.Surface((surfaceXS, surfaceYS)).convert()
self.background.fill(self.color)
surface.blit(self.background, (surfaceX, surfaceY))
for entity in self.border.itervalues():
entity.render(surface, x, y, xSize, ySize)
for entity in self.entities.itervalues():
if entity.getRenderType() == 0:
entity.render(surface, x, y, xSize, ySize)
for entity in self.entities.itervalues():
if entity.getRenderType() == 1:
entity.render(surface, x, y, xSize, ySize)
for entity in self.entities.itervalues():
if entity.getRenderType() == 2:
entity.render(surface, x, y, xSize, ySize)
def getCloseEntity(self, name, x, y, searchRange=100):
for entity in self.entities.itervalues():
if entity.name == name:
distance = entity.get_distance_to(x, y)
if distance < searchRange:
return entity
return None
def getPointEntity(self, x, y, searchRange=24):
for entity in self.entities.itervalues():
distance = entity.get_distance_to(x, y)
if distance < searchRange:
return entity
return None
def getPassability(self, x, y, radius):
for entity in self.entities.itervalues():
if entity.isPassable() == False:
if entity.get_distance_type() == 0:
if entity.get_distance_to(x, y) < entity.sizeradius + radius:
return False
else:
if entity.get_x_distance(x) < entity.sizeradius + radius and entity.get_y_distance(y) < entity.sizeradius + radius:
return False
return True
#Entities
#GameEntity
class GameEntity(object):
def __init__(self, name, world, images, x, y, speedModifier, sizeradius, animseq, animn):
self.world = world
self.name = name
self.images = images
self.location = Vector2(x, y)
self.destination = Vector2(x, y)
self.speedModifier = speedModifier
self.speed = self.speedModifier * 96
self.entityID = 0
self.sizeradius = sizeradius
self.image_to_render1 = 0
self.image_to_render2 = 0
self.animn = animn
self.animseq = animseq
self.time_passed = 0.0
self.moving = False
def render(self, surface, x, y, xSize, ySize):
if self.images[0] != None:
if self.location.x < x + xSize/2 + 100 and self.location.x > x - xSize/2 - 100 and self.location.y < y + ySize/2 + 100 and self.location.y > y - ySize/2 - 100:
blit_x = xSize/2 -(x - self.location.x)
blit_y = ySize/2 - (y - self.location.y)
if self.is_animated() == True and self.can_rotate() == True:
if self.moving == True:
rendering_image = self.images[self.animseq[self.image_to_render1]]
else:
rendering_image = self.images[0]
rendering_image1 = rendering_image[self.image_to_render2]
else:
rendering_image1 = self.images[0]
if self.name == "player":
surface.blit(rendering_image1,(blit_x-48, blit_y-48))
else:
surface.blit(rendering_image1,(blit_x, blit_y))
def process(self, time_passed):
if self.location != self.destination and self.animseq != None:
self.time_passed += time_passed
if self.time_passed > 0.25:
self.time_passed -= 0.25
self.image_to_render1 += 1
if self.image_to_render1 > self.animn:
self.image_to_render1 = 0
if self.speedModifier > 0:
if self.location != self.destination:
vec_to_destination = self.destination - self.location
distance_to_destination = vec_to_destination.getMagnitude()
heading = vec_to_destination
heading.normalize()
travelDistance = min(distance_to_destination, time_passed * self.speed)
processDistance = self.location + heading.__mul__(travelDistance)
if self.world.getPassability(processDistance.x, processDistance.y, self.sizeradius) == True:
self.location = processDistance
else:
self.set_moving(False)
if distance_to_destination <= 6.0:
self.set_moving(False)
else:
rotation = self.setRotation()
self.image_to_render2 = rotation
def getAngle(self):
vs = Vector2(-1.0, 0.0)
vtd = self.destination - self.location
if vtd.x != 0 or vtd.y != 0:
cosvalue = (vs.x * vtd.x + vs.y * vtd.y) / (sqrt(vs.x**2 + vs.y**2) * (sqrt(vtd.x**2 + vtd.y**2)))
finalvalue = acos(cosvalue) / pi * 180
if vtd.y > 0:
finalvalue = 360 - finalvalue
else:
finalvalue = 0
return finalvalue
def setRotation(self):
rotationAngle = self.getAngle()
hvalue = rotationAngle / 22.5
if hvalue > 1 and hvalue <= 3:
image_to_render = 1
elif hvalue > 3 and hvalue <= 5:
image_to_render = 2
elif hvalue > 5 and hvalue <= 7:
image_to_render = 3
elif hvalue > 7 and hvalue <= 9:
image_to_render = 4
elif hvalue > 9 and hvalue <= 11:
image_to_render = 5
elif hvalue > 11 and hvalue <= 13:
image_to_render = 6
elif hvalue > 13 and hvalue <= 15:
image_to_render = 7
else:
image_to_render = 0
return image_to_render
#Living Entity
class LivingEntity(GameEntity):
def __init__(self, name, world, image, x, y, speedModifier, sizeradius, animseq, animn):
GameEntity.__init__(self, name, world, image, x, y, speedModifier, sizeradius, animseq, animn)
#PLayer
class Player(LivingEntity):
def __init__(self, name, world, image, x, y, speedModifier, sizeradius, inv_image, filler, shadow, scx, scy, animseq, animn):
LivingEntity.__init__(self, name, world, image, x, y, speedModifier, sizeradius, animseq, animn)
self.inventory = None
self.scx = scx
self.scy = scy
self.add_inventory(inv_image, filler, shadow)
#Obstacle
class Obstacle(GameEntity):
def __init__(self, name, world, images, x, y, sizeradius, animseq, animn):
GameEntity.__init__(self, name, world, images, x, y, 0.0, sizeradius, animseq, animn)
#Square Obstacle
class SquareObstacle(Obstacle):
def __init__(self, name, world, images, x, y, sizeradius, animseq, animn):
Obstacle.__init__(self, name, world, images, x, y, sizeradius, animseq, animn)
class Collectible(Obstacle):
def __init__(self, name, world, images, x, y, sizeradius, animseq, animn):
Obstacle.__init__(self, name, world, images, x, y, sizeradius, animseq, animn)
self.drop = None
class Flower(Collectible):
def __init__(self, name, world, images, x, y, sizeradius, animseq, animn):
Collectible.__init__(self, name, world, images, x, y, sizeradius, animseq, animn)
class Trunk(Obstacle):
def __init__(self, name, world, x, y, sizeradius, animseq, animn):
Obstacle.__init__(self, name, world, [None], x, y, sizeradius, animseq, animn)
class Leaves(GameEntity):
def __init__(self, name, world, images, x, y, animseq, animn):
GameEntity.__init__(self, name, world, images, x, y, 0.0, 0, animseq, animn)
#Cursor
class Cursor(object):
def __init__(self, image, move_image):
self.image = image
self.stack = None
self.move_image = move_image
def render(self, surface, x, y, image = None):
if self.stack == None:
if image == None:
image_to_render = self.image
else:
image_to_render = image
else:
image_to_render = self.stack.item.image
surface.blit(image_to_render,(x, y))
if self.stack != None:
surface.blit(self.move_image,(x, y))
#Main
#Game
class Game(object):
def __init__(self):
self.worlds = []
self.currentWorld = 0
self.guis = []
self.player = None
self.cursor = None
self.screenX = 1920
self.screenY = 1080
self.run()
def run(self):
pygame.init()
screen = pygame.display.set_mode((self.screenX, self.screenY), FULLSCREEN, 32)
clock = pygame.time.Clock()
font_inv_index = pygame.font.SysFont("arial", 16)
self.cursor = Cursor(cursor_normal, cursor_move)
self.addWorld(self, "0", gras, 4800, 4800)
self.worlds[self.currentWorld].addBorder(cfelsen, gfelsen1, gfelsen2)
self.player = Player("player", self.worlds[self.currentWorld], [pnlist, pllist, prlist], 2112, 2112, 1.0, 24, inventory1, invfiller, invshade, self.screenX, self.screenY, [0, 1, 0, 2], 3)
self.worlds[self.currentWorld].addEntity(self.player)
self.worlds[self.currentWorld].addEntity(Obstacle("felsen", self.worlds[self.currentWorld], [felsen1], 480, 480, 48, None, 0))
self.worlds[self.currentWorld].addEntity(Obstacle("felsen", self.worlds[self.currentWorld], [felsen1], 1920, 480, 48, None, 0))
self.worlds[self.currentWorld].addEntity(Obstacle("felsen", self.worlds[self.currentWorld], [felsen1], 960, 672, 48, None, 0))
self.worlds[self.currentWorld].addEntity(Obstacle("felsen", self.worlds[self.currentWorld], [felsen1], 2400, 2400, 48, None, 0))
self.worlds[self.currentWorld].addEntity(Flower("sonnenblume", self.worlds[self.currentWorld], [pygame.transform.rotate(sunflower, 90 * randint(0,3))], 2300, 2300, 24, None, 0))
self.worlds[self.currentWorld].addEntity(Flower("sonnenblume", self.worlds[self.currentWorld], [pygame.transform.rotate(sunflower, 90 * randint(0,3))], 2200, 2400, 24, None, 0))
self.worlds[self.currentWorld].addEntity(Flower("sonnenblume", self.worlds[self.currentWorld], [pygame.transform.rotate(sunflower, 90 * randint(0,3))], 2400, 2200, 24, None, 0))
self.worlds[self.currentWorld].addEntity(Flower("sonnenblume", self.worlds[self.currentWorld], [pygame.transform.rotate(sunflower, 90 * randint(0,3))], 2200, 2300, 24, None, 0))
self.worlds[self.currentWorld].addEntity(Flower("sonnenblume", self.worlds[self.currentWorld], [pygame.transform.rotate(sunflower, 90 * randint(0,3))], 2300, 2200, 24, None, 0))
self.worlds[self.currentWorld].addEntity(Flower("sonnenblume", self.worlds[self.currentWorld], [pygame.transform.rotate(sunflower, 90 * randint(0,3))], 2400, 2300, 24, None, 0))
self.worlds[self.currentWorld].addEntity(Obstacle("felsen", self.worlds[self.currentWorld], [felsen2], 2600, 2600, 48, None, 0))
self.worlds[self.currentWorld].addTree(self.worlds[self.currentWorld], crown, 1000, 1000)
self.worlds[self.currentWorld].addTree(self.worlds[self.currentWorld], crown, 2000, 2900)
pygame.mouse.set_visible(False)
while True:
#print(round(self.player.location.x, 0), round(self.player.location.y, 0), round(self.player.destination.x, 0), round(self.player.destination.y, 0))
for event in pygame.event.get():
if event.type==QUIT:
exit()
elif event.type==MOUSEBUTTONDOWN:
if event.button == 1:
x, y = pygame.mouse.get_pos()
gui_in = None
for gui in self.guis:
if gui.check_inside(self.screenX, self.screenY, x, y) == True:
gui_in = gui
if gui_in != None:
gui_in.click_at(x, y, self.screenX, self.screenY, self.cursor, 0)
else:
realX = self.player.location.x - self.screenX / 2 + x
if realX < 120:
realX = 120
elif realX > self.worlds[self.currentWorld].xsize - 120:
realX = self.worlds[self.currentWorld].xsize - 120
realY = self.player.location.y - self.screenY / 2 + y
if realY < 120:
realY = 120
elif realY > self.worlds[self.currentWorld].ysize - 120:
realY = self.worlds[self.currentWorld].ysize - 120
self.player.destination = Vector2(realX, realY)
self.player.set_moving(True)
elif event.button == 3:
x, y = pygame.mouse.get_pos()
gui_in = None
for gui in self.guis:
if gui.check_inside(self.screenX, self.screenY, x, y) == True:
gui_in = gui
if gui_in != None:
gui_in.click_at(x, y, self.screenX, self.screenY, self.cursor, 1)
elif event.type==KEYDOWN:
if event.key == K_i:
self.player.inventory.toggle()
time_passed = clock.tick(32)
screen.fill(grau)
self.worlds[self.currentWorld].process(time_passed)
x2, y2 = pygame.mouse.get_pos()
realX2 = self.player.location.x - self.screenX / 2 + x2
realY2 = self.player.location.y - self.screenY / 2 + y2
if x2 > 100 and x2 < self.screenX - 100 and y2 > 100 and y2 < self.screenY - 100:
close_entity = self.worlds[self.currentWorld].getPointEntity(realX2, realY2)
else:
close_entity = None
cursor_image = None
if close_entity != None:
if close_entity.isCollectible() == True:
cursor_image = cursor_flower
else:
self.cursor_image = cursor_normal
screen.set_clip(0, 0, 1920, 1080)
self.worlds[self.currentWorld].render(screen, self.player.location.x, self.player.location.y, self.screenX, self.screenY)
self.player.inventory.render(screen, self.screenX, self.screenY, font_inv_index)
self.cursor.render(screen, x2, y2, image = cursor_image)
pygame.display.update()
def addWorld(self, game, name, color, xSize, ySize):
world = World(self, name, color, xSize, ySize)
self.worlds.append(world)
def addGUI(self, gui):
self.guis.append(gui)
Game()
Das ist ein Auszug aus meinem Code, der Teil, der vemutlich problematisch ist.
Der Prozessor braucht jetzt nur noch 30 - 50%, seit ich das mit dem convert gefixt habe, ruckeln tut es aber immer noch.