Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Title
- # Description
- # Bobby Clarke
- #Imports
- import pygame
- import sys
- import random
- from pygame.locals import *
- pygame.init()
- pygame.font.init()
- clock = pygame.time.Clock()
- HEIGHT = 600
- WIDTH = round(HEIGHT * ((1 + 5 ** 0.5) / 2))
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Fruit Clickin'")
- def deNegate(i):
- return (i ** 2) ** 0.5
- class Fruit():
- def __init__(self, fruit_type):
- self.type = fruit_type
- self.xspeed = random.randint(-10, 10)
- self.yspeed = random.randint(-10, 10)
- self.loadimg()
- self.width = self.img.get_width()
- self.height = self.img.get_height()
- self.x, self.y = self.genSpawnLoc()
- self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
- self.dead = False
- def loadimg(self):
- img = pygame.image.load("Fruit/" + self.type + ".png")
- self.img = pygame.transform.scale(img, (48, 48))
- def draw(self):
- screen.blit(self.img, (self.x, self.y))
- def move(self):
- self.x += self.xspeed
- self.y += self.yspeed
- if self.x < 0 or self.x > WIDTH - self.width:
- if self.x > WIDTH - self.width:
- self.x = WIDTH - self.width
- self.xspeed = -(self.xspeed)
- elif self.y < 0 or self.y > HEIGHT - self.height:
- if self.y > HEIGHT - self.height:
- self.y = HEIGHT - self.height
- self.yspeed = -(self.yspeed)
- self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
- def onClick(self, sm):
- if self.type == "bomb":
- sm.changeScore(-20)
- else:
- sm.changeScore(deNegate(self.xspeed) + deNegate(self.yspeed))
- self.dead = True
- def genSpawnLoc(self):
- x = random.randint(self.width, WIDTH - self.width)
- y = random.randint(self.height, HEIGHT - self.height)
- return x, y
- class ScoreManager():
- def __init__(self):
- self.font = pygame.font.SysFont("Arial", 16)
- self.score = 0
- self.scoreText = self.font.render("Score: " + str(self.score), True,
- (0, 0, 0))
- def changeScore(self, change):
- self.score += change # Negative numbers work
- self.scoreText = self.font.render("Score: " + str(self.score), True,
- (0, 0, 0))
- def drawScoreText(self):
- screen.blit(self.scoreText, (0, 0))
- def main():
- fruits = []
- sm = ScoreManager()
- fruitTypes = ("apple", "bannana", "pear", "orange", "bomb")
- framerate = 30
- bgColour = (255, 255, 255) #White
- while True:
- clock.tick(framerate)
- screen.fill(bgColour)
- if random.randint(0, 1/2 * framerate) == 1 and len(fruits) < 50:
- fruits.append(Fruit(random.choice(fruitTypes)))
- for fruit in fruits:
- fruit.draw()
- fruit.move()
- if fruit.dead:
- fruits.remove(fruit)
- sm.drawScoreText()
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit(0)
- elif event.type == MOUSEBUTTONDOWN:
- for fruit in fruits:
- if fruit.rect.collidepoint(pygame.mouse.get_pos()):
- fruit.onClick(sm)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement