Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame,random,math
- window = pygame.display.set_mode((800,800), pygame.NOFRAME)
- def blitRotate(surf, image, pos, originPos, angle):
- # calcaulate the axis aligned bounding box of the rotated image
- w, h = image.get_size()
- sin_a, cos_a = math.sin(math.radians(angle)), math.cos(math.radians(angle))
- min_x, min_y = min([0, sin_a*h, cos_a*w, sin_a*h + cos_a*w]), max([0, sin_a*w, -cos_a*h, sin_a*w - cos_a*h])
- # calculate the translation of the pivot
- pivot = pygame.math.Vector2(originPos[0], -originPos[1])
- pivot_rotate = pivot.rotate(angle)
- pivot_move = pivot_rotate - pivot
- # calculate the upper left origin of the rotated image
- origin = (pos[0] - originPos[0] + min_x - pivot_move[0], pos[1] - originPos[1] - min_y + pivot_move[1])
- # get a rotated image
- rotated_image = pygame.transform.rotate(image, angle)
- # rotate and blit the image
- surf.blit(rotated_image, origin)
- class handgun():
- 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)
- # LOL THESE IS THE HAND
- self.shootsright = pygame.image.load("hands.png")
- self.image = self.shootsright
- self.rect = self.image.get_rect(center = (self.x, self.y))
- self.look_at_pos = (self.x, self.y)
- self.isLookingAtPlayer = False
- self.look_at_pos = (x,y)
- self.hitbox = (self.x + -18, self.y, 46,60)
- def draw(self,drawX,drawY):
- self.rect.topleft = (drawX,drawY)
- # the guns hitbox
- # rotatiing the gun
- dx = self.look_at_pos[0] - self.rect.centerx
- dy = self.look_at_pos[1] - self.rect.centery
- angle = (180/math.pi) * math.atan2(-dy, dx)
- gun_size = self.image.get_size()
- pivot = (8, gun_size[1]//2)
- blitRotate(window, self.image, self.rect.center, pivot, angle)
- def lookAt( self, coordinate ):
- self.look_at_pos = coordinate
- white = (255,255,255)
- handgun1 = handgun(300,300,10,10,white)
- # 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
- self.rect = pygame.Rect(x,y,height,width)
- # 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)
- self.gun = (self.x + -18, self.y, 46,60)
- 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)), 13))
- 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 portal:
- def __init__(self,x,y,height,width,color):
- self.x = x
- self.y = y
- self.height = height
- self.width = width
- self.color = color
- self.rightblob = [
- pygame.image.load("port1.png"),
- pygame.image.load("port2.png"),
- pygame.image.load("port3.png"),
- pygame.image.load("port4.png"),
- pygame.image.load("port5.png"),
- pygame.image.load("port6.png"),
- pygame.image.load("port7.png"),
- pygame.image.load("port8.png")]
- self.rightblob = [pygame.transform.scale(image,(image.get_width()*3,image.get_height()*3)) for image in self.rightblob]
- self.rect = pygame.Rect(x,y,height,width)
- self.direction = "blobright"
- self.anim_index = 0
- def draw(self):
- self.rect.topleft = (self.x,self.y)
- pygame.draw.rect(window,self.color,self.rect)
- if self.direction == "blobright":
- window.blit(self.rightblob[self.anim_index], self.rect)
- self.anim_index += 1
- if self.anim_index == len(self.rightblob):
- self.anim_index = 0
- white = (255,255,255)
- port1 = portal(420,388,50,50,white)
- 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 = (23, 32, 42)
- box1 = box(320,385,130,50,white)
- boxes = [box1]
- 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,680,120,20,white)
- bg1color = (59, 66, 66)
- bg2color = (77, 86, 86)
- bg1 = platform22(0,540,820,750,bg1color)
- bg2 = platform22(100,100,120,750,bg2color)
- bg3 = platform22(300,100,120,750,bg2color)
- bg4 = platform22(400,100,120,750,bg2color)
- # 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 = (77, 86, 86)
- platform1 = platform(0,766,399990,3000,white)
- platforms = [platform1,platform2]
- platformGroup = pygame.sprite.Group
- level = [
- " c ",
- " ",
- " c c ",
- " c c ",
- " ",
- " c ",
- " ",
- " c ",
- " c ",
- " c ",
- " ",
- " ",
- " c ",
- " c c "]
- for iy, row in enumerate(level):
- for ix, col in enumerate(row):
- if col == "c":
- new_platforms = platform(ix*9.8, iy*50, 120,20,(23, 32, 42))
- platforms.append(new_platforms)
- # the game fps
- clock = pygame.time.Clock()
- FPS = 30
- # redraw the window
- def redraw():
- bg3.draw()
- bg4.draw()
- bg2.draw()
- bg1.draw()
- playerman.draw()
- for platform in platforms:
- platform.draw()
- platform2.draw()
- port1.draw()
- for box in boxes:
- box.draw()
- if box1.health > 25:
- handgun1.draw(playerman.x - 19,playerman.y - 29)
- 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
- else:
- box1.x = -900
- # 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()
- # gun rotation
- if playerman.rect.colliderect(handgun1.rect):
- mousex, mousey = pygame.mouse.get_pos()
- if not handgun1.isLookingAtPlayer:
- handgun1.lookAt((mousex, mousey))
- 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
- playerman.y = platform.rect.top - playerman.height + 1
- if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
- playerman.x = platform.rect.left - playerman.width
- if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
- playerman.x = platform.rect.right
- # 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((81, 90, 90))
- redraw()
- pygame.display.update()
- pygame.quit()
Add Comment
Please, Sign In to add comment