python collision detection? -
so i've been working on python game project.i'm having trouble if put obstacle in game can't make respond picture when picture collides it, game ends. i've been @ quite sometime i'm unable figure out code.any appreciated.i need this.please not i'm beginner , started studying python month ago.so please try understand. i've attached code below.
import pygame, random, sys pygame.locals import * backgroundcolor = (181, 230, 29) fps = 30 pixels = 5 pygame.init() mainclock = pygame.time.clock() windowsurface = pygame.display.set_mode((990, 557)) pygame.display.set_caption('clumsy claire :d') background = pygame.image.load('grass.jpg') backgroundrect = background.get_rect() size = (990, 557) background.get_size() image = pygame.image.load('snail 2.png') imagerect = image.get_rect() stone1 = pygame.image.load('rock.png') stone1rect = stone1.get_rect() stone2 = pygame.image.load('rock.png') stone2rect = stone2.get_rect() brown = (128,64,0) pygame.draw.line(background, brown, (98, 555), (98,69), 12) pygame.draw.line(background, brown, (98, 16), (98,1), 12) pygame.draw.line(background, brown, (94, 3), (283, 3),12) pygame.draw.line(background, brown, (278, 457), (278, 3),12) pygame.draw.line(background, brown, (278, 554), (278, 512),12) pygame.draw.line(background, brown, (274, 554), (470, 554),12) pygame.draw.line(background, brown, (465, 554), (465, 90),12) pygame.draw.line(background, brown, (465, 35), (465, 0),12) pygame.draw.line(background, brown, (465, 3), (657, 3),12) pygame.draw.line(background, brown, (652,555 ), (652, 502),12) pygame.draw.line(background, brown, (652, 449), (652, 0),12) pygame.draw.line(background, brown, (648, 553), (844, 553),12) pygame.draw.line(background, brown, (838, 553 ), (838, 138),12) pygame.draw.line(background, brown, (838, 84 ), (838, 0),12) while true: imagerect.topleft = (10,488) moveleft = false moveright = false moveup = false movedown = false while true: event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() if event.type == keydown: if event.key == k_left: moveleft = true if event.key == k_right: moveright = true if event.key == k_up: moveup = true if event.key == k_down: movedown = true if event.type == keyup: if event.key == k_left: moveleft = false if event.key == k_right: moveright = false if event.key == k_up: moveup = false if event.key == k_down: movedown = false if moveleft , imagerect.left > 0: imagerect.move_ip(-1 * pixels, 0) if moveright , imagerect.right < 990: imagerect.move_ip(pixels, 0) if moveup , imagerect.top > 0: imagerect.move_ip(0, -1 * pixels) if movedown , imagerect.bottom < 557: imagerect.move_ip(0, pixels) windowsurface.blit(background, backgroundrect) windowsurface.blit(image, imagerect) rock1 = background.blit(stone1,(658,337)) rock2 = background.blit(stone2,(225,150)) pygame.display.update() mainclock.tick(fps)
you need add colliderect
function code among other things. right have no way test collisions. paste below blit code:
if imagerect.colliderect(stone1rect): print('game over') pygame.quit() if imagerect.colliderect(stone2rect): print('game over') pygame.quit()
this code here:
rock1 = background.blit(stone1,(658,337)) rock2 = background.blit(stone2,(225,150))
also needs changed this:
windowsurface.blit(stone1, (658, 337)) windowsurface.blit(stone2, (225, 150))
the reason need change above this: code blitting on background image instead of window; bad practice.
for reason, i'm guessing you're learning python inventwithpython.org ;d that's way learned (if guess right ;d)
if need more or have questions comment below. luck!
Comments
Post a Comment