Когда я запускаю эту программу и перехожу к следующему этапу, она выдает следующее: я не уверен, почему это происходит, потому что я сделал Up
поверхность здесь: Up = pygame.image.load('up.png').convert()
Я хочу, чтобы она появилась в координатах x и y, которые вы здесь указали: ups.append(Up(500, 700, 70, 90))
Traceback (most recent call last):
File "D:\Coding\Python\Escape\main.py", line 291, in <module>
redrawGameWindow()
File "D:\Coding\Python\Escape\main.py", line 124, in redrawGameWindow
up.draw(win)
File "D:\Coding\Python\Escape\main.py", line 77, in draw
win.blit(Up, (self.x, self.y))
TypeError: argument 1 must be pygame.Surface, not type
Вот мой код:
import pygame
import time
pygame.init()
win = pygame.display.set_mode((1200, 900))
pygame.display.set_caption("Escape")
walkRight = [
pygame.image.load('right.png'),
pygame.image.load('walkingright.png')
]
walkLeft = [
pygame.image.load('left.png'),
pygame.image.load('walkingleft.png')
]
idle = pygame.image.load('idle.png')
bg = pygame.image.load('background.png')
Up = pygame.image.load('up.png').convert()
Speed = pygame.image.load('speed.png').convert()
clock = pygame.time.Clock()
mode = 0
# set mode to 0 for debugging, set mode to 1 for playing
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.jumpCount = 10
self.left = False
self.right = False
self.walkCount = 0
self.standing = True
self.hp = 100
self.stage = 0
self.gravity = True
self.isTouchingPlatform = False
self.jump = 0.25
self.upAvaliable = 0
self.speedAvaliable = 0
self.hitbox = (self.x + 8, self.y, self.width - 20, self.height)
def draw(self, win):
if self.walkCount + 1 >= 10:
self.walkCount = 0
if not (self.standing):
if self.left:
win.blit(walkLeft[self.walkCount // 5], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount // 5], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (self.x, self.y))
self.hitbox = (self.x + 8, self.y, self.width - 20, self.height)
if mode == 0:
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 1)
class Up(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = (self.x, self.y, width, height)
def draw(self, win):
win.blit(Up, (self.x, self.y))
class Speed(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = (self.x, self.y, width, height)
def Speed(self, win):
win.blit(Speed, (self.x, self.y))
class Platform(object):
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = (self.x, self.y, self.width, self.height)
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
class Obstacle(object):
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = (self.x, self.y, self.width, self.height)
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def redrawGameWindow():
win.blit(bg, (0, 0))
if (round(end - now) != 0):
text = font.render(str(round(end - now)), 1, (0, 0, 0))
win.blit(text, (10, 10))
for platform in platforms:
platform.draw(win)
for obstacle in obstacles:
obstacle.draw(win)
for up in ups:
up.draw(win)
for speed in speeds:
speed.draw(win)
david.draw(win)
pygame.display.update()
run = True
isPlatform = False
david = player(50, 734, 72, 116)
platforms = []
obstacles = []
ups = []
speeds = []
neg = -50
font = pygame.font.SysFont('comicsansms', 25, True)
now = time.time()
if mode == 0:
end = now + -1
else:
end = now + 30
canChange = True
index = 0
while run:
now = time.time()
david.speedAvaliable -= 1
david.upAvaliable -= 1
if david.stage == 0 and not (isPlatform):
platforms = []
obstacles = []
ups = []
speeds = []
index = 0
isPlatform = True
elif david.stage == 1 and not (isPlatform):
platforms = []
obstacles = []
ups = []
speeds = []
ups.append(Up(500, 700, 70, 90))
index = 0
isPlatform = True
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
for platform in platforms:
if david.hitbox[1] + david.hitbox[3] >= platform.y and david.hitbox[1] + david.hitbox[3] < platform.y + platform.height:
if david.hitbox[0] + david.hitbox[2] >= platform.x and david.hitbox[0] < platform.x + platform.width:
if canChange:
index = platforms.index(platform)
canChange = False
david.isTouchingPlatform = True
neg = 0
if david.hitbox[1] != platform.y:
david.y = platform.y - david.height
elif not(david.hitbox[0] + david.hitbox[2] >= platforms[index].x and david.hitbox[0] < platforms[index].x + platforms[index].width):
david.isTouchingPlatform = False
canChange = True
neg = -50
elif not(david.hitbox[1] + david.hitbox[3] >= platforms[index].y and david.hitbox[1] + david.hitbox[3] < platforms[index].y + platforms[index].height):
david.isTouchingPlatform = False
canChange = True
neg = -50
for obstacle in obstacles:
if david.hitbox[1] <= obstacle.y + obstacle.height and david.hitbox[1] + david.hitbox[3] > obstacle.y:
if david.hitbox[0] + david.hitbox[2] >= obstacle.x and david.hitbox[0] < obstacle.x + obstacle.width:
if david.left:
david.x += 5
if david.right:
david.x -= 5
for speed in speeds:
if david.hitbox[1] <= speed.y + speed.height and david.hitbox[1] + david.hitbox[3] > speed.y:
if david.hitbox[0] + david.hitbox[2] >= speed.x and david.hitbox[0] < speed.x + speed.width:
david.speedAvaliable = 30
for up in ups:
print(up)
if david.hitbox[1] <= up.y + up.height and david.hitbox[1] + david.hitbox[3] > up.y:
if david.hitbox[0] + david.hitbox[2] >= up.x and david.hitbox[0] < up.x + up.width:
david.upAvaliable = 30
if keys[pygame.K_LEFT] and david.x > david.vel:
david.x -= david.vel
david.left = True
david.right = False
david.standing = False
elif keys[pygame.K_RIGHT] and david.x < 1195 - david.width:
david.x += david.vel
david.left = False
david.right = True
david.standing = False
else:
david.standing = True
walkCount = 0
if not (david.isJump):
if keys[pygame.K_UP] and david.y >= 734:
david.jumpCount = 10
david.isJump = True
david.walkCount = 0
else:
for platform in platforms:
if keys[pygame.K_UP] and david.y + david.height >= platform.y and david.isTouchingPlatform:
david.jumpCount = 10
david.isJump = True
david.walkCount = 0
else:
if david.jumpCount >= 0:
david.gravity = False
david.y -= (david.jumpCount ** 2) * david.jump
david.jumpCount -= 1
else:
david.gravity = True
david.isJump = False
if david.gravity and david.y <= 734 and not (david.isTouchingPlatform):
david.y -= (david.jumpCount ** 2) * -0.25
if david.y > 734:
david.y = 734
if david.x >= 1125:
david.stage += 1
isPlatform = False
david.x = 50
if david.x <= 5 and david.stage > 0:
david.stage -= 1
isPlatform = False
david.x = 1125
if not (david.isJump):
if david.jumpCount >= neg:
david.jumpCount -= 1
else:
david.jumpCount = 0
if david.speedAvaliable > 0:
david.vel = 10
else:
if mode == 0:
david.vel = 10
else:
david.vel = 5
if david.upAvaliable > 0:
david.jump = 0.5
else:
if mode == 0:
david.jump = 0.5
else:
david.vel = 0.25
if (round(end - now) == 0):
run = False
else:
redrawGameWindow()
pygame.quit()
exit()