Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- ###################################################################
- pygame.init()
- ###################################################################
- WHITE=(255,255,255)
- RED=(255,0,0)
- BLUE=(0,0,255)
- screenWidth=600
- screenHeight=800
- BlockSize=50
- FPS=30
- screen=pygame.display.set_mode((screenHeight,screenWidth))
- Clock=pygame.time.Clock()
- class cBall(pygame.sprite.Sprite):
- def __init__ (self, color=RED, width=64, height=64):
- super(cBall,self).__init__()
- self.image=pygame.Surface((width,height))
- self.image.fill(color)
- self.rect=self.image.get_rect()
- self.hs=0
- self.vs=0
- def set_position(self,x,y):
- self.rect.x=x
- self.rect.y=y
- def set_image(self,filename=None):
- if (filename!=None):
- self.image=pygame.image.load(filename)
- self.rect=self.image.get_rect()
- def change_speed(self, hs, vs):
- self.hs += hs
- self.hs += vs
- def update(self):
- self.rect.x += self.hs
- self.rect.y += self.vs
- ##################################################################
- # Note that I moved these three lines OUTSIDE of the While loop, and before them,
- # so that object can be initialized and have its position set and it does not overwrite
- # itself over and over again in the loop.
- ball = cBall()
- ball.set_image("brick2.png")
- ball.set_position(0,0)
- sp_group=pygame.sprite.Group()
- sp_group.add(ball)
- GameExit=False
- while not GameExit:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- GameExit=True
- pygame.quit()
- if event.type==pygame.KEYDOWN:
- if event.key==pygame.K_RIGHT:
- print("keypressed")
- ball.change_speed(5,0)
- # Logic to correct the ball's position...
- ball.update()
- # All the drawing functions to refresh the screen...
- screen.fill(WHITE)
- sp_group.draw(screen)
- # Set the FPS and then actually refresh the screen for real.s
- Clock.tick(FPS)
- pygame.display.update()
- ##################################################################
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement