所以我有這個成熟的遊戲。我需要一個變量創建者。很難解釋。下面的代碼...如何用代碼創建一個變量(Python)
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)
所以,如果你看一下代碼... 你會看到,我需要一個變量發生器,使新的變量爲各個平臺。 (我說的是功能「platform_spawnandcheck」。)(該平臺是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
只需回答有關使變量的部分。我想爲我做一些變量,比如讓var1 var2 var3 var4等等。
我仍然需要ANSWERS。
我不明白你的問題和你的意思是通過新的變量。請修改您的代碼並只保留相關部分。 – Kikohs
列表,字典,容器。他們的全部目的就是這樣。 –
您可以通過創建類然後創建這些類的實例來完成此任務。 – jramirez