def __init__(self, screen): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("moose.gif") self.image = self.image.convert() self.rect = self.image.get_rect() self.rawr_sound = pygame.mixer.Sound("rawr.ogg") self.eat_sound = pygame.mixer.Sound("nomchomp.ogg") self.rect.center = (400,400) self.pos = vector(self.rect.center) self.vel = vector(0,0) self.acc = vector(0,0) self.lives = 3
def play(): pygame.init() window = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() milliseconds = 0 FPS = 1000 character = Character(400, 400, 100, pygame.Color("Red")) balls = [] balls.append(Circle(200, 300, 70)) balls[0].velocity = vector(0, 0) window.fill("Black") mouse = Mouse() while True: milliseconds = clock.tick(FPS) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() window.fill("Black") mouse.update() character.physics(milliseconds) character.borders(window) character.control(mouse.state, balls) check_collisions(character, balls) character.draw(window) if character.radius < 3: print("You lost") pygame.quit() i = 0 for ball in balls: ball.physics(milliseconds) ball.borders(window) check_collisions(ball, balls) ball.draw(window) if (balls[i].radius < 3): del balls[i] i += 1 pygame.display.flip()
def play(): pygame.init() window = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() milliseconds = 0 FPS = 1000 balls = [] balls.append(Circle(200, 300, 100)) balls[0].velocity = vector(0, 0) balls.append(Circle(700, 250, 50)) balls[1].velocity = vector(-175, 0) window.fill("Black") print(count_imp(balls)) while True: milliseconds = clock.tick(FPS) for event in pygame.event.get(): if event.type == QUIT: print(count_imp(balls)) pygame.quit() sys.exit() window.fill("Black") i = 0 for ball in balls: ball.physics(milliseconds) ball.borders(window) collision(ball, balls) ball.draw(window) if (balls[i].radius < 3): del balls[i] i += 1 pygame.display.flip()
class Character(Circle): def control(self): mouse_pos = pygame.mouse.get_pos() if pygame.mouse.get_pressed()[0] = False: return 0 direction = vector(mouse_pos[0] - self.pos.x, mouse_pos[1] - self.pos.y) opposite_direction = direction * -1 split_mass = self.mass / 100 mass_new = self.mass/100 - split.split_mass self.mass = mass_new child = Circle()
def __init__(self): pg.sprite.Sprite.__init__(self) self.sprites=[] self.index=0 self.x=random.randint(3,grid-3) self.y=random.randint(3,grid-3) self.posicao=vector(self.x,self.y) for i in range(8): self.sprites.append(imagem.subsurface((i*32,0),(32,32))) self.image=self.sprites[5] self.rect=self.image.get_rect() self.rect.center=(self.posicao.x*size,self.posicao.y*size)
def update(self, screen): self.acc = vector(0,2) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.acc.x = -1 if keys[pygame.K_RIGHT]: self.acc.x = 1 # equations for moving self.acc.x += self.vel.x * -0.12 self.vel += self.acc self.pos += self.vel + 0.5 * self.acc self.rect.midbottom = self.pos # Wrap around screen if self.pos.x > screen.get_width(): self.pos.x = 0 if self.pos.x < 0: self.pos.x = screen.get_width()
def control(self, mouse_state, balls): mouse_pos = pygame.mouse.get_pos() if pygame.mouse.get_pressed()[0] == False or mouse_state != 'pressing': return 0 direction = vector(mouse_pos[0] - self.pos.x, mouse_pos[1] - self.pos.y) child = Circle(self.pos.x, self.pos.y, 0, pygame.Color("Green")) split_mass = self.mass / 40 self.mass -= split_mass child.mass = split_mass child.pos = mouse_pos - self.pos child.pos.scale_to_length(self.radius + child.radius) child.pos += self.pos child.velocity = self.velocity + direction.normalize() * 400 self.velocity = ((self.mass + child.mass) * self.velocity - child.impulse) / self.mass balls.append(child)
from pygame.math import Vector2 as vector border_padding = 50 width, height = 545, 555 maze_width, maze_height = width-border_padding, height-border_padding fps = 60 intro_text_size_title = 40 intro_text_size_subtitle = 30 intro_text_size = 20 intro_font = 'Bahnschrift Light' #-#---COLORS---#-# pacman_yellow = (248, 211, 10) ghost_pink = (253, 173, 167) ghost_cyan = (73, 223, 202) ghost_red = (199, 59, 17) ghost_orange = (255, 152, 48) black = (0, 0, 0) white = (255, 255, 255) purple = (113, 63, 159) hot_pink = (200, 67, 82) green = (73, 174, 138) blue = (73, 108, 174) #-#---PLAYER__#-# starting_pos = vector(9, 11)
def __init__(self, posx=0, posy=0, radius=50, color=pygame.Color("White")): self.pos = vector(posx, posy) self.radius = radius self.color = color self.velocity = vector(0, 0)
def drawLine(self, x1, y1, x2, y2, r, g, b): color = pygame.Color(r, g, b) start = vector(x1, y1) end = vector(x2, y2) pygame.draw.line(self.screen, color, start, end, width=1)
def __init__(self): self.add=False self.ponto=any self.corpo=[vector(13,20),vector(15,20),vector(12,20)] self.direcao=vector(1,0)
fase2=False while True : view.fill([255,255,255]) bordas() for event in pg.event.get(): if event.type==pg.QUIT: pg.quit() sys.exit() if event.type==MOVI_C: if fase1: c.movimentacao() if event.type==pg.KEYDOWN: if event.key==pg.K_UP: c.direcao=vector(0,-1) if event.key==pg.K_LEFT: c.direcao=vector(-1,0) if event.key==pg.K_RIGHT: c.direcao=vector(1,0) if event.key==pg.K_DOWN: c.direcao=vector(0,1) if event.key==pg.K_RETURN: fase1=True if event.key==pg.K_0: pontos=3 if fase1:
def main(): points = 0 #entities background = pygame.image.load("bg.png") screen.blit(background, (0, 0)) # Player moose = CatWorldSprites.HeroCat(screen) # Scoreboard scoreboard = CatWorldSprites.ScoreBoard(screen) scoreSprite = pygame.sprite.Group(scoreboard) # Enemies enemies = [] for i in range(3): enemy = CatWorldSprites.EnemyCat(screen) enemies.append(enemy) # Platforms ground = CatWorldSprites.Platforms(screen, 860, 40, 0, 440) platform1 = CatWorldSprites.Platforms(screen, 200, 40, 100, 340) platform2 = CatWorldSprites.Platforms(screen, 200, 40, 340, 240) platform3 = CatWorldSprites.Platforms(screen, 200, 40, 80, 140) platformGroup = pygame.sprite.Group(ground, platform1, platform2, platform3) # Treats treats = [] for i in range(TREATS): treat = CatWorldSprites.Treats(screen) treats.append(treat) # SPrite Groups player = pygame.sprite.Group(moose) treatGroup = pygame.sprite.Group(treats) allsprites = pygame.sprite.Group(moose, enemies, treats, ground, platform1, platform2, platform3) enemyGroup = pygame.sprite.Group(enemies) keepGoing = True clock = pygame.time.Clock() pygame.mouse.set_visible(False) #game loop while keepGoing: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: moose.jump() if moose.vel.y > 0: platformCollisions = pygame.sprite.spritecollide( moose, platformGroup, False) if platformCollisions: moose.pos.y = platformCollisions[0].rect.top moose.vel.y = 0 enemyHeroCollision = pygame.sprite.spritecollide( moose, enemyGroup, False) if enemyHeroCollision: moose.lives -= 1 moose.pos = vector(400, 400) moose.rawr() scoreboard.lives = moose.lives platformGroup.update(screen) treatGroup.update(screen) allsprites.update(screen) enemyGroup.update(screen) scoreSprite.update(screen) #update the screen platformGroup.clear(screen, background) treatGroup.clear(screen, background) allsprites.clear(screen, background) enemyGroup.clear(screen, background) scoreSprite.clear(screen, background) platformGroup.draw(screen) allsprites.draw(screen) treatGroup.draw(screen) enemyGroup.draw(screen) scoreSprite.draw(screen) treatCollisions = pygame.sprite.spritecollide(moose, treatGroup, True) if treatCollisions: points += 1 scoreboard.score = points scoreSprite.update(screen) moose.eat() if points == 10: youWin() if moose.lives < 0: gameOver() font = pygame.font.SysFont("Comic Sans MS", 20) text = font.render("Space to Jump arrow keys to move", 1, (255, 0, 0)) screen.blit(text, (100, 440)) pygame.display.flip() pygame.quit()
def count_imp(list): """Counts impulses for balls.""" rslt = vector(0, 0) for ball in list: rslt += ball.mass * ball.velocity return rslt