Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- ##########################################################################################
- BLACK=(0,0,0)
- WHITE=(220,220,220)
- BLUE=(0,0,155)
- RED=(155,0,0)
- YELLOW=(255,255,0)
- FPS=30
- screenwidth=800
- screenheight=800
- padding=50
- screen=pygame.display.set_mode( (screenwidth,screenheight) )
- clock=pygame.time.Clock()
- ##########################################################################################
- class Ball(pygame.sprite.Sprite):
- def __init__(self,x,y,width=20,height=20, color=WHITE):
- super(Ball,self).__init__()
- self.image=pygame.Surface((width,height))
- self.image.fill(color)
- self.rect=self.image.get_rect()
- self.rect.x=x
- self.rect.y=y
- self.dx=1
- self.dy=1
- def move(self):
- self.rect.x += self.dx
- self.rect.y += self.dy
- def update(self, collide=pygame.sprite.Group()):
- self.move()
- if self.rect.right > screenwidth:
- self.dx *= -1
- elif self.rect.bottom > screenheight:
- self.dy *= -1
- if self.rect.left <= 0:
- self.dx *= -1
- elif self.rect.top < 0:
- self.dy *= -1
- collision_list=pygame.sprite.spritecollide(self,collide,False)
- for collide in collision_list:
- # !!!!!!!!!!!!!!!!!
- # So since all you really do when you collide with a paddle is just
- # bounce off it, like reverse the horizontal direction it is going in,
- # you don't really need to test anything, like which direction you are
- # going in or how you are moving. All you need to do is reverse your dx.
- #self.dx *= -1
- # If you want more precise collisions, you can test for the direction you
- # are moving in ( if your horizontal direction is positive or negative )
- # and then match the edge you are on with the edge you are hitting it with.
- # Like this...
- if self.dx > 0: # MOVING TO THE RIGHT, RIGHT SIDE IS WHAT HITS
- self.rect.right = collide.rect.left # Jump to the edge, just for that moment
- self.dx *= -1 # Still bounce
- continue # Get out of this iteration so we don't test for any other edges
- if self.dx < 0: # MOVING TO THE LEFT, LEFT SIDE IS WHAT HITS
- self.rect.left = collide.rect.right # Jump to the edge, just for that moment
- self.dx *= -1 # Still bounce
- continue# Get out of this iteration so we don't test for any other edges
- class Paddle(pygame.sprite.Sprite):
- def __init__(self,x,y,width=20,height=150, color=WHITE):
- super(Paddle,self).__init__()
- self.image=pygame.Surface((width,height))
- self.image.fill(color)
- self.rect=self.image.get_rect()
- self.rect.x=x
- self.rect.y=y
- self.movespeed=2
- self.pos=0
- def update(self):
- pos=pygame.mouse.get_pos()
- self.rect.y = pos[1]
- ##########################################################################################
- #PRE LOOP STUFF
- ball=Ball(screenwidth/2,screenheight/2-100)
- player=Paddle(0+padding, screenheight/2)
- active_sprites=pygame.sprite.Group()
- active_sprites.add(ball, player)
- collision_list=pygame.sprite.Group()
- collision_list.add(player)
- ##########################################################################################
- gameexit=False
- while not gameexit:
- screen.fill(BLACK)
- for event in pygame.event.get():
- if event.type==pygame.QUIT:
- gameexit=True
- pygame.quit()
- #update functions
- ball.update(collision_list)
- player.update()
- #logic
- #Draw
- active_sprites.draw(screen)
- #delay Frames
- #update screen
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment