Reputation: 11
import pygame
import random
import time
pygame.init()
backX = 1000
backY = 600
screen = pygame.display.set_mode((backX, backY))
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')
bananaXList = []
def change():
if score>=5:
monkey = pygame.transform.scale(pre_shark, (100, 100))
banana = pygame.transform.scale(pre_meat, (50, 50))
background = pygame.transform.scale(underwater, (backX, backY))
for i in range(50):
value = random.randint(10, 980)
bananaXList.append(value)
valuenumber = random.randint(1, 30)
bananaX = bananaXList[valuenumber - 1]
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')
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
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))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
end_it = True
screen.blit(nlabel, (400, 150))
screen.blit(info, (100, 300))
screen.blit(info2, (100, 350))
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.055)
vel = 6.5 + (timespent * 0.07)
# end game sequence
change()
if bananaY > 510:
bananaX = -50
bananaY = -50
banana
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()
valuenumber = random.randint(1, 30)
bananaX = bananaXList[valuenumber - 1]
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()
Right now I'm making a collection game. I want to use a function for replacing all the images once you hit a certain score. It doesn't work, though, no matter how high of a score you get. I don't understand the issue because I tested both the function and the if statement by putting print statements.
Upvotes: 1
Views: 29
Reputation: 210890
You have to use the global
statement when you want to change a variable in the global namespace within a function:
def change():
global monkey, banana, background
if score>=5:
monkey = pygame.transform.scale(pre_shark, (100, 100))
banana = pygame.transform.scale(pre_meat, (50, 50))
background = pygame.transform.scale(underwater, (backX, backY))
If you don't use the global
statement, the values are assigned to local variables in the scope of the function.
Another option is to return the new values from the function:
def change():
if score>=5:
return (
pygame.transform.scale(pre_shark, (100, 100)),
pygame.transform.scale(pre_meat, (50, 50)),
pygame.transform.scale(underwater, (backX, backY)) )
return monkey, banana, background
while True:
# [...]
monkey, banana, background = change()
Upvotes: 1