Я смотрел на столкновения в Pygame, и я понимаю, как они работают, но я хочу сделать мои так, чтобы, если вы прыгаете на вершину платформы (краб), она останется там, пока вы не решите уйти.до сих пор он решает, когда я касаюсь платформы, спрайт не может прыгнуть (я также хочу, чтобы персонаж мог прыгать на платформе:
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.