Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import paho.mqtt.client as mqtt
- pygame.init()
- tela_largura = 1201;
- tela_altura = 607;
- win = pygame.display.set_mode((tela_largura,tela_altura))
- pygame.display.set_caption("Trabalho de SIM")
- clock = pygame.time.Clock()
- bg = pygame.image.load('Ativos/bg.png')
- spr_torno = pygame.image.load('Ativos/spr_torno.png')
- spr_fresa = pygame.image.load('Ativos/spr_fresa.png')
- spr_asrs = pygame.image.load('Ativos/spr_asrs.png')
- spr_trobo = pygame.image.load('Ativos/spr_trobo.png')
- spr_frobo = pygame.image.load('Ativos/spr_frobo.png')
- spr_arobo = pygame.image.load('Ativos/spr_arobo.png')
- spr_carro = pygame.image.load('Ativos/spr_carro.png')
- spr_ca = pygame.image.load('Ativos/spr_ca.png')
- spr_cb = pygame.image.load('Ativos/spr_cb.png')
- spr_cc = pygame.image.load('Ativos/spr_cc.png')
- spr_cd = pygame.image.load('Ativos/spr_cd.png')
- tamanho_esteira = 30
- pos_asrs = 0
- pos_torno = 10
- pos_fresa = 20
- num_pallet = 10
- def on_log(client, userdata, level, buf):
- print("log: "+buf)
- def on_connect(client, userdata, flags, rc):
- if rc==0:
- print("Conexao OK")
- else:
- print("Nao conectado, erro: ",rc)
- def on_message(client, userdata, message):
- global mensagem
- global topico
- mensagem = int(message.payload)
- topico = str(message.topic)
- # print("Mensagem recebida: " ,str(message.payload.decode("utf-8")))
- print("Mensagem recebida: " ,str(mensagem))
- print("Topico da mensagem: ",topico)
- print("Mensagem qos: ",message.qos)
- print("Mensagem retain flag: ",message.retain)
- broker_address="192.168.11.6"
- client = mqtt.Client("P1") #criar nova instancia
- client.on_connect=on_connect #anexar funcao ao retorno de chamada
- client.on_log=on_log
- client.on_message = on_message
- client.loop_start()
- print("Assinando o topico","carros/numero")
- client.subscribe("carros/numero")
- if topico == 'carros/numero':
- numero_carros = mensagem
- for i in range(numero_carros):
- client.subscribe("carros/posicao"+i)
- class torno(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.animacao = 0
- def draw(self, win):
- if self.animacao == 0:
- win.blit(spr_torno,(self.x,self.y))
- #pygame.draw.rect(win, (255,0,0), self.hitbox,2)
- class fresa(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.animacao = 0
- def draw(self, win):
- if self.animacao == 0:
- win.blit(spr_fresa,(self.x,self.y))
- #pygame.draw.rect(win, (255,0,0), self.hitbox,2)
- class asrs(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.animacao = 0
- def draw(self, win):
- if self.animacao == 0:
- win.blit(spr_asrs,(self.x,self.y))
- #pygame.draw.rect(win, (255,0,0), self.hitbox,2)
- class robo_torno(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.animacao = 0
- def draw(self, win):
- if self.animacao == 0:
- win.blit(spr_trobo,(self.x,self.y))
- class robo_fresa(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.animacao = 0
- def draw(self, win):
- if self.animacao == 0:
- win.blit(spr_frobo,(self.x,self.y))
- class robo_asrs(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.animacao = 0
- def draw(self, win):
- if self.animacao == 0:
- win.blit(spr_arobo,(self.x,self.y))
- class carro(pygame.sprite.Sprite):
- def __init__(self,spr,x,y):
- self.spr, self.x, self.y = spr, x, y
- #self.posicao = 3
- def draw(self, win):
- win.blit(self.spr,(self.x,self.y))
- class ca(object):
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.posicao = 0
- def draw(self, win):
- win.blit(spr_ca,(self.x,self.y))
- def troca_posicao(posicao):
- if posicao == 1:
- x, y = 850, 100
- return x, y
- elif posicao == 2:
- x, y = 786, 100
- return x, y
- elif posicao == 3:
- x, y = 722, 100
- return x, y
- elif posicao == 4:
- x, y = 658, 100
- return x, y
- elif posicao == 5:
- x, y = 594, 100
- return x, y
- elif posicao == 6:
- x, y = 530, 100
- return x, y
- elif posicao == 7:
- x, y = 466, 100
- return x, y
- elif posicao == 8:
- x, y = 402, 100
- return x, y
- elif posicao == 9:
- x, y = 335, 100
- return x, y
- elif posicao == 10:
- x, y = 335, 160
- return x, y
- elif posicao == 11:
- x, y = 335, 223
- return x, y
- elif posicao == 12:
- x, y = 335, 283
- return x, y
- elif posicao == 13:
- x, y = 335, 344
- return x, y
- elif posicao == 14:
- x, y = 335, 404
- return x, y
- elif posicao == 15:
- x, y = 335, 465
- return x, y
- elif posicao == 16:
- x, y = 402, 465
- return x, y
- elif posicao == 17:
- x, y = 466, 465
- return x, y
- elif posicao == 18:
- x, y = 530, 465
- return x, y
- elif posicao == 19:
- x, y = 594, 465
- return x, y
- elif posicao == 20:
- x, y = 668, 465
- return x, y
- elif posicao == 21:
- x, y = 752, 465
- return x, y
- elif posicao == 22:
- x, y = 836, 465
- return x, y
- elif posicao == 23:
- x, y = 900, 465
- return x, y
- elif posicao == 24:
- x, y = 955, 465
- return x, y
- elif posicao == 25:
- x, y = 955, 404
- return x, y
- elif posicao == 26:
- x, y = 955, 344
- return x, y
- elif posicao == 27:
- x, y = 955, 283
- return x, y
- elif posicao == 28:
- x, y = 955, 223
- return x, y
- elif posicao == 29:
- x, y = 955, 163
- return x, y
- elif posicao == 30:
- x, y = 955, 100
- return x, y
- def refazerJanela():
- win.blit(bg, (0,0))
- torno.draw(win)
- fresa.draw(win)
- asrs.draw(win)
- robo_torno.draw(win)
- robo_fresa.draw(win)
- robo_asrs.draw(win)
- carro1.draw(win)
- carro2.draw(win)
- ca.draw(win)
- pygame.display.update()
- torno = torno(450,490)
- fresa = fresa(1080,130)
- asrs = asrs(15,100)
- robo_torno = robo_torno(600,380)
- robo_fresa = robo_fresa(930,180)
- robo_asrs = robo_asrs(230,170)
- carro1 = carro(spr_carro,850,100)
- carro2 = carro(spr_carro,786,100)
- ca = ca(850 + 10,100 - 35)
- run = True
- while run:
- clock.tick(27)
- if topico == 'carros/numero':
- numero_carros = mensagem
- if topic == 'carros/posicao':
- pass
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- tecla = pygame.key.get_pressed()
- if tecla[pygame.K_SPACE] and shootloop == 0:
- som_balas.play()
- if homem.esquerda:
- facing = -1
- else:
- facing = 1
- if len(balas) < 5:
- balas.append(projectile(int(homem.x + homem.largura//2), int(homem.y + homem.altura//2), 6, (0,0,0), facing))
- shootloop = 1
- if tecla[pygame.K_LEFT] and (homem.x > homem.vel):
- homem.x -= homem.vel
- homem.esquerda = True
- homem.direita = False
- homem.standing = False
- elif tecla[pygame.K_RIGHT] and (homem.x < tela_largura - homem.largura - homem.vel):
- homem.x += homem.vel
- homem.esquerda = False
- homem.direita = True
- homem.standing = False
- else:
- homem.standing = True
- homem.cont_andar = 0
- if not (homem.pular):
- if tecla[pygame.K_UP]:
- homem.pular = True
- homem.direita = False
- homem.esquerda = False
- homem.cont_andar = 0
- else:
- if homem.cont_pular >= -10:
- neg = 1
- if homem.cont_pular < 0:
- neg = -1
- homem.y -= (homem.cont_pular ** 2) * 0.5 * neg
- homem.cont_pular -= 1
- else:
- homem.pular = False
- homem.cont_pular = 10
- refazerJanela()
- client.loop_stop()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment