- from CONSTANTS import *
- import pygame
- from pygame.locals import *
- #importing the basic values from a different file
- pygame.init()
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
- pygame.display.set_caption(GAME_TITLE)
- #adding all my images to use later on
- COIN_img = pygame.image.load('Coin.png')
- DOOR_img = pygame.image.load('DOOR.png')
- WALL_img = pygame.image.load('WALL.png')
- BACKGROUND_img = pygame.image.load('GAMEBACKGROUND.png')
- PLATFORM_img = pygame.image.load('PLATFORM.png')
- class Player():
- def __init__(self,x,y):
- self.images_MoveRight = []
- self.images_MoveLeft = []
- self.animation_counter = 0
- self.index = 0
- for num in range(1, 5):
- img_MoveRight = pygame.image.load(f'SPRITE{num}.png')
- img_MoveRight =pygame.transform.scale(img_MoveRight , (30, 70))
- img_MoveLeft = pygame.transform.flip(img_MoveRight, True, False)
- self.images_MoveRight.append(img_MoveRight)
- self.images_MoveLeft.append(img_MoveLeft)
- self.image = self.images_MoveRight[self.index]
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.vel_y = 0
- self.rect.y =y
- self.width = self.image.get_width()
- self.height = self.image.get_height()
- self.playerjump = False
- self.direction = 0
- def update(self):
- #all relations to things that need to be updated such as movement
- #seeing if keys are pressed to check for movement
- #using standard movement with change in x and y
- cooldown_walk = 4
- dy = 0
- dx = 0
- key = pygame.key.get_pressed()
- if key[pygame.K_UP] and self.playerjump == False:
- self.vel_y = -12
- self.playerjump = True
- if key[pygame.K_UP] == False:
- self.playerjump = False
- if key[pygame.K_LEFT]:
- dx -= 4 #means that player can not speed up past the max vertical velocity of 4
- self.animation_counter += 1
- self.direction = -1
- if key[pygame.K_RIGHT]:
- dx += 4
- self.animation_counter += 1
- self.direction = 1
- if key[pygame.K_LEFT] == False and key[pygame.K_RIGHT] == False:
- self.animation_counter = 0
- self.index = 0
- if self.direction == 1:
- self.image = self.images_MoveRight[self.index]
- if self.direction == -1:
- self.image = self.images_MoveLeft[self.index]
- self.animation_counter += 1
- if self.animation_counter > cooldown_walk:
- self.animation_counter = 0
- self.index += 1
- self.index += 1 #increases by one each iteration
- if self.index >= len(self.images_MoveRight):
- self.index = 0 #if the index reaches the last one then go back to the first index
- self.image = self.images_MoveRight[self.index] #increases index by one in the moveRight list
- for tile in world.tile_list:
- if tile[1].colliderect(self.rect.x, self.rect.y + dy,self.width, self.height ):
- if self.vel_y < 0:
- dy = tile[1].bottom = self.rect.top
- if self.vel_y >= 0:
- dy = tile[1].top - self.rect.bottom
- #gravity that basically takes in the velocity the player is falling
- #and ensures it doesnt go past a velocity of in this case 9
- self.vel_y += 2
- if self.vel_y > 9:
- self.vel_y = 9 #max velocity is 9
- dy += self.vel_y
- self.rect.x += dx
- self.rect.y += dy
- screen.blit(self.image, self.rect)
- class GAME_WORLD():
- def __init__(self, data):
- self.tile_list = []
- #need to make a system that basially splits areas of pixels into blocks
- row_count = 0
- for row in data:
- col_count = 0
- for tile in row:
- if tile == 1:
- #pygame funciton to transform the size of a given image
- img = pygame.transform.scale(WALL_img, (size_tile, size_tile))
- img_rect = img.get_rect()
- #x axis will use column
- img_rect.x = col_count * size_tile
- #y axis will use rows
- img_rect.y = row_count * size_tile
- tile = (img, img_rect)
- self.tile_list.append(tile)
- #I can copy this line of code for any kind of block that i want to
- if tile == 2:
- #pygame funciton to transform the size of a given image
- img = pygame.transform.scale(PLATFORM_img, (size_tile, size_tile))
- img_rect = img.get_rect()
- #x axis will use column
- img_rect.x = col_count * size_tile
- #y axis will use rows
- img_rect.y = row_count * size_tile
- tile = (img, img_rect)
- self.tile_list.append(tile)
- if tile == 3:
- #pygame funciton to transform the size of a given image
- img = pygame.transform.scale(DOOR_img, (size_tile, size_tile))
- img_rect = img.get_rect()
- #x axis will use column
- img_rect.x = col_count * size_tile
- #y axis will use rows
- img_rect.y = row_count * size_tile
- tile = (img, img_rect)
- self.tile_list.append(tile)
- col_count += 1
- row_count += 1
- def draw(self):
- for tile in self.tile_list:
- screen.blit(tile[0], tile[1])
- #Making the map using the previous code that u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1,1,1,1,1,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
- ]
- world = GAME_WORLD(game_map_data)
- player = Player(100,SCREENHEIGHT - 164)
- run = True
- while run:
- clock.tick(FPS)
- screen.blit(BACKGROUND_img, (0, 0))
- world.draw()
- player.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- pygame.display.update()
- pygame.quit
[text] main.py
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: