Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- from pygame.locals import *
- import random
- import pygame
- import game_functions
- screen_size = w,h=480,620
- caption = "Smash-up"
- bgcol=(0,0,0)
- #A superclass for all on-screen things
- # makes writing the sub-classes (ball, block, bat) a little less
- # tedious
- class Thing(object):
- def __init__(self, x,y,w,h):
- self.rect=pygame.Rect(x,y,w,h)
- self.color=(255,255,255)
- def setColor(self,r,g,b):
- self.color=(r,g,b)
- class Bat(Thing):
- #Bat constructor takes one parameter more than the superclass
- #"speed" controls how fast the bat moves
- def __init__(self,x,y,w,h,speed):
- Thing.__init__(self,x,y,w,h)
- self.speed=speed
- def move(self,distance):
- self.rect.left+=distance*self.speed
- class Block(Thing):
- def __init__(self,x,y,w,h):
- Thing.__init__(self,x,y,w,h)
- #blocks don't do much, but they do need nice randomish colours
- self.color=(random.randint(0,150),
- random.randint(100,250),
- random.randint(0,150))
- class Ball(Thing):
- #The ball has a dx and dy parameter to control the direction of
- #movement. "dx" is a small change in x, "dy" a small change in y
- #The speed parameter is used to multiply (scale up) the speed of
- #movement
- def __init__(self,x,y,w,h,dx,dy,speed):
- Thing.__init__(self,x,y,w,h)
- self.speed=speed
- self.dx=dx
- self.dy=dy
- def move(self):
- #MISSING: 1
- #Use self.dx, self.dy and self.speed to move the ball's rect.
- #See the pygame documentation on rects:http://www.pygame.org/docs/ref/rect.html
- self.rect.left += self.dx * self.speed
- self.rect.top += self.dy * self.speed
- def bounceX(self):
- #MISSING: 2
- #Reverse the movement left/right
- self.dx *= -1
- def bounceY(self):
- #MISSING: 3
- #Reverse the movement up/down
- self.dy *= -1
- def main():
- #Initialisation
- pygame.init()
- screen = pygame.display.set_mode(screen_size)
- pygame.display.set_caption(caption)
- clock = pygame.time.Clock()
- #Background image
- bgImage,bgRect=game_functions.load_png("smash_bg.png")
- #Font for score, etc
- font = pygame.font.Font("YanoneTagesschrift.ttf", 22)
- # Draw the game background.
- background = (pygame.Surface(screen.get_size())).convert()
- background.fill(bgcol)
- #"move" is used to determine if the bat should move right (1)
- #or left (-1) or not at all (0)
- move=0
- #Set up bat (parameters are x position, y position, width and
- #height. See class definition above.
- bat=Bat(0,h-10,80,10,20)
- bat.setColor(0,200,0)
- #set up ball
- ball=Ball(w/2,h/2,10,10,1,-1,10)
- ball.setColor(0,200,0)
- #set up blocks
- blocks=[]
- blockwidth=30
- blockheight=20
- rows=8
- for x in range(w/blockwidth):
- for y in range(rows):
- blocks.append(
- Block(x*blockwidth,y*blockheight, blockwidth, blockheight)
- )
- #score
- score=0
- game_functions.start(screen,"Smash-up")
- #Game loop
- while True:
- screen.blit(bgImage, (0,0))
- #Timing - limits to 30 frames per second
- clock.tick(30)
- # Handle events.
- # Pressing a key sets the move variable
- # Releasing a key sets it back to 0
- pygame.event.pump()
- for event in pygame.event.get():
- if event.type == QUIT:
- return
- elif event.type == KEYDOWN:
- if event.key == K_LEFT:
- move=-1
- elif event.key == K_RIGHT:
- move=1
- elif event.type == KEYUP:
- if event.key == K_LEFT:
- move=0
- elif event.key == K_RIGHT:
- move=0
- # Move bat
- bat.move(move)
- bat.rect.clamp_ip(screen.get_rect())
- #move ball
- ball.move()
- #has the bat hit the left or right edge?
- if ball.rect.left<0 or ball.rect.right>w:
- ball.bounceX()
- #has the ball hit the top of the screen?
- if ball.rect.top<0:
- ball.bounceY()
- # Has the ball hit the bat?
- #MISSING: 4
- #If it has, reverse its direction in Y
- #Extra: can you cause the speed of the bat to
- #influence the X speed of the ball?
- if ball.rect.colliderect(bat.rect):
- ball.bounceY()
- # I think that I can =)
- tmp = abs(ball.dx)
- if bat.speed > tmp * ball.speed:
- tmp += 1
- else:
- tmp -= 1 # I know it's foolish but the ball will tend to get bat's speed
- if ball.dx < 0:
- tmp *= -1
- ball.dx = tmp
- #Has the ball hit the bottom of the screen?
- #MISSING: 5
- #If it has, make it bounce. This should affect the
- #player's score.
- if ball.rect.bottom > h:
- ball.bounceY()
- score += ball.speed # the faster the ball moves the faster will increase the score
- #has the ball hit any blocks?
- #MISSING: 6
- #If it has:
- # 1 - Remove the block from the list
- # 2 - Increase the player score
- # 3 - Make the ball bounce
- # Extra: can you determine if the ball hit the
- # bottom/top/left/right edge? And bounce accordingly?
- #HINT: look at the rect documentation again.
- for block in blocks:
- if block.rect.colliderect(ball.rect):
- blocks.remove(block)
- score += 100
- # and now the extra
- ### plm chiar nu-mi pot da seama ... daca se muta doar pixel cu pixel atunci era simplu:
- # verificai daca muchia de jos a mingii a atins muchia de sus a obstacolului sau invers
- # apoi verificai si daca macar una din muchiile laterale ale mingii se afla in interiorul obstacolului
- # si bam asta insemna ca se respinge pe Y (bounceY)
- # analog faceai si pentru X numai ca verificai daca atinge lateralele si daca macar una dintre top si bottom
- # ale mingii se afla in interior
- # asta doar daca mutarea obiectelor se facea pixel cu pixel ca sa fie posibila atingerea marginilor, dar asa cand sunt mutati
- # mai multi pixeli deodata se pot intersecta cazurile ... totusi ai cum sa-ti dai seama prin diferente
- # tinand cont de valorile dinaintea impactului si prin compararea diferentelor dintre muchii in relatie cu viteza
- # ai putea simula si afla in urma ciocnirii care latura a fost atinsa prima cea de pe X sau de pe Y :)
- #Draw the blocks
- for block in blocks:
- pygame.draw.rect(screen,(128,128,128),block.rect.move(2,2),2) #Light
- pygame.draw.rect(screen,(28,28,28),block.rect.move(-2,-2),2) #Shadow
- pygame.draw.rect(screen,block.color,block.rect.inflate(-2,-2)) #Colour
- #Draw the bat
- pygame.draw.rect(screen,bat.color,bat.rect)
- #Draw the ball
- pygame.draw.rect(screen,ball.color,ball.rect)
- #Display the score
- text = font.render("Score: %d"%score, True, (0,0,0))
- screen.blit(text, (2,2))
- text = font.render("Score: %d"%score, True, (255,255,255))
- screen.blit(text, (0,0))
- #Flip the backbuffer to the main buffer
- pygame.display.flip()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement