added beta shooting

This commit is contained in:
Marcin Kostrzewski 2019-01-21 22:55:58 +01:00
parent 9e5b73b27c
commit 80aee6c546
7 changed files with 52 additions and 9 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -24,3 +24,6 @@ P2_RIGHT=pygame.K_RIGHT
P2_LEFT=pygame.K_LEFT P2_LEFT=pygame.K_LEFT
P2_SHOOT=pygame.K_RCTRL P2_SHOOT=pygame.K_RCTRL
# bullets
BULLET_SPEED=20
SHOOT_SPEED=10

View File

@ -3,10 +3,16 @@ import pygame
from pygame.locals import * from pygame.locals import *
from sprites import * from sprites import *
from config import *
running=True running=True
players=[] players=[]
p1_bullet=[]
p2_bullet=[]
p1_bullet_group=pygame.sprite.Group()
p2_bullet_group=pygame.sprite.Group()
def events(): def events():
global running global running
for event in pygame.event.get(): for event in pygame.event.get():
@ -36,6 +42,12 @@ def collision_check(p):
return False return False
def player1_input(keys): def player1_input(keys):
if keys[P1_SHOOT]:
p1_bullet.append(Bullet('bullet', players[0].pos.x, players[0].pos.y, players[0].facing))
p1_bullet_group.add(p1_bullet[-1])
all_sprites.add(p1_bullet[-1])
p1_bullet[-1].direction=players[0].facing
p1_bullet[-1].shoot()
if not (keys[P1_UP] or keys[P1_DOWN] or keys[P1_RIGHT] or keys[P1_LEFT]): if not (keys[P1_UP] or keys[P1_DOWN] or keys[P1_RIGHT] or keys[P1_LEFT]):
players[0].stopmoving() players[0].stopmoving()
elif not collision_check(0): elif not collision_check(0):
@ -49,6 +61,12 @@ def player1_input(keys):
players[0].movedown() players[0].movedown()
def player2_input(keys): def player2_input(keys):
if keys[P2_SHOOT]:
p2_bullet.append(Bullet('bullet', players[1].pos.x, players[1].pos.y, players[1].facing))
p2_bullet_group.add(p1_bullet[-1])
all_sprites.add(p2_bullet[-1])
p2_bullet[-1].direction=players[1].facing
p2_bullet[-1].shoot()
if not (keys[P2_UP] or keys[P2_DOWN] or keys[P2_RIGHT] or keys[P2_LEFT]): if not (keys[P2_UP] or keys[P2_DOWN] or keys[P2_RIGHT] or keys[P2_LEFT]):
players[1].stopmoving() players[1].stopmoving()
elif not collision_check(1): elif not collision_check(1):

View File

@ -20,8 +20,8 @@ clock=pygame.time.Clock()
# initialize two players # initialize two players
events.players.append(Player('asd', 400, 100, 5)) events.players.append(Player('asd', 400, 100, 10))
events.players.append(Player('qwe', 400, 500, 5)) events.players.append(Player('qwe', 400, 500, 10))
all_sprites.add(events.players) all_sprites.add(events.players)
events.running=True events.running=True
@ -34,13 +34,16 @@ def draw_debug_text():
text_sprites=pygame.sprite.Group() text_sprites=pygame.sprite.Group()
texts=[ texts=[
font.render("P1 facing: " + str(events.players[0].facing), True, (100,100,100)), font.render("P1 facing: " + str(events.players[0].facing), True, (100,100,100)),
font.render("P2 facing: " + str(events.players[1].facing), True, (100,100,100)) font.render("P2 facing: " + str(events.players[1].facing), True, (100,100,100)),
font.render("P1 bullets: " + str(len(events.p1_bullet)), True, (100,100,100)),
font.render("P2 bullets: " + str(len(events.p2_bullet)), True, (100,100,100))
] ]
count=0 count=0
for text in texts: for text in texts:
screen.blit(text, (0,count*12)) screen.blit(text, (0,count*12))
count+=1 count+=1
# set hitbox objects as screen boundaries ### DEBUG ###
# game loop # game loop
while events.running: while events.running:

View File

@ -1,6 +1,6 @@
# import all files # import all files
from config import * from config import *
import events #import events
# import modules # import modules
import os, sys import os, sys
import pygame import pygame
@ -67,9 +67,6 @@ class Player(pygame.sprite.Sprite):
self.acc.x=-1.5 self.acc.x=-1.5
self.acc.y=0 self.acc.y=0
def shoot(self):
nothing
def stopmoving(self): def stopmoving(self):
self.acc=vec(0, 0) self.acc=vec(0, 0)
@ -97,7 +94,29 @@ class Player(pygame.sprite.Sprite):
self.move() self.move()
class Bullet(pygame.sprite.Sprite): class Bullet(pygame.sprite.Sprite):
def __init__(self, fname): def __init__(self, fname, x, y, direction):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.image, self.rect=load_img(fname + '.png') self.image, self.rect=load_img(fname + '.png')
self.rect.center=(x, y)
self.speed=vec(0, 0)
self.direction=direction
def shoot(self):
if self.direction==0:
self.speed.y=-BULLET_SPEED
elif self.direction==1:
self.speed.x=BULLET_SPEED
elif self.direction==2:
self.speed.y=BULLET_SPEED
elif self.direction==3:
self.speed.x=-BULLET_SPEED
self.rect.center+=self.speed
def update(self):
self.shoot()
class Hitbox(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
self.size=vec(w, h)
self.rect=self.size.get_rect()
self.rect.center=vec(x,y)