Advertisement
Guest User

Untitled

a guest
May 30th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.71 KB | None | 0 0
  1. from PPlay.window import *
  2. from PPlay.sprite import *
  3. from math import cos, sin, sqrt
  4.  
  5. Pi = 3.14159265359
  6. font = pygame.font.Font("arial.ttf", 32)
  7. scorefont = pygame.font.Font("arial.ttf", 18)
  8. starttext = font.render("Press Space to Start!", 0,(255,255,255))
  9. wintext = font.render("You win!", 0,(255,255,255))
  10. losetext = font.render("You lose!", 0,(255,255,255))
  11.  
  12. scoreA = 0
  13. scoreB = 0
  14.  
  15. class Game:
  16.     janela = False
  17.     screen = False
  18.     width = 0
  19.     heigth = 0
  20.     objetos = []
  21.     textobjs = []
  22.     keybinds = []
  23.     eventbinds = []
  24.     background = False
  25.     teclado = False
  26.     borders = False
  27.  
  28.     def __init__(self, w, h):
  29.         self.janela = Window(w, h)
  30.         self.screen = self.janela.get_screen()
  31.         self.teclado = self.janela.get_keyboard()
  32.         self.borders = [0, w, 0, h]
  33.         self.width = w
  34.         self.heigth = h
  35.  
  36.     def update(self):
  37.         if(self.janela):
  38.             if(self.background):
  39.                 if(type(self.background) == tuple or type(self.background) == list):
  40.                     self.screen.fill(self.background)
  41.                 else:
  42.                     self.background.draw()
  43.                
  44.             for bind in self.eventbinds:
  45.                 if(type(bind[1]) == int and bind[2]):
  46.                     bind[0](self, self.objetos[bind[1]], bind[2])
  47.                 elif(type(bind[1]) == int):
  48.                     bind[0](self, self.objetos[bind[1]])
  49.                 else:
  50.                     bind[0](self)
  51.            
  52.             for bind in self.keybinds:
  53.                 if(bind[1]):
  54.                     bind[0](self, self.teclado, self.objetos[bind[1]])
  55.                 else:
  56.                     bind[0](self, self.teclado)
  57.  
  58.             for obj in self.objetos:
  59.                 obj[0].set_position(obj[1], obj[2])
  60.                 obj[0].draw()
  61.  
  62.             for textobj in self.textobjs:
  63.                 game.screen.blit(textobj[0], textobj[1])
  64.  
  65.             self.janela.update()
  66.     def setbackground(self, obj):
  67.         self.background = obj
  68.  
  69.     def setborders(self, borders):
  70.         self.borders = borders
  71.  
  72.     def addobject(self, obj, w = 0, h = 0, posx = 0, posy = 0, v = [0, 0]):
  73.         #[obj, posx, posy, vetor, w, h, id]
  74.         obj.set_position(posx, posy)
  75.         tmp = []
  76.         tmp.append(obj)
  77.         tmp.append(posx)
  78.         tmp.append(posy)
  79.         tmp.append(v)
  80.         tmp.append(w)
  81.         tmp.append(h)
  82.         tmp.append(len(self.objetos))
  83.         self.objetos.append(tmp)
  84.         return len(self.objetos) - 1
  85.     def addtextobj(self, obj, pos):
  86.         self.textobjs.append([obj, pos])
  87.  
  88.     def bindkeyboard(self, f, objid = False):
  89.         #self.janela, self.teclado, self.objetos[objid]
  90.         self.keybinds.append([f, objid])
  91.         return len(self.keybinds) - 1
  92.  
  93.     def bindevent(self, f, objid = False, args = False):
  94.         self.eventbinds.append([f, objid, args])
  95.  
  96. # Inicio do Jogo:
  97.  
  98. def moverbat(game, teclado, obj):
  99.     janela = game.janela
  100.     borders = game.borders
  101.     if teclado.key_pressed("down"):
  102.         obj[2] += obj[3][1] * janela.delta_time()
  103.     if teclado.key_pressed("up"):
  104.         obj[2] -= obj[3][1] * janela.delta_time()
  105.  
  106.     if obj[1] > borders[1]-obj[4]:
  107.         obj[1] = borders[1]-obj[4]
  108.     elif obj[1] < borders[0]:
  109.         obj[1] = borders[0]
  110.     if obj[2] > borders[3]-obj[5]:
  111.         obj[2] = borders[3]-obj[5]
  112.     elif obj[2] < borders[2]:
  113.         obj[2] = borders[2]
  114.  
  115.     return
  116.  
  117. def moverbat2(game, teclado, obj):
  118.     janela = game.janela
  119.     borders = game.borders
  120.     if teclado.key_pressed("s"):
  121.         obj[2] += obj[3][1] * janela.delta_time()
  122.     if teclado.key_pressed("w"):
  123.         obj[2] -= obj[3][1] * janela.delta_time()
  124.  
  125.     if obj[1] > borders[1]-obj[4]:
  126.         obj[1] = borders[1]-obj[4]
  127.     elif obj[1] < borders[0]:
  128.         obj[1] = borders[0]
  129.     if obj[2] > borders[3]-obj[5]:
  130.         obj[2] = borders[3]-obj[5]
  131.     elif obj[2] < borders[2]:
  132.         obj[2] = borders[2]
  133.  
  134.     return
  135.  
  136. def speed(vx, vy):
  137.     return sqrt(vx**2 + vy**2)
  138.  
  139. def endGame(game, win):
  140.     text = wintext if win else losetext
  141.     game.objetos = []
  142.     game.keybinds = []
  143.     game.eventbinds = []
  144.     game.textobjs = []
  145.     game.setbackground([0, 0, 0])
  146.     game.addtextobj(text, [(game.width-text.get_width())/2, (game.heigth-text.get_height())/2])
  147.     game.bindkeyboard(startGame)
  148.     game.setbackground((0, 0, 0))
  149.     game.addtextobj(starttext, [(game.width-starttext.get_width())/2, (game.heigth-starttext.get_height())/2 + 40])
  150.  
  151. def moverball(game, obj):
  152.     janela = game.janela
  153.     borders = game.borders
  154.     for objc in game.objetos:
  155.         if objc[6] != obj[6]:
  156.             if(obj[7] != objc[6] and obj[0].collided(objc[0])):
  157.                 f = obj[5]/2
  158.                 a = 1
  159.                 b = 1
  160.                 if(obj[3][0] > 0):
  161.                     a = -1
  162.                     b = 1
  163.                     f = 0
  164.                 else:
  165.                     a = 1
  166.                     b = -1
  167.                    
  168.                 intersect = (objc[2]+(objc[5]/2)) - (obj[2] + f)
  169.                 nintersect = (intersect/(objc[5]/2))
  170.                 obj[8] = nintersect * 5*Pi/12
  171.                 curspeed = speed(obj[3][0], obj[3][1])
  172.                
  173.                 obj[3][0] = curspeed*cos(obj[8])*a
  174.                 obj[3][1] = curspeed*sin(obj[8])*b
  175.                 obj[7] = objc[6]
  176.                 break
  177.  
  178.     obj[1] += obj[3][0] * janela.delta_time()
  179.     obj[2] += obj[3][1] * janela.delta_time()
  180.  
  181.     if obj[1] > borders[1]-obj[4]:
  182.         endGame(game, True)
  183.         global scoreA
  184.         scoreA = scoreA + 1
  185.     elif obj[1] < borders[0]:
  186.         endGame(game, False)
  187.         global scoreB
  188.         scoreB = scoreB + 1
  189.     if obj[2] > borders[3]-obj[5]:
  190.         obj[2] = borders[3]-obj[5]
  191.         obj[3][1] = -obj[3][1]
  192.     elif obj[2] < borders[2]:
  193.         obj[2] = borders[2]
  194.         obj[3][1] = -obj[3][1]
  195.  
  196.     return
  197.  
  198. def moverbatia(game, obj, args):
  199.     ballobj = game.objetos[args[0]]
  200.     janela = game.janela
  201.     borders = game.borders
  202.     if ballobj[2] > obj[2] + obj[5]/2:
  203.         obj[2] += obj[3][1] * janela.delta_time()
  204.     if ballobj[2] < obj[2] + obj[5]/2:
  205.         obj[2] -= obj[3][1] * janela.delta_time()
  206.  
  207.     if obj[1] > borders[1]-obj[4]:
  208.         obj[1] = borders[1]-obj[4]
  209.     elif obj[1] < borders[0]:
  210.         obj[1] = borders[0]
  211.     if obj[2] > borders[3]-obj[5]:
  212.         obj[2] = borders[3]-obj[5]
  213.     elif obj[2] < borders[2]:
  214.         obj[2] = borders[2]
  215.  
  216.     return
  217.  
  218. def startGame(game, teclado):
  219.     if teclado.key_pressed("space"):
  220.         game.keybinds = []
  221.         game.textobjs = []
  222.         game.setborders([0, 640, 5, 475])
  223.         game.setbackground(Sprite("background.png"))
  224.         scoretext = scorefont.render(str(scoreA) + "  -  " + str(scoreB), 0,(255,255,255))
  225.         game.addtextobj(scoretext, [(game.width-scoretext.get_width())/2, 5])
  226.         ball = game.addobject(Sprite("ball.png"), 10, 10, 315, 235, [-400, 0])
  227.         game.objetos[ball].append(-1) # last collision
  228.         game.objetos[ball].append(0) # angle
  229.         batone = game.addobject(Sprite("bat.png"), 10, 80, 0, 200, [0, 300])
  230.         game.objetos[batone].append(0)
  231.         battwo = game.addobject(Sprite("bat.png"), 10, 80, 630, 200, [0, 310])
  232.         game.objetos[battwo].append(0)
  233.         game.bindkeyboard(moverbat, batone)
  234.         #game.bindevent(moverbatia, batone, [ball])
  235.         game.bindevent(moverbatia, battwo, [ball])
  236.         game.bindevent(moverball, ball)
  237.  
  238. game = Game(640, 480)
  239. game.janela.set_title("Pong")
  240. game.bindkeyboard(startGame)
  241. game.setbackground((0, 0, 0))
  242. game.addtextobj(starttext, [(game.width-starttext.get_width())/2, (game.heigth-starttext.get_height())/2])
  243.  
  244. while(1):
  245.     game.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement