help me make a player shoot and collide? =]

markwearspants

Still Fresh
Joined
Mar 13, 2013
Messages
1
First time playing with pygame and a novice at python. been playing for a while and made a "character" move around on screen. I have a square that bounces from side to side and would like to know how to make an event happen if the two collide. or event spit out a print line, I should be alright from there.

Also if I was to press the "w" key for example. the "character" shoots out one of these squares in the direction of the key. eg W shoots up, S shoots down etc.

attached is a zip of what I have including images. Thanks =]

**So I can't upload a zip, below is the code I have**

Code:
bg="bg.jpg"
character="toon.png"
character2="toon2.png"
bulletImg="bullet.png"

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640,360),0,32)

background=pygame.image.load(bg).convert()
toon = pygame.image.load(character).convert_alpha()
toon1 = pygame.image.load(character).convert_alpha()
toon2 = pygame.image.load(character2).convert_alpha()
bullet = pygame.image.load(bulletImg).convert_alpha()

x = 0
y = 0
movex = 0
movey = 0

z = 0
clock = pygame.time.Clock()
speed = 300

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                movex = -0.10
                toon = toon2
            elif event.key == K_RIGHT:
                movex = +0.10
                toon = toon2
            elif event.key == K_UP:
                movey = -0.10
                toon = toon2
            elif event.key == K_DOWN:
                movey = +0.10
                toon = toon2
            elif event.key == K_SPACE:
                print 'balls'
                                
        if event.type == KEYUP:
            if event.key == K_LEFT:
                movex = 0
                toon = toon1
            elif event.key == K_RIGHT:
                movex = 0
                toon = toon1
            elif event.key == K_UP:
                movey = 0
                toon = toon1
            elif event.key == K_DOWN:
                movey = 0
                toon = toon1
                
    x += movex
    y += movey
    
    if x > 580:
        x = 580
    if y > 300:
        y = 300
    if x < 0:
        x = 0
    if y < 0:
        y = 0
        
    milli = clock.tick()
    seconds = milli/1000.
    dm = seconds * speed
    z +=dm
    if z > 620:
        speed = -300
    if z < 0:
        speed = 300

    screen.blit(background,(0,0))
    screen.blit(toon,(x,y))
    screen.blit(bullet, (z, 160))
      
    pygame.display.update()
 
Last edited by a moderator:
Top