Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ss19:erstes_pygame_simulation_alex

Dies ist eine alte Version des Dokuments!


import pygame
import random
import math
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
is_blue = True
x = 30
y = 30
clock = pygame.time.Clock()
walls=[]

class Wall(object):
    size=5
    def __init__(self, pos):
        walls.append(self)
        self.x=pos[0]
        self.y=pos[1]
        self.rect = pygame.Rect(pos[0], pos[1], self.size,self.size)
animals=[]
class Creature(object):
    size=5
    sight=5
    food=0
    veränderungx=0
    veränderungy=0
    def __init__(self,x,y,angle,speed):
        animals.append(self)
        self.x1=x
        self.y1=y        
        self.speed=5
        self.stamina=250
        self.angle=angle
    def inrange(self):
        self.rect=pygame.Rect(int(self.x1)-int(self.sight),int(self.y1)-int(self.sight),self.sight*2+self.size,self.sight*2+self.size)
        if(self.rect.collidelist(walls)>-1):
			
            self.veränderungx=walls[self.rect.collidelist(walls)].x-self.x1
            self.veränderungy=walls[self.rect.collidelist(walls)].y-self.y1
            #self.x1=walls[self.rect.collidelist(walls)].x
            #self.y1=walls[self.rect.collidelist(walls)].y		
i=0
anzahlrect=150
while(i<anzahlrect):
	x=random.randint(0,398)
	y=random.randint(0,298)
	Wall((x,y))
	i+=1
j=0
anzahlanimal=50
while(j<anzahlanimal):
    x=200
    y=150
    speed=5
    angle=random.randint(0,360)
    Creature(x,y,angle,speed)
    j+=1

moveframes=5
while not done:
        clock.tick(50)
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
        i=0
        durchlaeufe=0
        screen.fill((0,100,50))
        while(i<1):
            
            for animal in animals:   
                animal.angle=random.randint(animal.angle-45,animal.angle+45)
                
                #if(moveframes==5):
                #    moveframes=0
                if(durchlaeufe%5==0):
                    
                    x2=animal.x1+animal.speed*math.sin((animal.angle/180)*math.pi)
                    y2=animal.y1+animal.speed*math.cos((animal.angle/180)*math.pi)
                    animal.veränderungx=x2-animal.x1
                    animal.veränderungy=y2-animal.y1
                    animal.inrange()
                #pygame.draw.line(screen,(255,0,0),(animal.x1,animal.y1),(x2,y2))
                animal.x1+=animal.veränderungx/moveframes
                animal.y1+=animal.veränderungy/moveframes
                #pygame.draw.circle(screen,(255,0,0),(int(animal.x1),int(animal.y1)),5)
                arect=pygame.Rect(int(animal.x1),int(animal.y1),animal.size,animal.size)
                if arect.collidelist(walls)>=0:
                    animal.food+=1
                    del(walls[arect.collidelist(walls)])
                    animal.stamina+=50
                    if(animal.food%2==0):
                        Creature(animal.x1,y,animal.angle,animal.speed)
                           
                pygame.draw.rect(screen,(255,0,0),arect)
                animal.stamina-=animal.speed/5
            j=0
            durchlaeufe+=1   
            while j< len(animals):
                if(animals[j].stamina<=0):
                    del(animals[j])
                    
                j+=1     
             
            i+=1
        for wall in walls:
            pygame.draw.rect(screen, (255, 0, 255), wall.rect)

            
        pygame.display.flip()
ss19/erstes_pygame_simulation_alex.1559651449.txt.gz · Zuletzt geändert: 2019/06/04 14:30 von alexanderdross