Advertisement
cmiN

pygame

Jul 7th, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. from pygame.locals import *
  4. import random
  5. import pygame
  6. import game_functions
  7.  
  8. screen_size = w,h=480,620
  9. caption = "Smash-up"
  10. bgcol=(0,0,0)
  11.  
  12.  
  13. #A superclass for all on-screen things
  14. # makes writing the sub-classes (ball, block, bat) a little less
  15. # tedious
  16. class Thing(object):
  17.     def __init__(self, x,y,w,h):
  18.         self.rect=pygame.Rect(x,y,w,h)
  19.         self.color=(255,255,255)
  20.     def setColor(self,r,g,b):
  21.         self.color=(r,g,b)
  22.  
  23.  
  24.  
  25. class Bat(Thing):
  26.     #Bat constructor takes one parameter more than the superclass
  27.     #"speed" controls how fast the bat moves
  28.     def __init__(self,x,y,w,h,speed):
  29.         Thing.__init__(self,x,y,w,h)
  30.         self.speed=speed
  31.     def move(self,distance):
  32.         self.rect.left+=distance*self.speed
  33.  
  34.  
  35. class Block(Thing):
  36.     def __init__(self,x,y,w,h):
  37.         Thing.__init__(self,x,y,w,h)
  38.         #blocks don't do much, but they do need nice randomish colours
  39.         self.color=(random.randint(0,150),
  40.                     random.randint(100,250),
  41.                     random.randint(0,150))
  42.  
  43.  
  44. class Ball(Thing):
  45.     #The ball has a dx and dy parameter to control the direction of
  46.     #movement. "dx" is a small change in x, "dy" a small change in y
  47.     #The speed parameter is used to multiply (scale up) the speed of
  48.     #movement
  49.     def __init__(self,x,y,w,h,dx,dy,speed):
  50.         Thing.__init__(self,x,y,w,h)
  51.         self.speed=speed
  52.         self.dx=dx
  53.         self.dy=dy
  54.     def move(self):
  55.         #MISSING: 1
  56.         #Use self.dx, self.dy and self.speed to move the ball's rect.
  57.         #See the pygame documentation on rects:http://www.pygame.org/docs/ref/rect.html
  58.         self.rect.left += self.dx * self.speed
  59.         self.rect.top += self.dy * self.speed
  60.     def bounceX(self):
  61.         #MISSING: 2
  62.         #Reverse the movement left/right
  63.         self.dx *= -1
  64.     def bounceY(self):
  65.         #MISSING: 3
  66.         #Reverse the movement up/down
  67.         self.dy *= -1
  68.  
  69. def main():
  70.     #Initialisation
  71.     pygame.init()
  72.     screen = pygame.display.set_mode(screen_size)
  73.     pygame.display.set_caption(caption)
  74.     clock = pygame.time.Clock()
  75.  
  76.         #Background image
  77.         bgImage,bgRect=game_functions.load_png("smash_bg.png") 
  78.  
  79.         #Font for score, etc
  80.         font = pygame.font.Font("YanoneTagesschrift.ttf", 22)
  81.  
  82.     # Draw the game background.
  83.     background = (pygame.Surface(screen.get_size())).convert()
  84.     background.fill(bgcol)
  85.  
  86.         #"move" is used to determine if the bat should move right (1)
  87.         #or left (-1) or not at all (0)        
  88.         move=0
  89.  
  90.         #Set up bat (parameters are x position, y position, width and
  91.         #height.  See class definition above.
  92.         bat=Bat(0,h-10,80,10,20)
  93.         bat.setColor(0,200,0)
  94.  
  95.         #set up ball
  96.         ball=Ball(w/2,h/2,10,10,1,-1,10)
  97.         ball.setColor(0,200,0)
  98.  
  99.         #set up blocks
  100.         blocks=[]
  101.         blockwidth=30
  102.         blockheight=20
  103.         rows=8
  104.         for x in range(w/blockwidth):
  105.             for y in range(rows):
  106.                 blocks.append(
  107.                     Block(x*blockwidth,y*blockheight, blockwidth, blockheight)
  108.                     )
  109.        
  110.  
  111.         #score
  112.         score=0
  113.  
  114.         game_functions.start(screen,"Smash-up")
  115.  
  116.     #Game loop
  117.     while True:
  118.         screen.blit(bgImage, (0,0))    
  119.  
  120.                 #Timing - limits to 30 frames per second
  121.         clock.tick(30)        
  122.  
  123.         # Handle events.
  124.         # Pressing a key sets the move variable
  125.         # Releasing a key sets it back to 0
  126.         pygame.event.pump()
  127.         for event in pygame.event.get():
  128.             if event.type == QUIT:
  129.                 return
  130.             elif event.type == KEYDOWN:
  131.                 if event.key == K_LEFT:
  132.                     move=-1
  133.                 elif event.key == K_RIGHT:
  134.                     move=1
  135.             elif event.type == KEYUP:
  136.                 if event.key == K_LEFT:
  137.                     move=0
  138.                 elif event.key == K_RIGHT:
  139.                     move=0
  140.         # Move bat
  141.                 bat.move(move)
  142.                 bat.rect.clamp_ip(screen.get_rect())
  143.  
  144.                 #move ball
  145.                 ball.move()
  146.                                
  147.        
  148.  
  149.                 #has the bat hit the left or right edge?
  150.                 if ball.rect.left<0 or ball.rect.right>w:
  151.                     ball.bounceX()
  152.                    
  153.                 #has the ball hit the top of the screen?
  154.                 if ball.rect.top<0:
  155.                     ball.bounceY()
  156.  
  157.         # Has the ball hit the bat?
  158.                 #MISSING: 4
  159.                 #If it has, reverse its direction in Y
  160.                 #Extra: can you cause the speed of the bat to
  161.                 #influence the X speed of the ball?
  162.                 if ball.rect.colliderect(bat.rect):
  163.                     ball.bounceY()
  164.                     # I think that I can =)
  165.                     tmp = abs(ball.dx)
  166.                     if bat.speed > tmp * ball.speed:
  167.                         tmp += 1
  168.                     else:
  169.                         tmp -= 1   # I know it's foolish but the ball will tend to get bat's speed
  170.                     if ball.dx < 0:
  171.                         tmp *= -1
  172.                     ball.dx = tmp
  173.  
  174.                 #Has the ball hit the bottom of the screen?
  175.                 #MISSING: 5
  176.                 #If it has, make it bounce.  This should affect the
  177.                 #player's score.
  178.                 if ball.rect.bottom > h:
  179.                     ball.bounceY()
  180.                     score += ball.speed # the faster the ball moves the faster will increase the score
  181.  
  182.                 #has the ball hit any blocks?
  183.                 #MISSING: 6
  184.                 #If it has:
  185.                 # 1 - Remove the block from the list
  186.                 # 2 - Increase the player score
  187.                 # 3 - Make the ball bounce
  188.                 # Extra: can you determine if the ball hit the
  189.                 # bottom/top/left/right edge?  And bounce accordingly?
  190.                 #HINT: look at the rect documentation again.
  191.                 for block in blocks:
  192.                     if block.rect.colliderect(ball.rect):
  193.                         blocks.remove(block)
  194.                         score += 100
  195.                         # and now the extra
  196.                         ### plm chiar nu-mi pot da seama ... daca se muta doar pixel cu pixel atunci era simplu:
  197.                               # verificai daca muchia de jos a mingii a atins muchia de sus a obstacolului sau invers
  198.                               # apoi verificai si daca macar una din muchiile laterale ale mingii se afla in interiorul obstacolului
  199.                               # si bam asta insemna ca se respinge pe Y (bounceY)
  200.                               # analog faceai si pentru X numai ca verificai daca atinge lateralele si daca macar una dintre top si bottom
  201.                               # ale mingii se afla in interior
  202.                        # asta doar daca mutarea obiectelor se facea pixel cu pixel ca sa fie posibila atingerea marginilor, dar asa cand sunt mutati
  203.                        # mai multi pixeli deodata se pot intersecta cazurile ... totusi ai cum sa-ti dai seama prin diferente
  204.                        # tinand cont de valorile dinaintea impactului si prin compararea diferentelor dintre muchii in relatie cu viteza
  205.                        # ai putea simula si afla in urma ciocnirii care latura a fost atinsa prima cea de pe X sau de pe Y :)
  206.  
  207.                 #Draw the blocks
  208.                 for block in blocks:
  209.                     pygame.draw.rect(screen,(128,128,128),block.rect.move(2,2),2) #Light
  210.                     pygame.draw.rect(screen,(28,28,28),block.rect.move(-2,-2),2) #Shadow
  211.                     pygame.draw.rect(screen,block.color,block.rect.inflate(-2,-2)) #Colour
  212.  
  213.                 #Draw the bat
  214.                 pygame.draw.rect(screen,bat.color,bat.rect)
  215.                
  216.                 #Draw the ball
  217.                 pygame.draw.rect(screen,ball.color,ball.rect)
  218.  
  219.                
  220.                 #Display the score
  221.                 text = font.render("Score: %d"%score, True, (0,0,0))
  222.                 screen.blit(text, (2,2))
  223.                 text = font.render("Score: %d"%score, True, (255,255,255))
  224.                 screen.blit(text, (0,0))
  225.  
  226.  
  227.        
  228.         #Flip the backbuffer to the main buffer
  229.         pygame.display.flip()        
  230.  
  231. if __name__ == '__main__':
  232.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement