Add horse manual vertical movement functionality using up() and down() functions. Add barrier movement.
This commit is contained in:
Binary file not shown.
+31
-4
@@ -9,10 +9,10 @@ class GameObject:
|
|||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
surface.blit(self.image, self.rect)
|
surface.blit(self.image, self.rect)
|
||||||
def set_position(self, x, y): # TODO: FIX CENTERING
|
def set_position(self, x, y): # TODO: FIX CENTERING
|
||||||
self.rect.x = x - self.image.get_width() / 2
|
self.rect.x = x
|
||||||
self.rect.y = y - self.image.get_height() / 2
|
self.rect.y = y
|
||||||
self.x = x - self.image.get_width() / 2
|
self.x = x
|
||||||
self.y = y - self.image.get_width() / 2
|
self.y = y
|
||||||
def move(self, dx, dy):
|
def move(self, dx, dy):
|
||||||
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
||||||
def set_size(self, width, height):
|
def set_size(self, width, height):
|
||||||
@@ -21,12 +21,39 @@ class GameObject:
|
|||||||
def scale_by(self, scale):
|
def scale_by(self, scale):
|
||||||
self.image = pygame.transform.scale(self.image, (self.image.get_width() * scale, self.image.get_height() * scale))
|
self.image = pygame.transform.scale(self.image, (self.image.get_width() * scale, self.image.get_height() * scale))
|
||||||
self.rect = self.image.get_rect(topleft=(self.x,self.y))
|
self.rect = self.image.get_rect(topleft=(self.x,self.y))
|
||||||
|
def get_position(self):
|
||||||
|
return (self.x, self.y)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Horse(GameObject):
|
class Horse(GameObject):
|
||||||
|
default_vacceleration = 1
|
||||||
|
vspeed = 0
|
||||||
|
vacceleration = 0
|
||||||
def __init__(self, image_path, x, y):
|
def __init__(self, image_path, x, y):
|
||||||
|
self.vspeed = 0
|
||||||
|
self.vacceleration = 0
|
||||||
super().__init__(image_path, x, y)
|
super().__init__(image_path, x, y)
|
||||||
|
def apply_vspeed(self):
|
||||||
|
pos = self.get_position()
|
||||||
|
self.set_position(pos[0], pos[1] + self.vspeed)
|
||||||
|
def apply_vacceleration(self):
|
||||||
|
self.vspeed += self.vacceleration
|
||||||
|
def set_vspeed(self, speed):
|
||||||
|
self.vspeed = speed
|
||||||
|
def add_vspeed(self, delta):
|
||||||
|
self.vspeed += delta
|
||||||
|
def set_vacceleration(self, vacceleration):
|
||||||
|
self.vacceleration = vacceleration
|
||||||
|
def up(self):
|
||||||
|
self.set_vacceleration(-self.default_vacceleration)
|
||||||
|
def down(self):
|
||||||
|
self.set_vacceleration(self.default_vacceleration)
|
||||||
|
def stay(self):
|
||||||
|
self.set_vacceleration(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Background(GameObject):
|
class Background(GameObject):
|
||||||
def __init__(self, image_path, x, y):
|
def __init__(self, image_path, x, y):
|
||||||
|
|||||||
@@ -9,26 +9,44 @@ screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|||||||
pygame.display.set_caption("NIC_Project")
|
pygame.display.set_caption("NIC_Project")
|
||||||
|
|
||||||
WHITE = (255, 255, 255)
|
WHITE = (255, 255, 255)
|
||||||
RED = (255, 0, 0)
|
|
||||||
|
|
||||||
|
grass = Background("images/grass.jpg", 0, 0)
|
||||||
grass = Background("images/grass.jpg", WIDTH/2, HEIGHT/2)
|
horse1 = Horse("images/horse.png", 50, 50)
|
||||||
horse1 = Horse("images/horse.png",50, 50)
|
|
||||||
barrier1 = Barrier("images/barrier.png", 400, 300)
|
barrier1 = Barrier("images/barrier.png", 400, 300)
|
||||||
|
|
||||||
horse1.set_size(75, 75)
|
BARRIER_SPEED = 1
|
||||||
|
|
||||||
|
horse1.set_size(75, 75)
|
||||||
grass.set_size(WIDTH, HEIGHT)
|
grass.set_size(WIDTH, HEIGHT)
|
||||||
|
|
||||||
gameobjects = [grass, horse1, barrier1]
|
gameobjects = [grass, horse1, barrier1]
|
||||||
|
horses = [horse1]
|
||||||
|
barriers = [barrier1]
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
|
for horse in horses:
|
||||||
|
if keys[pygame.K_UP]:
|
||||||
|
horse.up()
|
||||||
|
elif keys[pygame.K_DOWN]:
|
||||||
|
horse.down()
|
||||||
|
else:
|
||||||
|
horse.stay()
|
||||||
|
|
||||||
|
horse.apply_vacceleration()
|
||||||
|
horse.apply_vspeed()
|
||||||
|
horse.draw(screen)
|
||||||
|
|
||||||
|
for barrier in barriers:
|
||||||
|
barrier.move(-BARRIER_SPEED, 0)
|
||||||
|
|
||||||
for object in gameobjects:
|
for object in gameobjects:
|
||||||
object.draw(screen)
|
object.draw(screen)
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
pygame.time.Clock().tick(60)
|
pygame.time.Clock().tick(60)
|
||||||
|
|
||||||
Reference in New Issue
Block a user