Renpygame debacle: Python34 vs Renpygame integration

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Aurelius
Newbie
Posts: 20
Joined: Fri Jun 06, 2014 6:56 pm
Projects: Secrets of the Ark, Degrees of Freedom
Organization: Bleau Eye Entertainment
Location: United States
Contact:

Renpygame debacle: Python34 vs Renpygame integration

#1 Post by Aurelius »

Hello everyone,

I was trying to integrate some custom python code for a mini-game sequence in my game but am unable to integrate it properly. I tried following the different tutorials for renpygame, and I tried replacing everything that says pygame with renpygame and adding self to certain objects and still no cigar. I also tried to see an example of the demo of Renpygame, but it crashed right as it launched.

I was using python34 to code this and I am not sure what python renpy is coded in. Is there a problem with compatibility between the various python downloads?

This is my code below what changes do you suggest that should be down with it in order to make it work?

Code: Select all

bif = "bg.jpg"
mif = "RedBall.png"
lif = "LittlePlayer.png"
tif = "Tree.png"
gif = "Girl.png"
wif = "Wall.png"

import pygame, random, sys, os
from pygame.locals import *

ballmove = (0,0)
treenum = 0
wallnum = 0

# Class for the orange dude
class Player(object):
    
    def __init__(self):
        self.rect = pygame.Rect(32, 32, 16, 16)

    def move(self, dx, dy):
        
        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)
    
    def move_single_axis(self, dx, dy):
        
        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        # If you collide with a wall, move out based on velocity
        for wall in collidables:
            if self.rect.colliderect(wall.rect):
                if dx > 0: # Moving right; Hit the left side of the wall
                    self.rect.right = wall.rect.left
                if dx < 0: # Moving left; Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0: # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0: # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom
        for gtree in collidableTree:
            if self.rect.colliderect(gtree.rect):
                if dx > 0: # Moving right; Hit the left side of the tree
                    self.rect.right = gtree.rect.left
                if dx < 0: # Moving left; Hit the right side of the tree
                    self.rect.left = gtree.rect.right
                if dy > 0: # Moving down; Hit the top side of the tree
                    self.rect.bottom = gtree.rect.top
                if dy < 0: # Moving up; Hit the bottom side of the tree
                    self.rect.top = gtree.rect.bottom
    def render():
        pygame.draw.rect(screen, (255, 200, 0), player.rect)


# Nice class to hold a wall rect
class Wall(object):
    
    def __init__(self, pos):
        collidables.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
    def render():
        pygame.draw.rect(screen, (255, 255, 255), wall.rect)
        
class GTree(object):

    def __init__(self, pos):
        collidableTree.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
    def render():
        pygame.draw.rect(screen, (0, 255, 255), GTree.rect)

# Initialise pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()

# Set up the display
pygame.display.set_caption("Get to the red square!")
screen = pygame.display.set_mode((320, 240))

background = pygame.image.load(bif).convert()
ball = pygame.image.load(mif).convert_alpha()
player2d = pygame.image.load(lif).convert_alpha()
tree = pygame.image.load(tif).convert_alpha()
girl = pygame.image.load(gif).convert_alpha()
walles = pygame.image.load(wif).convert_alpha()

clock = pygame.time.Clock()
collidables = [] # List to hold the collision objects
collidableTree = []

player = Player() # Create the player

# Holds the level layout in a list of strings.
level = [
"WWWWWWWWWWWWWWWWWWWW",
"W                  W",
"W         WWWWWW   W",
"W   WWWW       W   W",
"W   W        WWWW  W",
"W WWW  TTTT        W",
"W   W     W W      W",
"W   W     W   WWW WW",
"W   WWW WWW   W W  W",
"W     W   W   W W  W",
"WWW   W   WWWWW W  W",
"W W      WW        W",
"W W   WWWW   WWW   W",
"W     W    E   W   W",
"WWWWWWWWWWWWWWWWWWWW",
]

# Parse the level string above. W = wall, E = exit, T = Tree
x = y = 0
for row in level:
    for col in row:
        if col == "W":
            wallnum += 1
            Wall((x, y))
        if col == "T":
            treenum += 1
            GTree((x, y))
        if col == "E":
            end_rect = pygame.Rect(x, y, 16, 16)
        x += 16
    y += 16
    x = 0

running = True
while running:
    
    clock.tick(60)
    
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
        if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
            print (treenum)
            print (wallnum)
            running = False
    
    # Move the player if an arrow key is pressed
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        player.move(-2, 0)
    if key[pygame.K_RIGHT]:
        player.move(2, 0)
    if key[pygame.K_UP]:
        player.move(0, -2)
    if key[pygame.K_DOWN]:
        player.move(0, 2)
    
    # Just added this to make it slightly fun ;)
    if player.rect.colliderect(end_rect):
        print ("You win!")
        running = False
    
    # Draw the backscene
    screen.fill((0, 0, 0))

    for GTree in collidableTree:
        GTree.render
        
    for wall in collidables:
        Wall.render
    
    pygame.draw.rect(screen, (255, 0, 0), end_rect)
    Player.render()

    #Draw shown TopScene
    screen.blit(background, (0,0))
    for wall in collidables:
        screen.blit(walles, (wall.rect))
    for GTree in collidableTree:
        screen.blit(tree, (GTree.rect))
    screen.blit(player2d, (player.rect.x,player.rect.y - (player2d.get_height()/3)))
    screen.blit(girl, (end_rect.x, (end_rect.y - (girl.get_height()/3))))
    #pygame.display.flip()
    pygame.display.update()
    
pygame.quit()
sys.exit()

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Renpygame debacle: Python34 vs Renpygame integration

#2 Post by PyTom »

Ren'Py is using Python 2.7.

Also, you want to use renpygame, rather than pygame. And even there, I'm not sure how much longer it will work - Ren'Py has been moving to using OpenGL directly. For a game like this, you may want to consider using something like a Creator-Defined Displayable:

http://www.renpy.org/doc/html/udd.html
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Aurelius
Newbie
Posts: 20
Joined: Fri Jun 06, 2014 6:56 pm
Projects: Secrets of the Ark, Degrees of Freedom
Organization: Bleau Eye Entertainment
Location: United States
Contact:

Re: Renpygame debacle: Python34 vs Renpygame integration

#3 Post by Aurelius »

Thank you for answering PyTom, I just tried this and ren'py threw me the exception:

File "game/testGame.rpy", line 193: expected statement.
add minigame→:

So would you suggest that I take each of my classes and make a Creator-Define Displayable like so:

Code: Select all


init python:
    
    bif = "bg.jpg"
    mif = "RedBall.png"
    lif = "LittlePlayer.png"
    tif = "Tree.png"
    gif = "Girl.png"
    wif = "Wall.png"

    import renpygame, random, sys, os
    from renpygame.locals import *

    ballmove = (0,0)
    treenum = 0
    wallnum = 0
    class miniGame(renpy.Displayable):
        
        class Player(renpy.Displayable):
            
            def __init__(self):
                self.rect = pygame.Rect(32, 32, 16, 16)

            def move(self, dx, dy):
                
                # Move each axis separately. Note that this checks for collisions both times.
                if dx != 0:
                    self.move_single_axis(dx, 0)
                if dy != 0:
                    self.move_single_axis(0, dy)
            
            def move_single_axis(self, dx, dy):
                
                # Move the rect
                self.rect.x += dx
                self.rect.y += dy

                # If you collide with a wall, move out based on velocity
                for wall in collidables:
                    if self.rect.colliderect(wall.rect):
                        if dx > 0: # Moving right; Hit the left side of the wall
                            self.rect.right = wall.rect.left
                        if dx < 0: # Moving left; Hit the right side of the wall
                            self.rect.left = wall.rect.right
                        if dy > 0: # Moving down; Hit the top side of the wall
                            self.rect.bottom = wall.rect.top
                        if dy < 0: # Moving up; Hit the bottom side of the wall
                            self.rect.top = wall.rect.bottom
                for gtree in collidableTree:
                    if self.rect.colliderect(gtree.rect):
                        if dx > 0: # Moving right; Hit the left side of the tree
                            self.rect.right = gtree.rect.left
                        if dx < 0: # Moving left; Hit the right side of the tree
                            self.rect.left = gtree.rect.right
                        if dy > 0: # Moving down; Hit the top side of the tree
                            self.rect.bottom = gtree.rect.top
                        if dy < 0: # Moving up; Hit the bottom side of the tree
                            self.rect.top = gtree.rect.bottom
            def render():
                pygame.draw.rect(screen, (255, 200, 0), player.rect)


        # Nice class to hold a wall rect
        class Wall(renpy.Displayable):
            
            def __init__(self, pos):
                collidables.append(self)
                self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
            def render():
                pygame.draw.rect(screen, (255, 255, 255), wall.rect)
                
        class GTree(renpy.Displayable):

            def __init__(self, pos):
                collidableTree.append(self)
                self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
            def render():
                pygame.draw.rect(screen, (0, 255, 255), GTree.rect)

        # Initialise pygame
        os.environ["SDL_VIDEO_CENTERED"] = "1"
        pygame.init()

        # Set up the display
        pygame.display.set_caption("Get to the red square!")
        screen = pygame.display.set_mode((320, 240))

        background = pygame.image.load(bif).convert()
        ball = pygame.image.load(mif).convert_alpha()
        player2d = pygame.image.load(lif).convert_alpha()
        tree = pygame.image.load(tif).convert_alpha()
        girl = pygame.image.load(gif).convert_alpha()
        walles = pygame.image.load(wif).convert_alpha()

        clock = pygame.time.Clock()
        collidables = [] # List to hold the collision objects
        collidableTree = []

        player = Player() # Create the player

        # Holds the level layout in a list of strings.
        level = [
        "WWWWWWWWWWWWWWWWWWWW",
        "W                  W",
        "W         WWWWWW   W",
        "W   WWWW       W   W",
        "W   W        WWWW  W",
        "W WWW  TTTT        W",
        "W   W     W W      W",
        "W   W     W   WWW WW",
        "W   WWW WWW   W W  W",
        "W     W   W   W W  W",
        "WWW   W   WWWWW W  W",
        "W W      WW        W",
        "W W   WWWW   WWW   W",
        "W     W    E   W   W",
        "WWWWWWWWWWWWWWWWWWWW",
        ]

        # Parse the level string above. W = wall, E = exit, T = Tree
        x = y = 0
        for row in level:
            for col in row:
                if col == "W":
                    wallnum += 1
                    Wall((x, y))
                if col == "T":
                    treenum += 1
                    GTree((x, y))
                if col == "E":
                    end_rect = pygame.Rect(x, y, 16, 16)
                x += 16
            y += 16
            x = 0

        running = True
        while running:
            
            clock.tick(60)
            
            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    running = False
                if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
                    print (treenum)
                    print (wallnum)
                    running = False
            
            # Move the player if an arrow key is pressed
            key = pygame.key.get_pressed()
            if key[pygame.K_LEFT]:
                player.move(-2, 0)
            if key[pygame.K_RIGHT]:
                player.move(2, 0)
            if key[pygame.K_UP]:
                player.move(0, -2)
            if key[pygame.K_DOWN]:
                player.move(0, 2)
            
            # Just added this to make it slightly fun ;)
            if player.rect.colliderect(end_rect):
                print ("You win!")
                running = False
            
            # Draw the backscene
            screen.fill((0, 0, 0))

            for GTree in collidableTree:
                GTree.render
                
            for wall in collidables:
                Wall.render
            
            pygame.draw.rect(screen, (255, 0, 0), end_rect)
            Player.render()

            #Draw shown TopScene
            screen.blit(background, (0,0))
            for wall in collidables:
                screen.blit(walles, (wall.rect))
            for GTree in collidableTree:
                screen.blit(tree, (GTree.rect))
            screen.blit(player2d, (player.rect.x,player.rect.y - (player2d.get_height()/3)))
            screen.blit(girl, (end_rect.x, (end_rect.y - (girl.get_height()/3))))
            #pygame.display.flip()
            pygame.display.update()
            
        pygame.quit()
        sys.exit()



label testGame:
    add miniGame:
        xalign 0.5
        yalign 0.5

    jump end

Post Reply

Who is online

Users browsing this forum: Dark12ose, Sugar_and_rice