Whenever my bird collides with a base then it will jump and his y position will be the y position of the base.
Basically i am stuck at the point whenever the bird jumps from the bottom and while jumping it touches a base it comes down to the starting position of the jump however if it collides with any base in the jump it should take the y coordinate of the bird equal to the y coordinate of that base.
import pygame, sys
from random import randint
pygame.init()
screen = pygame.display.set_mode((350,675))
pygame.display.set_caption("First Game")
x_poss = randint(0,350)
y_poss = randint(0,675)
width = 20
height = 20
vel = 8
bases = []
red = (255,0,0)
image_1 = pygame.image.load('bird.png').convert_alpha()
image_2 = pygame.image.load('base.png').convert_alpha()
image_3 = pygame.image.load('base2.png').convert_alpha()
image_4 = pygame.image.load('enemy.png').convert_alpha()
isJump = False
jumpCount = 10
run = True
base_1 = pygame.draw.rect(screen, (255,255,255), (randint(0,330), randint(0,675), width, height))
base_2 = pygame.draw.rect(screen, (255,255,255), (randint(0,330), randint(0,675), width, height))
def draw_rect(color, x, y, width, height):
pygame.draw.rect(screen, color, (x,y,width, height))
while len(bases) < 10:
rect = pygame.Rect(randint(0,330), randint(0,675), width, height)
if rect.collidelist(bases) < 0:
bases.append(rect)
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and x_poss > vel - width:
x_poss -= vel
if key[pygame.K_q]:
run = False
sys.exit()
if key[pygame.K_RIGHT] and x_poss < 350 - vel - width:
x_poss += vel
if not(isJump):
if key[pygame.K_UP] and y_poss > vel:
y_poss -= vel
if key[pygame.K_DOWN] and y_poss < 670 - height - vel:
y_poss += vel
if key[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
y_poss -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False
bird = pygame.draw.rect(screen, (255,255,255), (x_poss, y_poss, width, height))
def jump():
global jumpCount, y_poss
for base in bases:
if bird.colliderect(base):
isJump = True
if jumpCount >= -10:
y_poss -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False
screen.fill((255,255,255))
draw_rect((255,255,255),x_poss, y_poss, width, height)
screen.blit(image_1, pygame.draw.rect(screen, (255,255,255), (x_poss, y_poss, width, height)))
for base_1 in bases[:5]:
screen.blit(image_2, base_1)
for base_2 in bases[5:]:
screen.blit(image_3, base_2)
for i, base_1 in enumerate(bases):
if base_1.collidelist(bases[i+1:]) >= 0:
bases.remove(base_1)
bases.append(pygame.draw.rect(screen, (255,255,255), (randint(0,330), randint(0,675), width, height)))
for i, base_2 in enumerate(bases):
if base_2.collidelist(bases[i+1:]) >= 0:
bases.remove(base_2)
bases.append(pygame.draw.rect(screen, (255,255,255), (randint(0,330), randint(0,675), width, height)))
jump()
for base in bases:
base.y += 0
draw_rect((0,0,0),300, 440, 10, 10)
screen.blit(image_4, pygame.draw.rect(screen, (0,0,0), (300,440,10,10)))
pygame.display.update()
pygame.quit()
I think the adjustments should be in this piece of code:
def jump():
global jumpCount, y_poss
for base in bases:
if bird.colliderect(base):
isJump = True
if jumpCount >= -10:
y_poss -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False