Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame,random
- window = pygame.display.set_mode((800,800), pygame.NOFRAME)
- # player factory
- class player:
- def __init__(self,x,y,height,width,color):
- self.x = x
- self.y = y
- self.height = height
- self.width = width
- self.color = color
- self.speed = 4
- self.fall = 0
- self.isJump = False
- # collisions for platforms
- self.moveleft = True
- self.moveright = True
- # animation frame for right:
- self.right = [pygame.image.load("player1.png"),
- pygame.image.load("player2.png"),
- pygame.image.load("player3.png"),
- pygame.image.load("player4.png"),
- pygame.image.load("player5.png")]
- # animation frame for left:
- self.left = [pygame.image.load("player10.png"),
- pygame.image.load("player11.png"),
- pygame.image.load("player12.png"),
- pygame.image.load("player13.png"),
- pygame.image.load("player14.png")]
- self.idleright = [pygame.image.load("idle1.png"),
- pygame.image.load("idle1.png"),
- pygame.image.load("idle1.png"),
- pygame.image.load("idle1.png")]
- self.idleleft = [pygame.image.load("id1.png"),
- pygame.image.load("id1.png"),
- pygame.image.load("id1.png"),
- pygame.image.load("id1.png")]
- self.anim_index = 0
- self.direction = "right"
- self.direction = "left"
- self.direction = "standright"
- self.direction = "standleft"
- self.direction = "jumpright"
- self.direction = "jumpleft"
- self.health = 10
- self.hitbox = (self.x + 20, self.y, 26,60)
- self.clock = pygame.time.Clock()
- self.fps = 24
- self.JumpCount = 10
- self.rect = pygame.Rect(x,y,height,width)
- def draw(self):
- self.rect.topleft = (self.x,self.y)
- if self.direction == "right":
- self.clock.tick(self.fps)
- image_list = self.right
- elif self.direction == "left":
- self.clock.tick(self.fps)
- image_list = self.left
- elif self.direction == "standright":
- self.clock.tick(self.fps)
- image_list = self.idleright
- elif self.direction == "standleft":
- self.clock.tick(self.fps)
- image_list = self.idleleft
- else:
- self.clock.tick(self.fps)
- image_list = self.idleright
- if self.anim_index >= len(image_list):
- self.anim_index = 0
- player_image = image_list[self.anim_index]
- self.anim_index += 1
- self.hitbox = (self.x + -18, self.y, 46,60)
- pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 80, 10)) # NEW
- pygame.draw.rect(window, (0,200,0), (self.hitbox[0], self.hitbox[1] - 40, 80 - (5 * (10 - self.health)), 10))
- player_rect = player_image.get_rect(center = self.rect.center)
- player_rect.centerx += -2 # 10 is just an example
- player_rect.centery += -25# 15 is just an example
- window.blit(player_image, player_rect)
- class box:
- def __init__(self,x,y,height,width,color):
- self.x = x
- self.y = y
- self.height = height
- self.width = width
- self.color = color
- self.imagebox = pygame.image.load("box1.png")
- self.imagebox = pygame.transform.scale(self.imagebox, (self.imagebox.get_width()//3,self.imagebox.get_height()//3))
- self.rect = pygame.Rect(x,y,height,width)
- self.hitbox = (self.x + 10, self.y + 10 , 46,60)
- self.health = 10
- def draw(self):
- self.rect.topleft = (self.x,self.y)
- self.hitbox = (self.x + -5, self.y + 10 , 46,60)
- window.blit(self.imagebox,self.rect)
- white = (34,32,52)
- box1 = box(280,520,150,50,white)
- pink = (34,32,52)
- playerman = player(300,300,40,40,pink)
- # player factory
- class platform22:
- def __init__(self,x,y,height,width,color):
- self.x = x
- self.y = y
- self.height = height
- self.width = width
- self.color = color
- self.image = pygame.image.load("platform1.png")
- self.image = pygame.transform.scale(self.image, (self.image.get_width()+60, self.image.get_height()+60))
- self.rect = pygame.Rect(x,y,height,width)
- def draw(self):
- self.rect.topleft = (self.x,self.y)
- pygame.draw.rect(window,self.color,self.rect)
- platform2 = platform22(280,580,180,50,white)
- # player factory
- class platform:
- def __init__(self,x,y,height,width,color):
- self.x = x
- self.y = y
- self.height = height
- self.width = width
- self.color = color
- self.rect = pygame.Rect(x,y,height,width)
- def draw(self):
- self.rect.topleft = (self.x,self.y)
- pygame.draw.rect(window,self.color,self.rect)
- white = (34,32,52)
- platform1 = platform(0,766,399990,3000,white)
- platforms = [platform1,platform2]
- # the game fps
- clock = pygame.time.Clock()
- FPS = 30
- # redraw the window
- def redraw():
- playerman.draw()
- for platform in platforms:
- platform.draw()
- box1.draw()
- platform2.draw()
- if playerman.rect.colliderect(box1.rect):
- if keys[pygame.K_p]:
- pygame.draw.rect(window, (69,40,60), (box1.hitbox[0], box1.hitbox[1] - 40, 80, 20)) # NEW
- pygame.draw.rect(window, (238,195,154), (box1.hitbox[0], box1.hitbox[1] - 40, 5 - (5 * (10 - box1.health)), 20))
- if box1.health < 25:
- box1.health += 0.4
- # main loop
- runninggame = True
- while runninggame:
- clock.tick(FPS)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- runningggame = False
- # key event
- keys = pygame.key.get_pressed()
- if playerman.direction == "right":
- playerman.direction = "standright"
- if playerman.direction == "left":
- playerman.direction = "standleft"
- if keys[pygame.K_d]:
- if playerman.moveright:
- playerman.direction = "right"
- playerman.x += playerman.speed
- if keys[pygame.K_a]:
- if playerman.moveleft:
- playerman.direction = "left"
- playerman.x -= playerman.speed
- # move the player
- collide = False
- if not playerman.isJump:
- if keys[pygame.K_SPACE]:
- if playerman.direction == "left" and playerman.direction == "standleft":
- playerman.direction = "jumpleft"
- playerman.clock.tick(30)
- image_list = playerman.jumpleft
- if playerman.direction == "right" and playerman.direction == "standright":
- playerman.direction = "jumpright"
- playerman.isJump = True
- playerman.y += playerman.fall
- playerman.fall += 1
- playerman.isJump = False
- collide = False
- # collisions
- for platform in platforms:
- if playerman.rect.colliderect(platform.rect):
- collide = True
- playerman.isJump = False
- if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom) or
- platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom)):
- playerman.y = platform.rect.top - playerman.height + 1
- playerman.moveright = True
- playerman.moveleft = True
- if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.top) or
- platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom - 10)):
- playerman.moveright = False
- elif (platform.rect.collidepoint(playerman.rect.left, playerman.rect.top) or
- platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom - 10)):
- playerman.moveleft = False
- else:
- playerman.moveright = True
- playerman.moveleft = True
- # this makes the player fall down up to
- if playerman.rect.bottom >= 890:
- collide = True
- playerman.isJump = False
- playerman.JumpCount = 10
- playerman.y = 890 - playerman.height
- if collide:
- if keys[pygame.K_SPACE]:
- playerman.isJump = True
- playerman.fall = 0
- else:
- if playerman.JumpCount >0:
- playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount)) * 0.3
- playerman.JumpCount -= 1
- else:
- playerman.JumpCount = 10
- playerman.isJump = False
- window.fill((0,0,0))
- redraw()
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement