Я пытаюсь сделать это, когда я касаюсь blockClass3, меня отправляют обратно на начало моего уровня, но я новичок в программировании и не знаю, что делать. Я пробовал очень простые вещи, но у меня просто нет знания, что я получил этот код от просмотра видео, но теперь я пытаюсь добавить материал самостоятельно
видео: https://www.youtube.com/watch?v=58XQ8UDMc5Q&list=PL2RPjWnJduNkhB8l5GWBgsCFW4ZcC7R_s&index=13
вот мой код: Python 3.6 BTW
import time
import pygame
import sys
from pygame import *
from pygame.sprite import Group
winHeight = 600
winWidth = 800
halfWinHeight = winHeight/2
halfWinWidth = winWidth/2
red = (100, 255, 0)
green = (255, 150, 0)
grey = (50, 50, 50)
deadred = (255, 0, 0,)
gravity = 10
display = (winWidth, winHeight)
depth = 32
timer = pygame.time.Clock()
flags = 0
gameRunning = True
pygame.init()
screen = pygame.display.set_mode(display, flags, depth)
music = pygame.mixer.Sound('UndertaleTheme.wav')
pygame.mixer.music.load('UndertaleTheme.wav')
pygame.mixer.music.play(-1)
def main():
playerObj = playerClass(0, 100)
playerGroup = pygame.sprite.Group()
platformGroup = pygame.sprite.Group()
backGround = Surface((winWidth, winHeight))
backGround.convert()
backGround.fill(Color("#00E9FF"))
upKey = downKey = rightKey = leftKey = False
platformList = []
x = y = 0
level = [
" PPPPPPP ",
" PPP PPP ",
" PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPPPPPPPPPPP ",
" P PPPPPPPPPPPPPPPP",
" P PPPP P PPPPPPPPPPPPPPPP",
" P PPP PP PPPPPPPPPPPPPPPP",
" P PP PPPPPPPPPPPPPPPP ",
" P PP PPPPPPPPPPPPPPPP",
" P PPP P P P P PP P PPPPPP",
" P PPPPPPP PPPP PPP P PP PPPPPP",
" P PPP PPPPPPPPPPP PPPPPPPP",
" PPPPPP PPPPPP PPPPPPPP",
" PPP P PPPPPP PPPPPPPP",
" PP PPPP P PPPPPP PPPPPPPP",
" P PPPPPP PPPPPPPP",
" P PPPPPP PPPPPPPP",
"PPPPPPP PPPPPP PPPPPPPP",
"PPPPPPPP PPPPPP PPPPPPPP",
"PPPPPPPPP PPPPPP PPPPPPPP",
"PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP PPPPPPPP",
"P P P P",
"P PP PP P",
"P P",
"P P",
"P P",
"P PP P",
"P P PP P P",
"P P P P",
"P PPP PPPPP P P",
"P P",
"P PP P PPPP P",
"P PPPP PPP P",
"P PP P P",
"P PP PPP P",
"P PP PP P P",
"P P P P P P PPPP PPP P",
"P P PPPPPPPPP",
"P P PPPPPPPPPP",
"P P PPPPPPPPPPP",
"P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"P P P",
"I PP P",
"P P",
"P P",
"P P",
"I P",
"P P",
"P P",
"I P",
"P P",
"I P",
"I P",
"P III P",
"I II IIDDII P",
"IIIIII IIII P",
"IIIIIII P",
"IIIIIIII P",
"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
]
for row in level:
for col in row:
if col == "P":
P = blockClass(x, y)
platformGroup.add(P)
platformList.append(P)
if col == "I":
I = blockClass2(x, y)
platformGroup.add(I)
platformList.append(I)
if col == "D":
D = blockClass3(x, y)
platformGroup.add(D)
platformList.append(D)
x += 50
y += 50
x = 0
totalLevelWidth = len(level[0])*50
totalLevelHeight = len(level)*50
cameraObj = cameraClass(complexCamera, totalLevelWidth, totalLevelHeight)
playerGroup.add(playerObj)
while gameRunning:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_UP:
upKey = True
if event.key == K_LEFT:
leftKey = True
if event.key == K_RIGHT:
rightKey = True
if event.key == K_DOWN:
downKey = True
if event.type == KEYUP:
if event.key == K_UP:
upKey = False
if event.key == K_LEFT:
leftKey = False
if event.key == K_RIGHT:
rightKey = False
if event.key == K_DOWN:
downKey = False
screen.blit(backGround, (0, 0))
cameraObj.update(playerObj)
playerObj.update(upKey, downKey, leftKey, rightKey, platformList)
for i in playerGroup:
screen.blit(i.image, cameraObj.apply(i))
for i in platformGroup:
screen.blit(i.image, cameraObj.apply(i))
timer.tick(60)
pygame.display.update()
class cameraClass(object):
def __init__(self, cameraFunc, width, height):
self.cameraFunc = cameraFunc
self.state = Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.cameraFunc(self.state, target.rect)
def complexCamera(camera, targetRect):
x, y, w, h = targetRect
return Rect(halfWinWidth-x, halfWinHeight-y, w, h)
class entity (pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class blockClass (entity):
def __init__(self, x, y):
entity.__init__(self)
self.image = Surface((50, 50))
self.image.fill(red)
self.image.convert()
self.rect = Rect(x, y, 50, 50)
class blockClass2 (entity):
def __init__(self, x, y):
entity.__init__(self)
self.image = Surface((50, 50))
self.image.fill(grey)
self.image.convert()
self.rect = Rect(x, y, 50, 50)
class blockClass3 (entity):
def __init__(self, x, y):
entity.__init__(self)
self.image = Surface((50, 50))
self.image.fill(deadred)
self.image.convert()
self.rect = Rect(x, y, 50, 50)
def collide(self, xvel, yvel, playerClass):
for i in playerClass:
if pygame.sprite.collide_rect(self, i):
xvel = 0
yvel = 0
class playerClass (entity):
def __init__(self, x, y):
entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.image = Surface((50, 50))
self.image.fill(green)
self.image.convert()
self.rect = Rect(x, y, 50, 50)
self.onGround = False
def update(self, upKey, downKey, leftKey, rightKey, platforms):
if upKey and self.onGround:
self.yvel -= gravity
if rightKey:
if self.xvel < 6:
self.xvel += 1
if leftKey:
if self.xvel > -6:
self.xvel -= 1
if not (leftKey or rightKey):
self.xvel = 0
if not self.onGround and self.yvel < 100:
self.yvel += 0.3
self.rect.left += self.xvel
self.collide(self.xvel, 0, platforms)
self.rect.top += self.yvel
self.onGround = False
self.collide(0, self.yvel, platforms)
def collide(self, xvelDelta, yvelDelta, platforms):
for i in platforms:
if pygame.sprite.collide_rect(self, i):
if xvelDelta > 0:
self.rect.right = i.rect.left
if xvelDelta < 0:
self.rect.left = i.rect.right
if yvelDelta > 0:
self.rect.bottom = i.rect.top
self.yvel = 0
self.onGround = True
if yvelDelta < 0:
self.rect.top = i.rect.bottom
main()