checking when the player is touching the top of a platform
up vote
0
down vote
favorite
I have looked at collisions in pygame and I do understand how they work but I want to make mine so that if you jump onto the top of the platform (the crab) then it will stay there until you decide to walk off. so far what it currently decides to do is whenever i touch the platform the sprite cannot jump (I also want to make the character to be able to jump on the platform:
import pygame
import random
import sys
import time
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
# Define this in the global scope
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
self.on_ground = True
self.rect = self.picture.get_rect()
def update(self):
# Call this method each frame to update the positions.
#self.xpos += self.speed_x
#self.ypos += self.speed_y
GRAVITY = .9
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Stop falling at the bottom of the screen.
if self.ypos >= 520: #if the position is more than 520 then it will make the ypos 520 and there will be be no y_speed
self.ypos = 520
self.speed_y = 0
self.on_ground = True #this means that you are on the ground
#def change (self, costume):
#if costume == 0:
#picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
#elif costume == 1:
#picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
# Not necessary anymore.
# def move_left(self):
# self.xpos -= 5 # -= not = -5 (augmented assignment).
#
# def move_right(self):
# self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self):
if self.on_ground: # This prevents air jumps. AND WHEN THE FUNCTION IS CALLED IT CHECKS IF YOU ARE ON THE GROUND IF YOU ARE YOU ARE ALLOWED TO JUMP BUT IF YOU ARE IN THE AIR IT WILL NOT WORK
self.on_ground = False #whenever you call this function it checks wether you are on teh gorund or not it can happen if on_ground is true or false
#then on_ground value becomes flase meaning that you are not on the ground and then the y_speed goes backwards 25 times for 9 in each
self.speed_y = -25
#self.ypos =- 1
#self.ypos =+ 1
#self.ypos =- 10
#self.ypos =+ 10
# What do you want to do here?
#for x in range(1, 10):
#self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
#for x in range(1, 10):
#self.ypos += 1
# pygame.display.show()
def jump_costume(self):
picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def default_costume(self):
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def shrink(self):
self.picture = pygame.transform.scale(self.picture, (200, 30))
def rise(self):
self.picture = pygame.transform.scale(self.picture, (200, 200))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def teleport(self):
self.xpos = random.randint(1, 1280)
self.ypos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Bullet(pygame.sprite.Sprite):
picture = pygame.image.load("C:/pics/magma_ball.png").convert_alpha()
picture = pygame.transform.scale(picture, (100, 100))
def __init__(self):
self.xpos = 360
self.ypos = 360
self.speed_x = 0
super().__init__()
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class platform:
picture = pygame.image.load("C:/pics/darkcrab.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 75))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
crab = platform(360, 430)
bullet_list = pygame.sprite.Group()
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
hammerhood.jump_costume()
#hammerhood.change(1)
#hammerhood.ypos =- 10
#hammerhood.ypos =+ 10#
#elif event.key == pygame.K_s:
#hammerhood.shrink()
elif event.key == pygame.K_SPACE:
bullet = Bullet()
#bullet.xpos = hammerhood.speed_x
#bullet.ypos = hammerhood.speed_y
bullet.xpos = hammerhood.xpos
bullet.ypos = hammerhood.ypos
bullet.speed_x = 14
bullet_list.add(bullet)
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
#elif event.key == pygame.K_s:
#hammerhood.rise()
#if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
#hammerhood.speed_x = 0
# Update the game.
if hammerhood.is_collided_with(crab):
hammerhood.speed_y = 0
hammerhood.update()
fish.teleport()
#bullet_list.update()
#for bullet in bullet_list:
#bullet.draw(screen)
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
crab.draw()
bullet_list.update()
for bullet in bullet_list:
bullet.draw()
#bullet_list.update()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
python pygame
add a comment |
up vote
0
down vote
favorite
I have looked at collisions in pygame and I do understand how they work but I want to make mine so that if you jump onto the top of the platform (the crab) then it will stay there until you decide to walk off. so far what it currently decides to do is whenever i touch the platform the sprite cannot jump (I also want to make the character to be able to jump on the platform:
import pygame
import random
import sys
import time
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
# Define this in the global scope
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
self.on_ground = True
self.rect = self.picture.get_rect()
def update(self):
# Call this method each frame to update the positions.
#self.xpos += self.speed_x
#self.ypos += self.speed_y
GRAVITY = .9
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Stop falling at the bottom of the screen.
if self.ypos >= 520: #if the position is more than 520 then it will make the ypos 520 and there will be be no y_speed
self.ypos = 520
self.speed_y = 0
self.on_ground = True #this means that you are on the ground
#def change (self, costume):
#if costume == 0:
#picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
#elif costume == 1:
#picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
# Not necessary anymore.
# def move_left(self):
# self.xpos -= 5 # -= not = -5 (augmented assignment).
#
# def move_right(self):
# self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self):
if self.on_ground: # This prevents air jumps. AND WHEN THE FUNCTION IS CALLED IT CHECKS IF YOU ARE ON THE GROUND IF YOU ARE YOU ARE ALLOWED TO JUMP BUT IF YOU ARE IN THE AIR IT WILL NOT WORK
self.on_ground = False #whenever you call this function it checks wether you are on teh gorund or not it can happen if on_ground is true or false
#then on_ground value becomes flase meaning that you are not on the ground and then the y_speed goes backwards 25 times for 9 in each
self.speed_y = -25
#self.ypos =- 1
#self.ypos =+ 1
#self.ypos =- 10
#self.ypos =+ 10
# What do you want to do here?
#for x in range(1, 10):
#self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
#for x in range(1, 10):
#self.ypos += 1
# pygame.display.show()
def jump_costume(self):
picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def default_costume(self):
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def shrink(self):
self.picture = pygame.transform.scale(self.picture, (200, 30))
def rise(self):
self.picture = pygame.transform.scale(self.picture, (200, 200))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def teleport(self):
self.xpos = random.randint(1, 1280)
self.ypos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Bullet(pygame.sprite.Sprite):
picture = pygame.image.load("C:/pics/magma_ball.png").convert_alpha()
picture = pygame.transform.scale(picture, (100, 100))
def __init__(self):
self.xpos = 360
self.ypos = 360
self.speed_x = 0
super().__init__()
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class platform:
picture = pygame.image.load("C:/pics/darkcrab.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 75))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
crab = platform(360, 430)
bullet_list = pygame.sprite.Group()
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
hammerhood.jump_costume()
#hammerhood.change(1)
#hammerhood.ypos =- 10
#hammerhood.ypos =+ 10#
#elif event.key == pygame.K_s:
#hammerhood.shrink()
elif event.key == pygame.K_SPACE:
bullet = Bullet()
#bullet.xpos = hammerhood.speed_x
#bullet.ypos = hammerhood.speed_y
bullet.xpos = hammerhood.xpos
bullet.ypos = hammerhood.ypos
bullet.speed_x = 14
bullet_list.add(bullet)
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
#elif event.key == pygame.K_s:
#hammerhood.rise()
#if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
#hammerhood.speed_x = 0
# Update the game.
if hammerhood.is_collided_with(crab):
hammerhood.speed_y = 0
hammerhood.update()
fish.teleport()
#bullet_list.update()
#for bullet in bullet_list:
#bullet.draw(screen)
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
crab.draw()
bullet_list.update()
for bullet in bullet_list:
bullet.draw()
#bullet_list.update()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
python pygame
If you want to make a platformer, check out the first example here. Please describe your goals in detail if you want to achieve something different. If the linked answer solves your problem, we should close your question because it's a duplicate.
– skrx
Nov 20 at 14:19
@skrx no I want to like make the game so that when the sprite is touching the TOP of the platform the sprite it will just stay or and it can jump off platform if it wants to
– Will.2004
Nov 20 at 20:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have looked at collisions in pygame and I do understand how they work but I want to make mine so that if you jump onto the top of the platform (the crab) then it will stay there until you decide to walk off. so far what it currently decides to do is whenever i touch the platform the sprite cannot jump (I also want to make the character to be able to jump on the platform:
import pygame
import random
import sys
import time
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
# Define this in the global scope
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
self.on_ground = True
self.rect = self.picture.get_rect()
def update(self):
# Call this method each frame to update the positions.
#self.xpos += self.speed_x
#self.ypos += self.speed_y
GRAVITY = .9
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Stop falling at the bottom of the screen.
if self.ypos >= 520: #if the position is more than 520 then it will make the ypos 520 and there will be be no y_speed
self.ypos = 520
self.speed_y = 0
self.on_ground = True #this means that you are on the ground
#def change (self, costume):
#if costume == 0:
#picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
#elif costume == 1:
#picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
# Not necessary anymore.
# def move_left(self):
# self.xpos -= 5 # -= not = -5 (augmented assignment).
#
# def move_right(self):
# self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self):
if self.on_ground: # This prevents air jumps. AND WHEN THE FUNCTION IS CALLED IT CHECKS IF YOU ARE ON THE GROUND IF YOU ARE YOU ARE ALLOWED TO JUMP BUT IF YOU ARE IN THE AIR IT WILL NOT WORK
self.on_ground = False #whenever you call this function it checks wether you are on teh gorund or not it can happen if on_ground is true or false
#then on_ground value becomes flase meaning that you are not on the ground and then the y_speed goes backwards 25 times for 9 in each
self.speed_y = -25
#self.ypos =- 1
#self.ypos =+ 1
#self.ypos =- 10
#self.ypos =+ 10
# What do you want to do here?
#for x in range(1, 10):
#self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
#for x in range(1, 10):
#self.ypos += 1
# pygame.display.show()
def jump_costume(self):
picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def default_costume(self):
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def shrink(self):
self.picture = pygame.transform.scale(self.picture, (200, 30))
def rise(self):
self.picture = pygame.transform.scale(self.picture, (200, 200))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def teleport(self):
self.xpos = random.randint(1, 1280)
self.ypos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Bullet(pygame.sprite.Sprite):
picture = pygame.image.load("C:/pics/magma_ball.png").convert_alpha()
picture = pygame.transform.scale(picture, (100, 100))
def __init__(self):
self.xpos = 360
self.ypos = 360
self.speed_x = 0
super().__init__()
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class platform:
picture = pygame.image.load("C:/pics/darkcrab.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 75))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
crab = platform(360, 430)
bullet_list = pygame.sprite.Group()
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
hammerhood.jump_costume()
#hammerhood.change(1)
#hammerhood.ypos =- 10
#hammerhood.ypos =+ 10#
#elif event.key == pygame.K_s:
#hammerhood.shrink()
elif event.key == pygame.K_SPACE:
bullet = Bullet()
#bullet.xpos = hammerhood.speed_x
#bullet.ypos = hammerhood.speed_y
bullet.xpos = hammerhood.xpos
bullet.ypos = hammerhood.ypos
bullet.speed_x = 14
bullet_list.add(bullet)
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
#elif event.key == pygame.K_s:
#hammerhood.rise()
#if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
#hammerhood.speed_x = 0
# Update the game.
if hammerhood.is_collided_with(crab):
hammerhood.speed_y = 0
hammerhood.update()
fish.teleport()
#bullet_list.update()
#for bullet in bullet_list:
#bullet.draw(screen)
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
crab.draw()
bullet_list.update()
for bullet in bullet_list:
bullet.draw()
#bullet_list.update()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
python pygame
I have looked at collisions in pygame and I do understand how they work but I want to make mine so that if you jump onto the top of the platform (the crab) then it will stay there until you decide to walk off. so far what it currently decides to do is whenever i touch the platform the sprite cannot jump (I also want to make the character to be able to jump on the platform:
import pygame
import random
import sys
import time
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
# Define this in the global scope
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
self.on_ground = True
self.rect = self.picture.get_rect()
def update(self):
# Call this method each frame to update the positions.
#self.xpos += self.speed_x
#self.ypos += self.speed_y
GRAVITY = .9
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Stop falling at the bottom of the screen.
if self.ypos >= 520: #if the position is more than 520 then it will make the ypos 520 and there will be be no y_speed
self.ypos = 520
self.speed_y = 0
self.on_ground = True #this means that you are on the ground
#def change (self, costume):
#if costume == 0:
#picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
#elif costume == 1:
#picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
#picture = pygame.transform.scale(picture, (200, 200))
# Not necessary anymore.
# def move_left(self):
# self.xpos -= 5 # -= not = -5 (augmented assignment).
#
# def move_right(self):
# self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self):
if self.on_ground: # This prevents air jumps. AND WHEN THE FUNCTION IS CALLED IT CHECKS IF YOU ARE ON THE GROUND IF YOU ARE YOU ARE ALLOWED TO JUMP BUT IF YOU ARE IN THE AIR IT WILL NOT WORK
self.on_ground = False #whenever you call this function it checks wether you are on teh gorund or not it can happen if on_ground is true or false
#then on_ground value becomes flase meaning that you are not on the ground and then the y_speed goes backwards 25 times for 9 in each
self.speed_y = -25
#self.ypos =- 1
#self.ypos =+ 1
#self.ypos =- 10
#self.ypos =+ 10
# What do you want to do here?
#for x in range(1, 10):
#self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
#for x in range(1, 10):
#self.ypos += 1
# pygame.display.show()
def jump_costume(self):
picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def default_costume(self):
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def shrink(self):
self.picture = pygame.transform.scale(self.picture, (200, 30))
def rise(self):
self.picture = pygame.transform.scale(self.picture, (200, 200))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def teleport(self):
self.xpos = random.randint(1, 1280)
self.ypos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Bullet(pygame.sprite.Sprite):
picture = pygame.image.load("C:/pics/magma_ball.png").convert_alpha()
picture = pygame.transform.scale(picture, (100, 100))
def __init__(self):
self.xpos = 360
self.ypos = 360
self.speed_x = 0
super().__init__()
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
class platform:
picture = pygame.image.load("C:/pics/darkcrab.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 75))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.rect = self.picture.get_rect()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
crab = platform(360, 430)
bullet_list = pygame.sprite.Group()
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
hammerhood.jump_costume()
#hammerhood.change(1)
#hammerhood.ypos =- 10
#hammerhood.ypos =+ 10#
#elif event.key == pygame.K_s:
#hammerhood.shrink()
elif event.key == pygame.K_SPACE:
bullet = Bullet()
#bullet.xpos = hammerhood.speed_x
#bullet.ypos = hammerhood.speed_y
bullet.xpos = hammerhood.xpos
bullet.ypos = hammerhood.ypos
bullet.speed_x = 14
bullet_list.add(bullet)
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
#elif event.key == pygame.K_s:
#hammerhood.rise()
#if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
#hammerhood.speed_x = 0
# Update the game.
if hammerhood.is_collided_with(crab):
hammerhood.speed_y = 0
hammerhood.update()
fish.teleport()
#bullet_list.update()
#for bullet in bullet_list:
#bullet.draw(screen)
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
crab.draw()
bullet_list.update()
for bullet in bullet_list:
bullet.draw()
#bullet_list.update()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
python pygame
python pygame
asked Nov 19 at 18:02
Will.2004
124
124
If you want to make a platformer, check out the first example here. Please describe your goals in detail if you want to achieve something different. If the linked answer solves your problem, we should close your question because it's a duplicate.
– skrx
Nov 20 at 14:19
@skrx no I want to like make the game so that when the sprite is touching the TOP of the platform the sprite it will just stay or and it can jump off platform if it wants to
– Will.2004
Nov 20 at 20:00
add a comment |
If you want to make a platformer, check out the first example here. Please describe your goals in detail if you want to achieve something different. If the linked answer solves your problem, we should close your question because it's a duplicate.
– skrx
Nov 20 at 14:19
@skrx no I want to like make the game so that when the sprite is touching the TOP of the platform the sprite it will just stay or and it can jump off platform if it wants to
– Will.2004
Nov 20 at 20:00
If you want to make a platformer, check out the first example here. Please describe your goals in detail if you want to achieve something different. If the linked answer solves your problem, we should close your question because it's a duplicate.
– skrx
Nov 20 at 14:19
If you want to make a platformer, check out the first example here. Please describe your goals in detail if you want to achieve something different. If the linked answer solves your problem, we should close your question because it's a duplicate.
– skrx
Nov 20 at 14:19
@skrx no I want to like make the game so that when the sprite is touching the TOP of the platform the sprite it will just stay or and it can jump off platform if it wants to
– Will.2004
Nov 20 at 20:00
@skrx no I want to like make the game so that when the sprite is touching the TOP of the platform the sprite it will just stay or and it can jump off platform if it wants to
– Will.2004
Nov 20 at 20:00
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380286%2fchecking-when-the-player-is-touching-the-top-of-a-platform%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you want to make a platformer, check out the first example here. Please describe your goals in detail if you want to achieve something different. If the linked answer solves your problem, we should close your question because it's a duplicate.
– skrx
Nov 20 at 14:19
@skrx no I want to like make the game so that when the sprite is touching the TOP of the platform the sprite it will just stay or and it can jump off platform if it wants to
– Will.2004
Nov 20 at 20:00