In this game I am creating one image is controlled by the players using arrows (racecar) and two images (banana) which move in a defined direction and speed (which is slowed down for now so I could see better what was happening) but their starting point is random. The goal would be for the car to avoid these blocks and to reach the finish line, only when going forward it is then impossible to pass these bananas without the crash segment to start even when there is clearly no contact. It is quit tricky because things move on the X axis. please help me. Here is the code segment for collision between the two images:
if x + car_width >= banX:
print('ycollision')
if y + car_width >= banX and y + car_width <= banX + ban_height or ybottom + car_width >= banX and ybottom + car_width <= banX + ban_height:
print('xcollision')
crash()
Here is the whole code:
import pygame
import random
import time
pygame.init()
#Colors
black = (0,0,0)
white = (255,255,255)
grey = (96,96,96)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
#Setting and variables
display_width = 1570
display_height = 450
car_width = 98
car_height = 66
clock = pygame.time.Clock()
wn = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('My own game')
#racecar
carImg = pygame.image.load('myOwnRGame.png')
Xchange = 0
Ychange = 0
y = 192
x = 10
def car(x,y):
wn.blit(carImg, (x,y))
#finish line
finish_line = pygame.image.load('myOwnFinishreal.png')
Xfin = 1480
Yfin = 0
def finish():
wn.blit(finish_line, (Xfin, Yfin))
#Crashing and wining
def textObjects(text, font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def displayMessage(text):
textFont1 = pygame.font.Font('freesansbold.ttf', 32)
textSurf, textRect = textObjects(text, textFont1)
textRect.center = ((display_width/2),(display_height/2))
while True:
wn.blit(textSurf, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.display.quit()
pygame.quit()
if event.key == pygame.K_SPACE:
gameLoop()
pygame.display.update()
def crash():
displayMessage('You crashed!Press X to quit, _SPACE_ to restart!')
def win():
displayMessage('Bravo! You are the best car runner! Press X to quit and _SPACE_ to restart.')
#Banana widht48 height26
banImg = pygame.image.load('myOwnBanana.png')
ban_width = 48
ban_height = 26
def banana(banX, banY):
wn.blit(banImg, (banX, banY))
def banana2(banX, banY2):
wn.blit(banImg, (banX, banY2))
#Game loop
def gameLoop():
y = 192
x = 10
Xchange = 0
Ychange = 0
banX = 1600
banY = random.randrange(-5, (display_height - 21))
banY2 = random.randrange(-5, (display_height - 21))
ban_change = -3
alive = True
losing = True
while alive and losing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.display.quit()
pygame.quit()
if event.key == pygame.K_UP:
Xchange = 0
Ychange = 0
Xchange = 2.5
elif event.key == pygame.K_LEFT:
Xchange = 0
Ychange = 0
Ychange = -3
elif event.key == pygame.K_RIGHT:
Xchange = 0
Ychange = 0
Ychange = 3
elif event.key == pygame.K_DOWN:
Xchange = 0
Ychange = 0
Xchange = -3
#Borders
if y <= -15 or y >= display_height - 15:
Xchange = 0
Ychange = 0
ban_change = 2.5
crash()
if x >= display_width:
Xchange = 0
Ychange = 0
ban_change = 2.5
win()
if x <= 0:
x = 10
#Banana spon
if banX <= 0:
banY = random.randrange(-5, (display_height - 21))
banY2 = random.randrange(-5, (display_height - 21))
banX = 1540
x += Xchange
y += Ychange
ybottom = y + car_height
#Banana collision
if x + car_width >= banX:
print('ycollision')
if y + car_width >= banX and y + car_width <= banX + ban_height or ybottom + car_width >= banX and ybottom + car_width <= banX + ban_height:
print('xcollision')
crash()
wn.fill(grey)
finish()
car(x, y)
banana(banX, banY)
banana2(banX, banY2)
banX += ban_change
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
gameLoop()
Question summary: How could I make the bananas and car collide but still be able to dodge the collision by moving the car?
I recommend to draw the objects before invoking crash
, to show the actual position of the objects when crashing. Note the objects have been moved, but not redrawn before the crash
wn.fill(grey)
finish()
car(x, y)
banana(banX, banY)
banana2(banX, banY2)
if # collision test
crash()
To evaluate if to rectangles (x1, y1, w1, h1) and _(x2, y2, w2, h2) are intersecting, you a have to evaluate if the rectangles are overlapping in both dimensions:
collide = x1 < x2+w2 and x2 < x1+w1 and y1 < y2+h2 and y2 < y1+h1
For your code that ma look as follows:
if x < banX + ban_width and banX < x + car_width:
if y < banY + ban_height and banY < y + car_height:
crash()
Anyway I recommend to pygame.Rect
and colliderect()
for the collision test. Get the rectangles form the pygame.Surface
objects carImg
and banImg
by get_rect()
. FOr instance:
car_rect = carImg.get_rect(topleft = (x, y))
ban_rect = banImg.get_rect(topleft = (banX, banY))
ban2_rect = banImg.get_rect(topleft = (banX, banY2))
if car_rect.colliderect(ban_rect) or car_rect.colliderect(ban2_rect):
crash()