Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Trying to have collision detection in pygame. Drawing a maze to the screen. If the player sprite hits the wall sprite I just want the

Trying to have collision detection in pygame. Drawing a maze to the screen. If the player sprite hits the wall sprite I just want the player sprite to not go through the wall. The method I use is called checkCollision which takes two parameters (sprite1,sprite2)

import pygame,sys from pygame.locals import* from maze_class import Maze from button_class import pill_button

pygame.init()

WHITE=(255,255,255)

DISPLAYSURF=pygame.display.set_mode((1000,600),DOUBLEBUF,32)

pygame.display.set_caption("ARGG")

BUTTONW = 300 BUTTONH = 72

BUTTON1 = pill_button(BUTTONH, BUTTONW, (100, 100, 100), (225, 225, 225), "Start Game", DISPLAYSURF, (343,300)) BUTTON1.active=True

CLOCK=pygame.time.Clock()

count=0 def gameStart(surf): start=True intro=pygame.image.load('introScreen.png') while start: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN: mouseXY = pygame.mouse.get_pos() if BUTTON1.clicked(mouseXY): BUTTON1.hilighted = True elif event.type==MOUSEBUTTONUP: if BUTTON1.clicked(mouseXY): BUTTON1.hilighted=False main() gameStart=False surf.blit(intro,(0,0)) BUTTON1.display() pygame.display.update()

def checkCollision(sprite1,sprite2): col=pygame.sprite.collide_rect(sprite1,sprite2) if col==True: px=0

def main(): background=pygame.image.load('ship.png') block=pygame.image.load('square1.png') wallRect=block.get_rect() player=pygame.image.load('rat.png') playerRect=player.get_rect()

player=pygame.transform.scale(player,(30,30)) x=365 y=405 px=0 py=0 game=True while game:

for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if (event.type == pygame.KEYDOWN): if (event.key==pygame.K_LEFT): player=pygame.transform.scale(player,(30,30)) px=-5 if (event.key==pygame.K_RIGHT): player=pygame.transform.scale(player,(30,30)) px=5 if (event.key==pygame.K_UP): py=-5 if (event.key==pygame.K_DOWN): py=5 if (event.type == pygame.KEYUP): if (event.key==pygame.K_LEFT): px=0 if (event.key==pygame.K_RIGHT): px=0 if (event.key==pygame.K_UP): py=0 if (event.key==pygame.K_DOWN): py=0 DISPLAYSURF.blit(background,(0,0)) maze=Maze() maze.draw(DISPLAYSURF,block) checkCollision(wallRect,playerRect) DISPLAYSURF.blit(player,(x,y)) x+=px y+=py CLOCK.tick(50) pygame.display.update()

gameStart(DISPLAYSURF)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

6. Vanguard

Answered: 1 week ago