-3

So I have this platformer game. I need a variable creator. It's hard to explain. Here's the code...

import pygame,time,random,sys
pygame.display.set_caption('My Platformer V1.0')
x = 10
y = 30
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
GREEN = (0,255,0)
WHITE =(0,0,0)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
LIGHTBLUE = (60,60,240)
LIGHTBROWN = (255, 236, 139)
RED = (255, 0, 0)
YELLOW = (255,255,0)
DARKRED = (102,0,0)
GRAY = (160,160,160)
WINDOWWIDTH = 1200
WINDOWHEIGHT = 700
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
PlayerRect = pygame.Rect(0,0,50,80)
global PlayerX
global PlayerY
PlayerX = 0
PlayerY = 0
moveRight = False
moveLeft = False
moveUp = False
moveDown = False
gravity = False
Jump = False
JumpTime = 0
global PPX
global PPY
PPX = PlayerX
PPY = PlayerY
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
# Define Functions ---------------------------------------- #
def platform_spawnandcheck(x,y,sx,sy):
    Rect2 = pygame.Rect(x,y,sx,sy)
    pygame.draw.rect(screen,WHITE,Rect2)
    pygame.draw.rect(screen,GREEN,PlayerRect)
    pygame.display.update()
    y2 = y
    y2 += 80
    global PlayerY
    if PlayerY < y2:
        if PlayerRect.colliderect(Rect2):
            global PPX
            global PlayerY
            PlayerY = PPY
    if PlayerY > y2:
        if PlayerRect.colliderect(Rect2):
            global PPX 
            PlayerX = PPX
while True:
    # Make Past Coordinance ------------------------------- #
    PPX = PlayerX
    PPY = PlayerY
    # Jump ------------------------------------------------ #
    if Jump == True:
        JumpTime += 1
        PlayerY -= 12
        if JumpTime == 30:
            Jump = False
            JumpTime = 0
    # Gravity --------------------------------------------- #
    gravity = True
    # Movement -------------------------------------------- #
    if moveRight == True:
        PlayerX += 5
    if moveLeft == True:
        PlayerX -= 5
    if PlayerY > 620:
        gravity = False
    if moveUp == True:
        PlayerY -= 3
        if gravity == False:
            Jump = True
    if gravity == True:
        PlayerY += 5
    # Reload the player Rect ------------------------------ #
    PlayerRect = pygame.Rect(PlayerX,PlayerY,50,80)
    # Check for pressed keys ------------------------------ #
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == ord('d'):
                moveRight = True
            if event.key == K_SPACE:
                moveUp = True
            if event.key == ord('a'):
                moveLeft = True
        if event.type == KEYUP:
            if event.key == ord('d'):
                moveRight = False
            if event.key == K_SPACE:
                moveUp = False
            if event.key == ord('a'):
                moveLeft = False
    # Update ---------------------------------------------- #
    screen.fill(LIGHTBLUE)
    platform_spawnandcheck(500,650,100,20)
    pygame.draw.rect(screen,GREEN,PlayerRect)
    pygame.display.update()
    mainClock.tick(60)

So if you look at the code... You will see that I need a variable generator to make new variables for individual platforms. (I'm talking about the function "platform_spawnandcheck".) (The platforms are for pygame.)

def platform_spawnandcheck(x,y,sx,sy):
    Rect2 = pygame.Rect(x,y,sx,sy)
    pygame.draw.rect(screen,WHITE,Rect2)
    pygame.draw.rect(screen,GREEN,PlayerRect)
    pygame.display.update()
    y2 = y
    y2 += 80
    global PlayerY
    if PlayerY < y2:
        if PlayerRect.colliderect(Rect2):
            global PPX
            global PlayerY
            PlayerY = PPY
    if PlayerY > y2:
        if PlayerRect.colliderect(Rect2):
            global PPX 
            PlayerX = PPX

Just answer the part about making variables. I want something to make variables for me like have it make var1 var2 var3 var4 and so on.

I still need ANSWERS.

AkshayDandekar
  • 421
  • 2
  • 11
Larry McMuffin
  • 229
  • 3
  • 12

3 Answers3

4

A - if it is hard to explain, you are probably missing some clarity that is necessary for coding

B- familiarize yourself with "dict"

move = {"up":False, "down":True}

is what you want.

you can then add values

move["jump"]=True
vish
  • 1,046
  • 9
  • 26
1

So you need to create variables using logic rather than making a direct assignment. That is what I get from your question. So rather than this,

a = 10

you want to do

if some logic:
    make_var(10)  # somehow returns (a,10) in bizaro python

Although, this is one way to make a variable assignment, that is not the right way. What vish is trying to tell you is that you can make variables by creating a python dict and making an assignment inside the dict as

made_vars = {}
if some logic:
    made_vars[a] = 10

print made_vars[a]  # should print 10

So you get your assignment done. However instead of names var1, var2 etc, you would have the variable names as made_var[var1], made_var[var2] etc.

I was searching for a better explanation online, and I came up with this link.

Community
  • 1
  • 1
AkshayDandekar
  • 421
  • 2
  • 11
0

You should create a class for the platforms that gets initialized with some random position.

The __init__ method of a class is the "generator" for that class, also known as a constructor. Construct instances of the class with Platform(), and the __init__ method is called and returns a new Platform object.

class Platform:
    def __init__(self):
        self.x = random.randint(0, WINDOWWIDTH)
        self.y = random.randint(0, WINDOWHEIGHT)
        self.sx = 100
        self.sy = 20

# creating platforms
platforms = []
for i in xrange(20):
    platforms.append(Platform())

# accessing the coordinates of the platforms
for platform in platforms:
    print platform.x, platform.y, platform.sx, platform.sy

# passing the platforms into your function
for platform in platforms:
    platform_spawnandcheck(platform.x, platform.y, platform.sx, platform.sy)
OregonTrail
  • 8,594
  • 7
  • 43
  • 58
  • The stuff you gave me is already what I did with the platform_spawnandcheck(). – Larry McMuffin Feb 22 '14 at 12:44
  • The platform spawns and then if I spawn another one they switch between showing them in pygame before it has to reuse a variable. So in other words the platforms flash... D: It would be the same issue with both of ours. – Larry McMuffin Feb 22 '14 at 12:46