import pygame from program_class import * vec = pygame.math.Vector2 class Player: def __init__(self, app, pos): self.app = app self.grid_pos = vec(pos[0], pos[1]) self.pix_pos = self.get_pix_pos() self.direction = vec(1, 0) self.stored_direction = None self.able_to_move = True self.current_score = 0 self.speed = 1 self.lives = 1 self.moving = True def update(self): if self.able_to_move: self.pix_pos += self.direction*self.speed # Locks player to grid, stored direction stores input for "lag" if self.move_time(): if self.stored_direction is not None: self.direction = self.stored_direction # print(self.move_time()) self.able_to_move = self.can_move() # print(self.move_time()) # print(self.grid_pos, self.pix_pos) # Sets grid position according to pixel position, helps keep him on grid self.grid_pos[0] = (self.pix_pos[0]-BUFFERS//2+self.app.cell_width//2)//self.app.cell_width self.grid_pos[1] = (self.pix_pos[1]-BUFFERS//2+self.app.cell_height//2)//self.app.cell_height if self.coin_hit(): self.nom_coin() # ###################################### Helper Functions ####################################### # gets the pixel position of the player using the player's grid position and size of cell's def get_pix_pos(self): return vec((self.grid_pos[0] * self.app.cell_width) + BUFFERS // 2 + self.app.cell_width // 2, (self.grid_pos[1] * self.app.cell_height) + BUFFERS // 2 + self.app.cell_height // 2) # draw's player and lives on screen def draw(self): # Draw's player pygame.draw.circle(self.app.screen, PLAYER_TEST_COLOR, (int(self.pix_pos.x)+5, int(self.pix_pos.y)+2), self.app.cell_width//2) # Draw's player's lives for life in range(self.lives): pygame.draw.circle(self.app.screen, PLAYER_TEST_COLOR, (68 + 12*life, SCREEN_H - 13), self.app.cell_width//2) self.app.draw_text('Lives: ', self.app.screen, [25, SCREEN_H - 20], 10, WHITE, START_FONT) # Draws grid position rectangle, used primarily for debugging pygame.draw.rect(self.app.screen, RED, (self.grid_pos[0]*self.app.cell_width+BUFFERS//2, self.grid_pos[1]*self.app.cell_height+BUFFERS//2, self.app.cell_width, self.app.cell_height), 1) # Checks if to lock player to center of grid # ###################################### Movement Functions ####################################### # returns true to the player's movement in the corresponding direction def move_time(self): if int(self.pix_pos.x + BUFFERS // 2) % self.app.cell_width == 0 or self.moving == "SHOULD": if self.direction == vec(1, 0) or self.direction == vec(-1, 0) or self.direction == vec(0, 0) \ or self.moving == "SHOULD": return True if int(self.pix_pos.y+BUFFERS//2) % self.app.cell_height == 0 or self.moving == "SHOULD": if self.direction == vec(0, 1) or self.direction == vec(0, -1) or self.direction == vec(0, 0) \ or self.moving == "SHOULD": return True # determines if the player can move, if player is going into a wall it stops def can_move(self): for wall in self.app.walls: if vec(self.grid_pos + self.direction) == wall: print("no move") self.moving = "NO" return False if self.moving == "SHOULD": print("Should be") self.moving = "YES" return True self.moving = "YES" return True # function that makes the player move def move(self, direction): self.stored_direction = direction # ###################################### Collision Functions ####################################### # determines what happens if player hits a coin def coin_hit(self): if self.grid_pos in self.app.coins: if int(self.pix_pos.x + BUFFERS // 2) % self.app.cell_width == 0: if self.direction == vec(1, 0) or self.direction == vec(-1, 0): return True if int(self.pix_pos.y + BUFFERS // 2) % self.app.cell_height == 0: if self.direction == vec(0, 1) or self.direction == vec(0, -1): return True return False # adds to score when hitting coin def nom_coin(self): self.app.coins.remove(self.grid_pos) self.current_score += 1