Reputation: 29
Here's the game
import pygame
import random
import time
pygame.init()
backX = 1000
backY = 600
screen = pygame.display.set_mode((backX, backY))
restarttrue = 1
score = 0
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
timespent = 0
pygame.display.set_caption('Monkey Simulator') # game name
pygame.font.init() # you have to call this at the start,
# if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render('Score: ' + str(score), False, (255, 255, 255))
pygame.mixer.init()
# music
sound = pygame.mixer.Sound('background1.mp3')
collection = pygame.mixer.Sound('collection.mp3')
gameover1 = pygame.mixer.Sound('gameover.mp3')
sound.play(-1)
# score indicator
text = myfont.render('Score: ' + str(score), True, white, blue)
textRect = text.get_rect() # getting the rectangle for the text object
textRect.center = (400 // 2, 400 // 2)
pre_background = pygame.image.load('background.jpeg')
background = pygame.transform.scale(pre_background, (backX, backY))
clock = pygame.time.Clock()
FPS = 60
vel = 6.5
BLACK = (0, 0, 0)
monkeyimg = pygame.image.load('monkey.png')
playerX = 410
playerY = 435
bananavelocity = 4
monkey = pygame.transform.scale(monkeyimg, (100, 120))
prebanana = pygame.image.load('banana.png')
bananaX = random.randint(10, 980)
bananaY = 0
banana = pygame.transform.scale(prebanana, (50, 50))
underwater = pygame.image.load('underwater.jpg')
pre_shark = pygame.image.load('shark.png')
pre_meat = pygame.image.load('meat.png')
pre_mouse = pygame.image.load('mouse.png')
pre_cheese = pygame.image.load('cheese.png')
city = pygame.image.load('house.png')
change_list = [[pre_background, pre_money, prebanana],[underwater, pre_shark, pre_meat], [city, pre_mouse, pre_cheese], [pre_background,]]
def change(score):
global monkey, banana, background, pre_shark, pre_meat, underwater
for i in range(len(change_list)):
if(score == 5**i and score != 1):
monkey = pygame.transform.scale(change_list[i][1], (100, 100))
banana = pygame.transform.scale(change_list[i][2], (50, 50))
background = pygame.transform.scale(change_list[i][0], (backX, backY))
banana_rect = banana.get_rect(topleft=(bananaX, bananaY))
monkey_rect = monkey.get_rect(topleft=(playerX, playerY))
run = True
black = (0, 0, 0)
# start screen
screen.blit(background, (0, 0))
myfont = pygame.font.SysFont("Britannic Bold", 50)
end_it = False
change(1)
while not end_it:
myfont1 = pygame.font.SysFont("Britannic Bold", 35)
nlabel = myfont.render("Monkey Simulator", 1, (255, 255, 255))
info = myfont1.render("Use your right and left arrow keys to move the character.", 1, (255, 255, 255))
info2 = myfont1.render("Try to catch as many bananas as you can while the game speeds up!", 1, (255, 255, 255))
info3 = myfont1.render("Click anywhere to start.", 1, (255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
end_it = True
screen.blit(nlabel, (400, 150))
screen.blit(info, (150, 300))
screen.blit(info2, (150, 350))
screen.blit(info3, (150, 400))
pygame.display.flip()
while run:
clock.tick(FPS)
screen.fill(BLACK)
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
# banana animation
bananaY = bananaY + bananavelocity
timespent = int(pygame.time.get_ticks() / 1000)
bananavelocity = 4 + (timespent * 0.065)
vel = 6.5 + (timespent * 0.065)
# end game sequence
change(score)
if bananaY > 510:
bananaX = -50
bananaY = -50
restarttrue = 1
velocity = 0
gameover = pygame.image.load("gameover.jpg")
background = pygame.transform.scale(gameover, (backX, backY))
pygame.mixer.Sound.stop(sound)
gameover1.play()
# collecting coins sequence
if banana_rect.colliderect(monkey_rect):
collection.play()
bananaX = random.randint(10, 980)
bananaY = -25
score += 1
# moving character
if keys[pygame.K_LEFT] and playerX > 0:
playerX = playerX - vel
if keys[pygame.K_RIGHT] and playerX < 930:
playerX = playerX + vel
# adding the sprites to the screen
screen.blit(monkey, (playerX, playerY))
screen.blit(banana, (bananaX, bananaY))
banana_rect = banana.get_rect(topleft=(bananaX, bananaY))
monkey_rect = monkey.get_rect(topleft=(playerX, playerY))
# pygame.draw.rect(screen, (150, 75, 0), pygame.Rect(0, 534, 1000, 20))
screen.blit(textsurface, (30, 0))
textsurface = myfont.render('Score: ' + str(score), False, (255, 255, 255))
pygame.display.update()
It's a game where you control a character in two directions and have them collect something falling. The first set is a monkey collecting a banana on a jungle background. The second set is a shark collecting meat in the ocean. The last set is a mouse collecting cheese in a house. The last set doesn't work however. It's hard to explain so I'll put a screencast here. https://drive.google.com/file/d/1jgb7EPEPkN0XFV3kOcG-UGhfp9WalUR4/view Skip to 0:42 for what I'm talking about
Upvotes: 1
Views: 39
Reputation: 210878
pygame.transform.scale
is a time consuming operation. Scale the Sprites when loading them instead of in the change
function:
change_list = [
[pygame.transform.scale(img, (backX, backY)) for img in [pre_background, pre_money, prebanana]],
[pygame.transform.scale(img, (100, 100)) for img in [underwater, pre_shark, pre_meat]],
[pygame.transform.scale(img, (50, 50)) for img in [city, pre_mouse, pre_cheese]],
[pre_background,]
]
def change(score):
global monkey, banana, background, pre_shark, pre_meat, underwater
for i in range(len(change_list)):
if score == 5**i and score != 1:
monkey = change_list[i][1]
banana = change_list[i][2]
background = change_list[i][0]
Upvotes: 1