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()