Advertisement
xgeovanni

Fruit Clickin'

Dec 6th, 2012
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. # Title
  2. # Description
  3. # Bobby Clarke
  4.  
  5. #Imports
  6. import pygame
  7. import sys
  8. import random
  9. from pygame.locals import *
  10. pygame.init()
  11. pygame.font.init()
  12.  
  13. clock = pygame.time.Clock()
  14. HEIGHT = 600
  15. WIDTH = round(HEIGHT * ((1 + 5 ** 0.5) / 2))
  16. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  17. pygame.display.set_caption("Fruit Clickin'")
  18.  
  19. def deNegate(i):
  20.     return (i ** 2) ** 0.5
  21.  
  22. class Fruit():
  23.     def __init__(self, fruit_type):
  24.         self.type = fruit_type
  25.         self.xspeed = random.randint(-10, 10)
  26.         self.yspeed = random.randint(-10, 10)
  27.  
  28.         self.loadimg()
  29.  
  30.         self.width = self.img.get_width()
  31.         self.height = self.img.get_height()
  32.         self.x, self.y = self.genSpawnLoc()
  33.         self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
  34.  
  35.         self.dead = False
  36.  
  37.     def loadimg(self):
  38.         img = pygame.image.load("Fruit/" + self.type + ".png")
  39.         self.img = pygame.transform.scale(img, (48, 48))
  40.  
  41.     def draw(self):
  42.         screen.blit(self.img, (self.x, self.y))
  43.  
  44.     def move(self):
  45.         self.x += self.xspeed
  46.         self.y += self.yspeed
  47.  
  48.         if self.x < 0 or self.x > WIDTH - self.width:
  49.             if self.x > WIDTH - self.width:
  50.                 self.x = WIDTH - self.width
  51.             self.xspeed = -(self.xspeed)
  52.            
  53.         elif self.y < 0 or self.y > HEIGHT - self.height:
  54.             if self.y > HEIGHT - self.height:
  55.                 self.y = HEIGHT - self.height
  56.             self.yspeed = -(self.yspeed)
  57.  
  58.         self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
  59.  
  60.     def onClick(self, sm):
  61.         if self.type == "bomb":
  62.             sm.changeScore(-20)
  63.         else:
  64.             sm.changeScore(deNegate(self.xspeed) + deNegate(self.yspeed))
  65.  
  66.         self.dead = True
  67.  
  68.     def genSpawnLoc(self):
  69.         x = random.randint(self.width, WIDTH - self.width)
  70.         y = random.randint(self.height, HEIGHT - self.height)
  71.  
  72.         return x, y
  73.  
  74. class ScoreManager():
  75.     def __init__(self):
  76.         self.font = pygame.font.SysFont("Arial", 16)
  77.         self.score = 0
  78.         self.scoreText = self.font.render("Score: " + str(self.score), True,
  79.                                      (0, 0, 0))
  80.  
  81.     def changeScore(self, change):
  82.         self.score += change # Negative numbers work
  83.  
  84.         self.scoreText = self.font.render("Score: " + str(self.score), True,
  85.                                      (0, 0, 0))
  86.  
  87.     def drawScoreText(self):
  88.         screen.blit(self.scoreText, (0, 0))
  89.  
  90. def main():
  91.     fruits = []
  92.     sm = ScoreManager()
  93.     fruitTypes = ("apple", "bannana", "pear", "orange", "bomb")
  94.     framerate = 30
  95.     bgColour = (255, 255, 255) #White
  96.    
  97.     while True:
  98.         clock.tick(framerate)
  99.         screen.fill(bgColour)
  100.  
  101.         if random.randint(0, 1/2 * framerate) == 1 and len(fruits) < 50:
  102.             fruits.append(Fruit(random.choice(fruitTypes)))
  103.  
  104.         for fruit in fruits:
  105.             fruit.draw()
  106.             fruit.move()
  107.  
  108.             if fruit.dead:
  109.                 fruits.remove(fruit)
  110.  
  111.         sm.drawScoreText()
  112.         pygame.display.update()
  113.  
  114.         for event in pygame.event.get():
  115.             if event.type == pygame.QUIT:
  116.                 sys.exit(0)
  117.  
  118.             elif event.type == MOUSEBUTTONDOWN:
  119.                 for fruit in fruits:
  120.                     if fruit.rect.collidepoint(pygame.mouse.get_pos()):
  121.                         fruit.onClick(sm)
  122.  
  123.  
  124. if __name__ == "__main__":
  125.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement