RootOfTheNull

Statix SC's Corrected Code

Apr 11th, 2015
265
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. ###################################################################
  3. pygame.init()
  4. ###################################################################
  5. WHITE=(255,255,255)
  6. RED=(255,0,0)
  7. BLUE=(0,0,255)
  8.  
  9. screenWidth=600
  10. screenHeight=800
  11. BlockSize=50
  12. FPS=30
  13.  
  14. screen=pygame.display.set_mode((screenHeight,screenWidth))
  15. Clock=pygame.time.Clock()
  16.  
  17.  
  18. class cBall(pygame.sprite.Sprite):
  19.     def __init__ (self, color=RED, width=64, height=64):
  20.         super(cBall,self).__init__()
  21.         self.image=pygame.Surface((width,height))
  22.         self.image.fill(color)
  23.         self.rect=self.image.get_rect()
  24.         self.hs=0
  25.         self.vs=0
  26.  
  27.  
  28.     def set_position(self,x,y):
  29.         self.rect.x=x
  30.         self.rect.y=y
  31.  
  32.     def set_image(self,filename=None):
  33.         if (filename!=None):
  34.             self.image=pygame.image.load(filename)
  35.             self.rect=self.image.get_rect()
  36.  
  37.     def change_speed(self, hs, vs):
  38.         self.hs += hs
  39.         self.hs += vs
  40.  
  41.     def update(self):
  42.         self.rect.x += self.hs
  43.         self.rect.y += self.vs
  44.  
  45.  
  46.  
  47. ##################################################################
  48.  
  49.  
  50. # Note that I moved these three lines OUTSIDE of the While loop, and before them,
  51. # so that object can be initialized and have its position set and it does not overwrite
  52. # itself over and over again in the loop.
  53. ball = cBall()
  54.  
  55. ball.set_image("brick2.png")
  56.  
  57. ball.set_position(0,0)
  58.  
  59. sp_group=pygame.sprite.Group()
  60. sp_group.add(ball)
  61.  
  62. GameExit=False
  63. while not GameExit:
  64.  
  65.     for event in pygame.event.get():
  66.         if event.type == pygame.QUIT:
  67.             GameExit=True
  68.             pygame.quit()
  69.         if event.type==pygame.KEYDOWN:
  70.             if event.key==pygame.K_RIGHT:
  71.                 print("keypressed")
  72.                 ball.change_speed(5,0)
  73.  
  74.     # Logic to correct the ball's position...
  75.     ball.update()
  76.  
  77.     # All the drawing functions to refresh the screen...
  78.     screen.fill(WHITE)
  79.     sp_group.draw(screen)
  80.  
  81.     # Set the FPS and then actually refresh the screen for real.s
  82.     Clock.tick(FPS)
  83.     pygame.display.update()
  84.  
  85. ##################################################################
  86. pygame.quit()
RAW Paste Data