I'm not fully understanding what I need to do to turn my game into a Creator-Defined Displayable. I'm especially confused about what to do with collision detection in Ren'Py. When it comes to collision detection in pygame, every image gets a rect defined around it and you just use "player_rect.colliderect(coin_rect)" but I imagine I'm going to have to do something differently for Ren'Py. Any ideas on this and porting the game in general?
Here's my pygame code:
Code: Select all
import random
import pygame
# Initialize pygame
pygame.init()
# Set display surface
WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 400
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Feed the Dragon")
# Set FPS and clock
FPS = 60
clock = pygame.time.Clock()
# Set game values
PLAYER_STARTING_LIVES = 5
PLAYER_VELOCITY = 10
COIN_STARTING_VELOCITY = 10
COIN_ACCELERATION = 0.5
BUFFER_DISTANCE = 100
score = 0
player_lives = PLAYER_STARTING_LIVES
coin_velocity = COIN_STARTING_VELOCITY
# Set colors
GREEN = (0, 255, 0)
DARKGREEN = (10, 50, 10)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set fonts
font = pygame.font.Font("AttackGraffiti.ttf", 32)
# Set text
score_text = font.render("Score: " + str(score), True, GREEN, DARKGREEN)
score_rect = score_text.get_rect()
score_rect.topleft = (10, 10)
title_text = font.render("Feed the Dragon", True, GREEN, DARKGREEN)
title_rect = title_text.get_rect()
title_rect.centerx = WINDOW_WIDTH//2
title_rect.y = 10
lives_text = font.render("Lives: " + str(player_lives), True, GREEN, DARKGREEN)
lives_rect = lives_text.get_rect()
lives_rect.topright = (WINDOW_WIDTH - 10, 10)
game_over_text = font.render("GAME OVER", True, GREEN, DARKGREEN)
game_over_rect = game_over_text.get_rect()
game_over_rect.center = (WINDOW_WIDTH//2, WINDOW_HEIGHT//2)
continue_text = font.render("Press Enter to play again", True, GREEN, DARKGREEN)
continue_rect = continue_text.get_rect()
continue_rect.center = (WINDOW_WIDTH//2, WINDOW_HEIGHT//2 + 32)
# Set sounds and music
coin_sound = pygame.mixer.Sound("coin_sound.wav")
miss_sound = pygame.mixer.Sound("miss_sound.wav")
miss_sound.set_volume(0.1)
pygame.mixer.music.load("ftd_background_music.wav")
# Set images
player_image = pygame.image.load("dragon_right.png")
player_rect = player_image.get_rect()
player_rect.left = 32
player_rect.centery = WINDOW_HEIGHT//2
coin_image = pygame.image.load("coin.png")
coin_rect = coin_image.get_rect()
coin_rect.x = WINDOW_WIDTH + BUFFER_DISTANCE
coin_rect.y = random.randint(64, WINDOW_HEIGHT - 32)
# The main game loop
pygame.mixer.music.play(-1, 0.0)
running = True
while running:
# Check to see if user wants to quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Check to see if the user wants to move
keys = pygame.key.get_pressed()
if (keys[pygame.K_UP] or keys[pygame.K_w]) and player_rect.top > 64:
player_rect.y -= PLAYER_VELOCITY
if (keys[pygame.K_DOWN] or keys[pygame.K_s]) and player_rect.bottom < WINDOW_HEIGHT:
player_rect.y += PLAYER_VELOCITY
# Move the coin
if coin_rect.x < 0:
# Player missed the coin
player_lives -= 1
miss_sound.play()
coin_rect.x = WINDOW_WIDTH + BUFFER_DISTANCE
coin_rect.y = random.randint(64, WINDOW_HEIGHT - 32)
else:
# Move the coin
coin_rect.x -= coin_velocity
# Check for collisions
if player_rect.colliderect(coin_rect):
score += 1
coin_sound.play()
coin_velocity += COIN_ACCELERATION
coin_rect.x = WINDOW_WIDTH + BUFFER_DISTANCE
coin_rect.y = random.randint(64, WINDOW_HEIGHT - 32)
# Update HUD
score_text = font.render("Score: " + str(score), True, GREEN, DARKGREEN)
lives_text = font.render("Lives: " + str(player_lives), True, GREEN, DARKGREEN)
# Check for game over
if player_lives == 0:
display_surface.blit(game_over_text, game_over_rect)
display_surface.blit(continue_text, continue_rect)
pygame.display.update()
# Pause the game until player presses a key, then reset the game
pygame.mixer.music.stop()
is_paused = True
while is_paused:
for event in pygame.event.get():
# The player wants to play again
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
score = 0
player_lives = PLAYER_STARTING_LIVES
player_rect.y = WINDOW_HEIGHT//2
coin_velocity = COIN_STARTING_VELOCITY
pygame.mixer.music.play(-1, 0.0)
is_paused = False
# The player wants to quit
if event.type == pygame.QUIT:
is_paused = False
running = False
# Fill the display
display_surface.fill(BLACK)
# Blit the HUD to screen
display_surface.blit(score_text, score_rect)
display_surface.blit(title_text, title_rect)
display_surface.blit(lives_text, lives_rect)
pygame.draw.line(display_surface, WHITE, (0, 64), (WINDOW_WIDTH, 64), 2)
# Blit assets to screen
display_surface.blit(player_image, player_rect)
display_surface.blit(coin_image, coin_rect)
# Update display and tick the clock
pygame.display.update()
clock.tick(FPS)
# End the game
pygame.quit()