python - What's the error in this pygame program? -


i'm making pong game in pygame isn't complete yet. names of variables, classes , other things in code in portuguese think that's easy know wich 1 does. code should make 2 paddles move when user wants displays background , paddles. don't error message.

here's code:

import pygame,sys,os,time pygame.locals import * pygame.init()  janela=pygame.display.set_mode((800, 533),0,32) pygame.display.set_caption("pong!") superficie=pygame.display.get_surface()  clock=pygame.time.clock()  fundoficheiro=os.path.join("imagens","fundo2.png") fundoimagem=pygame.image.load(fundoficheiro).convert()  barraficheiro=os.path.join("imagens","barra.png") barraimagem=pygame.image.load(barraficheiro).convert_alpha()  def imprimirbarra(x,y):         janela.blit(barraimagem,(x,y))     class barra():     y=1     x=1     velocidade=1     def subir(self):         self.y-=self.velocidade     def descer(self):         self.y+=self.velocidade     def parar(self):         self.velocidade=0     def comecar(self):         self.velocidade=1         barra1=barra() barra1.x=5 barra1.y=superficie.get_height()/2-barraimagem.get_height()/2 barra2=barra() barra2.velocidade=1 barra2.x=superficie.get_width()-(barraimagem.get_width()+5) barra2.y=superficie.get_height()/2-barraimagem.get_height()/2  def interface():     event in pygame.event.get():         if event.type==keydown:             if event.key==k_up:                 barra2.comecar()                 barra2.subir()             elif event.key==k_down:                 barra2.comecar()                 barra2.descer()             elif event.key==k_w:                 barra1.comecar()                 barra1.subir()             elif event.key==k_s:                 barra1.comecar()                 barra1.descer()         elif event.type==keyup:             if event.key==k_up:                 barra2.parar()             elif event.key==k_down:                 barra2.parar()             elif event.key==k_w:                 barra1.parar()             elif event.key==k_s:                 barra1.parar()     while true:     clock.tick(60)     event in pygame.event.get():         if event.type==quit:             pygame.quit()             sys.exit()     janela.blit(fundoimagem,(0,0))             imprimirbarra(barra1.x,barra1.y)     imprimirbarra(barra2.x,barra2.y)     interface()     pygame.display.update() 

for event in pygame.event.get():     if event.type==quit:         pygame.quit()         sys.exit() 

pygame.event.get() gives events occurred since last time called it. loop uses events available. therefore, when interface() called, , tries pygame.event.get() again, there no more events left check key presses (unless happen occur while janela.blit calls being made, should fast; not sure if pygame process events during calls anyway).

another problem keydown events when key pressed, not continuously while held (unless configure pygame.key.set_repeat(), may interfere rest of logic). have code set try move paddle continuously while key held down, won't since .subir or .descer gets called once per key press.

there lot of ways fix this, in case have re-structure things bit. suggestion have 1 loop in program handles events, , translates them kind of data structure (e.g., have set of keys pressed down, , keys changed since last time). might able simplify pygame.key.get_pressed(), beware can miss quick key taps way.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -