152 lines
4.4 KiB
Python
152 lines
4.4 KiB
Python
import pygame
|
|
import sys
|
|
import random
|
|
import objects
|
|
import math
|
|
|
|
class Player():
|
|
def __init__(self, x, y):
|
|
image_path = "assets/Sprites/Characters/Default/character_green_walk_a.png"
|
|
image_path2 = "assets/Sprites/Characters/Default/character_green_walk_b.png"
|
|
self.image1 = pygame.image.load(image_path).convert_alpha()
|
|
self.image2 = pygame.image.load(image_path2).convert_alpha()
|
|
self.rect = self.image1.get_rect()
|
|
self.pos = pygame.Vector2(x, y)
|
|
self.velocity = pygame.Vector2(0, 0)
|
|
self.acceleration = pygame.Vector2(0, 0)
|
|
self.on_ground = False
|
|
self.speed = 20
|
|
self.jump_strength = -300
|
|
|
|
self.current_image = 0
|
|
self.image = self.image1
|
|
|
|
self.frame_counter = 0
|
|
self.animation_speed = 30
|
|
|
|
def animate(self):
|
|
game_fps = 15
|
|
self.frame_counter += 1
|
|
if self.frame_counter>=game_fps:
|
|
if self.current_image == 0:
|
|
self.current_image = 1
|
|
else:
|
|
self.current_image = 0
|
|
self.frame_counter = 0
|
|
|
|
if self.current_image == 0:
|
|
self.image = self.image1
|
|
else:
|
|
self.image = self.image2
|
|
|
|
def update(self):
|
|
#self.velocity.x *= 0.5
|
|
print("updating---", self.pos, self.velocity, self.acceleration)
|
|
|
|
def show(self, surface):
|
|
surface.blit(self.image, self.rect)
|
|
|
|
def apply_force(self, force_x, force_y):
|
|
self.acceleration.x += force_x
|
|
self.acceleration.y += force_y
|
|
|
|
def move(self):
|
|
|
|
self.velocity += self.acceleration
|
|
|
|
if self.on_ground:
|
|
self.velocity.y = 0
|
|
self.pos.y = 600
|
|
|
|
print("moving---", self.acceleration, self.velocity)
|
|
self.pos += self.velocity
|
|
|
|
# Update the rect position
|
|
self.rect.bottomleft = self.pos
|
|
self.velocity *=0.85
|
|
|
|
class Game():
|
|
def __init__(self):
|
|
self.height = 760
|
|
self.width = 1280
|
|
self.window = pygame.display.set_mode((self.width, self.height))
|
|
self.window_rect = self.window.get_rect()
|
|
self.clock = pygame.time.Clock()
|
|
self.window.set_colorkey((35, 35, 35))
|
|
self.debug_grid = True
|
|
self.background = (35, 35, 35)
|
|
|
|
self.player = Player(100, 100)
|
|
|
|
self.dt = 0
|
|
|
|
def main(self):
|
|
self.event_handler()
|
|
self.blit()
|
|
|
|
def event_handler(self):
|
|
"""
|
|
This method handles mouse and keyboard events for the scene and passes
|
|
them on as is needed.
|
|
"""
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
pygame.quit()
|
|
sys.exit(0)
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
direction_x = 0
|
|
direction_y = 0
|
|
if keys[pygame.K_d]:
|
|
direction_x += 1
|
|
self.player.apply_force(direction_x, direction_y)
|
|
if keys[pygame.K_a]:
|
|
direction_x -= 1
|
|
self.player.apply_force(direction_x, direction_y)
|
|
if keys[pygame.K_SPACE]:
|
|
direction_y += self.player.jump_strength/60
|
|
self.player.on_ground = False
|
|
self.player.apply_force(direction_x, direction_y)
|
|
|
|
if self.player.pos.y <= 600:
|
|
print("falling")
|
|
direction_y += 1
|
|
self.player.apply_force(direction_x, direction_y)
|
|
else:
|
|
self.player.on_ground = True
|
|
|
|
self.player.move()
|
|
|
|
|
|
def update(self, events):
|
|
pass
|
|
|
|
|
|
def blit(self):
|
|
self.window.fill(self.background)
|
|
if self.debug_grid:
|
|
for y in range(1, int(self.window_rect.height/32) + 1):
|
|
pygame.draw.line(self.window, (150, 150, 150), (0, y * 32), (self.window_rect.width, y * 32))
|
|
|
|
for x in range(1, int(self.window_rect.width/32) + 1):
|
|
pygame.draw.line(self.window, (150, 150, 150), (x * 32, 0), (x * 32, self.window_rect.height))
|
|
|
|
pygame.draw.line(self.window, (255, 0, 0), (0, 600), (1280, 600))
|
|
self.player.animate()
|
|
self.player.show(self.window)
|
|
self.player.update()
|
|
pygame.display.flip()
|
|
self.clock.tick(60)
|
|
self.dt = self.clock.tick(60) / 1000
|
|
|
|
|
|
pygame.display.set_caption('Physics galore!')
|
|
|
|
pygame.init()
|
|
pygame.font.init()
|
|
scene = Game()
|
|
|
|
while scene is not None:
|
|
scene.main() |