python - Pygame: Spaceinvaders: can't shoot bullets -
i building mockup of space invaders. want make user shoot stuff , invaders die (1hit kill), can't seem figure out how that. have tried drawing small rectangle, background paints on it. help!
import pygame, sys, random pygame.locals import * # set pygame pygame.init() mainclock = pygame.time.clock() # set window windowwidth = 800 windowheight = 700 windowsurface = pygame.display.set_mode((windowwidth, windowheight), 0, 32) pygame.display.set_caption('sp@c3 inv@d3r5') # set movement variables moveleft = false moveright = false moveup = false movedown = false # set direction variables downleft = 1 downright = 3 upleft = 7 upright = 9 left = 4 right = 6 = 8 down = 2 movespeed = 10 move = 2 # set counting score = 0 # set colors black = (0, 0, 0) green = (0, 255, 0) white = (255, 255, 255) red = (255, 0, 0) blue = (135, 206, 250) # set font font = pygame.font.sysfont(none, 50) def makeship(windowwidth): # set bouncer , food data structures user = pygame.rect(350, 600, 100, 25) return user def drawwalls(walls): in range(6): wall = pygame.rect(0,i+1,100,25) pygame.draw.rect(windowsurface,green,wall) walls.append(wall) def makeinvaders(invaders): y = 0 in invaders: # each row x = 0 j in range(10): # create invader invader = pygame.rect(75+x, 100+y, 40, 25) # append invader invaders[row] i.append(invader) # increase invader's x attribute 50 x += 60 # increase invaders y 35 y += 45 return invaders def movepaddle(user): # move paddle if moveleft , user.left > 0: user.left -= movespeed if moveright , user.right < windowwidth: user.right += movespeed return user def moveinvaders(invaders, invdir): # move invaders row in invaders: invader in row: if invdir == right , invaders[1][9].right < windowwidth: invader.right += move elif invaders[1][9].right > windowwidth: invader.left -= move invdir = left if invdir == left , invaders[0][0].left > 0: invader.left -= move elif invaders[0][0].left < 0: invader.right += move invdir = right return invdir def shootbullets(windowsurface,blue2): x = pygame.rect(400,595,2,5) pygame.draw.rect(windowsurface,green,x) def movebullets(bullets): bullet in bullets: pass def drawstuff(user, invaders): # draw user onto surface pygame.draw.rect(windowsurface, black, user) in invaders: invader in i: if invader in invaders[0]: pygame.draw.rect(windowsurface, black, invader) elif invader in invaders[1]: pygame.draw.rect(windowsurface, black, invader) elif invader in invaders[2]: pygame.draw.rect(windowsurface, black, invader) elif invader in invaders[3]: pygame.draw.rect(windowsurface, black, invader) elif invader in invaders[4]: pygame.draw.rect(windowsurface, black, invader) invaders = [[],[],[],[],[]] invdir = left walls = [] drawwalls(walls) # make figures user = makeship(windowwidth) # make invaders invaders = makeinvaders(invaders) # run game loop while true: # draw black background onto surface windowsurface.fill(white) # check quit event event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() if event.type == keydown: # change keyboard variables if event.key == k_left or event.key == ord('a'): moveright = false moveleft = true if event.key == k_right or event.key == ord('d'): moveleft = false moveright = true if event.key == k_space: shootbullets(windowsurface,red) if event.type == keyup: if event.key == k_escape: pygame.quit() sys.exit() if event.key == k_left or event.key == ord('a'): moveleft = false if event.key == k_right or event.key == ord('d'): moveright = false # draw stuff drawstuff(user, invaders) # move invaders invdir = moveinvaders(invaders, invdir) # paddle movement user = movepaddle(user) # draw window onto screen pygame.display.update() mainclock.tick(80)
take @ minimal space invaders-like game i've written question:
import pygame # you'll able shoot every 450ms reload_speed = 450 # foes move every 1000ms sideways , every 3500ms down move_side = 1000 move_down = 3500 screen = pygame.display.set_mode((300, 200)) clock = pygame.time.clock() pygame.display.set_caption("micro invader") # create bunch of events move_side_event = pygame.userevent + 1 move_down_event = pygame.userevent + 2 reloaded_event = pygame.userevent + 3 move_left, reloaded = true, true invaders, colors, shots = [], [] ,[] x in range(15, 300, 15): y in range(10, 100, 15): invaders.append(pygame.rect(x, y, 7, 7)) colors.append(((x * 0.7) % 256, (y * 2.4) % 256)) # set timer movement events pygame.time.set_timer(move_side_event, move_side) pygame.time.set_timer(move_down_event, move_down) player = pygame.rect(150, 180, 10, 7) while true: clock.tick(40) if pygame.event.get(pygame.quit): break e in pygame.event.get(): if e.type == move_side_event: invader in invaders: invader.move_ip((-10 if move_left else 10, 0)) move_left = not move_left elif e.type == move_down_event: invader in invaders: invader.move_ip(0, 10) elif e.type == reloaded_event: # when reload timer runs out, reset reloaded = true pygame.time.set_timer(reloaded_event, 0) shot in shots[:]: shot.move_ip((0, -4)) if not screen.get_rect().contains(shot): shots.remove(shot) else: hit = false invader in invaders[:]: if invader.colliderect(shot): hit = true = invaders.index(invader) del colors[i] del invaders[i] if hit: shots.remove(shot) pressed = pygame.key.get_pressed() if pressed[pygame.k_left]: player.move_ip((-4, 0)) if pressed[pygame.k_right]: player.move_ip((4, 0)) if pressed[pygame.k_space]: if reloaded: shots.append(player.copy()) reloaded = false # when shooting, create timeout of reload_speed pygame.time.set_timer(reloaded_event, reload_speed) player.clamp_ip(screen.get_rect()) screen.fill((0, 0, 0)) invader, (a, b) in zip(invaders, colors): pygame.draw.rect(screen, (150, a, b), invader) shot in shots: pygame.draw.rect(screen, (255, 180, 0), shot) pygame.draw.rect(screen, (180, 180, 180), player) pygame.display.flip()
it uses colliderect
detect collisions, code missing.
also, code in general , movement code lot simpler yours; maybe can serve inspiration you.
Comments
Post a Comment