如何与其他玩家同时射击子弹,以及在矩形和玩家之间创建边界的简便方法?

时间:2019-01-23 01:15:53

标签: python pygame

我正在创建2D游戏,我的代码遇到了一些问题。首先,我的玩家不能同时发射子弹。另外,它们是否是在我的两个玩家和屏幕上的矩形之间创建边界的更简单的方法或函数,还是我只需要对矩形外部的点使用pyagame.Rect.collidepoint(),以及与矩形碰撞我会说: if keystate[pygame.K_w] and not pygame.Rect.collidepoint(): self.y_change = -5

主要代码:

import OpenGL
import panda3d
import pygame
import pyglet
import tkinter
import time
import random

pygame.init()
display_width = 1200
display_height = 600

green = (0, 255, 0)
yellow = (255, 255, 0)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)

person_height = 113
person_width = 150
person2_width = 150
person2_height = 113


gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Wert')
clock = pygame.time.Clock()

personImg = pygame.image.load('Untitled.png').convert_alpha()
personImg2 = pygame.image.load('Untitled2.png').convert_alpha()
bulletImg = pygame.image.load('Untitled1.png').convert_alpha()

player_health = 100
player2_health = 100
x = (0)
y = (display_height * 0.37)
ny = (display_height * 0.37)
nx = (display_width * 0.87)
thing_width = 100
thing_height = 100





class Player(pygame.sprite.Sprite):
 def __init__(self):
     pygame.sprite.Sprite.__init__(self)
     self.image = personImg
     self.rect = self.image.get_rect()
     self.rect.x = x
     self.rect.y = y
     self.player_health = 100
     self.x_change = 0
     self.y_change = 0


 def update(self):
     self.x_change = 0
     self.y_change = 0
     keystate = pygame.key.get_pressed()
     if keystate[pygame.K_w] and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height):
         self.y_change = -5
     if keystate[pygame.K_s] and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y):
         self.y_change = 5
     if keystate[pygame.K_d] and pygame.key.get_mods() and pygame.KMOD_LSHIFT and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10):
         self.x_change = 10
     elif keystate[pygame.K_d] and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10):
         self.x_change = 5
     if keystate[pygame.K_a] and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
         self.x_change = -5
     self.rect.x += self.x_change
     self.rect.y +=self.y_change
     if self.rect.right > display_width:
         self.rect.right = display_width
     if self.rect.left < 0:
         self.rect.left = 0
     if self.rect.top < 0:
         self.rect.top = 0
     if self.rect.bottom > display_height:
         self.rect.bottom = display_height


 def shoot(self):
     bullet = Bullet1(self.rect.x, self.rect.y)
     all_sprites.add(bullet)
     bullets.add(bullet)





class Player2(pygame.sprite.Sprite):
 def __init__(self):
     pygame.sprite.Sprite.__init__(self)
     self.image = personImg2
     self.rect = self.image.get_rect()
     self.rect.x = nx
     self.rect.y = ny
     self.player2_health = 100
     self.x_change1 = 0
     self.y_change1 = 0

 def update(self):
     self.x_change1 = 0
     self.y_change1 = 0
     keystate = pygame.key.get_pressed()
     if keystate[pygame.K_UP] and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/2, player.rect.y + person_height) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/4, player.rect.y + person_height) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width - 10, player.rect.y + person_height):
         self.y_change1 = -5
     if keystate[pygame.K_DOWN] and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/2, player.rect.y) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width/4, player.rect.y) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width - 10, player.rect.y):
         self.y_change1 = 5
     if keystate[pygame.K_LEFT] and pygame.key.get_mods() and pygame.KMOD_RSHIFT and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/2) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/4) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height - 10):
         self.x_change1 = -10
     elif keystate[pygame.K_LEFT] and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/2) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height/4) and not pygame.Rect.collidepoint(self.rect, player.rect.x + person_width, player.rect.y + person_height - 10):
         self.x_change1 = -5
     if keystate[pygame.K_RIGHT] and not pygame.Rect.collidepoint(self.rect, player.rect.x, player.rect.y + person_height/2) and not pygame.Rect.collidepoint(self.rect, player.rect.x, player.rect.y + person_height/4) and not pygame.Rect.collidepoint(self.rect, player.rect.x, player.rect.y + person_height - 10):
         self.x_change1 = 5
     self.rect.x += self.x_change1
     self.rect.y += self.y_change1
     if self.rect.right > display_width:
         self.rect.right = display_width
     if self.rect.left < 0:
         self.rect.left = 0
     if self.rect.top < 0:
         self.rect.top = 0
     if self.rect.bottom > display_height:
         self.rect.bottom = display_height

 def shoot(self):
     bullet = Bullet(self.rect.x, self.rect.y)
     all_sprites.add(bullet)
     bullets1.add(bullet)



all_sprites = pygame.sprite.Group()
player = Player()
player2 = Player2()
bullets = pygame.sprite.Group()
bullets1 = pygame.sprite.Group()


class Bullet(pygame.sprite.Sprite):
 def __init__(self, x, y):
     pygame.sprite.Sprite.__init__(self)
     self.image = pygame.Surface((20, 10))
     self.mask = pygame.mask.from_surface(gameDisplay)
     self.image.fill(black)
     self.rect = self.image.get_rect()
     self.rect.x = x
     self.rect.y = y
     self.x_change = -10


 def update(self):
     self.rect.x += self.x_change
     if self.rect.left > display_width:
         self.kill()
     if self.rect.right < 0:
         self.kill()

class Bullet1(pygame.sprite.Sprite):
 def __init__(self, x, y):
     pygame.sprite.Sprite.__init__(self)
     self.image = pygame.Surface((20, 10))
     self.mask = pygame.mask.from_surface(gameDisplay)
     self.image.fill(black)
     self.rect = self.image.get_rect()
     self.rect.x = x
     self.rect.y = y
     self.x_change1 = 10

 def update(self):
     self.rect.x += self.x_change1
     if self.rect.left > display_width:
         self.kill()
     if self.rect.right < 0:
         self.kill()


bullets = pygame.sprite.Group()
players1 = pygame.sprite.Group()
players2 = pygame.sprite.Group()
bullet1 = Bullet1(x, y)
players1.add(player)
players2.add(player2)
bullet = Bullet(x, y)
all_sprites.add(player)
all_sprites.add(player2)


def person(x, y):
 gameDisplay.blit(personImg, (x, y))



def person2(nx,ny):
 gameDisplay.blit(personImg2, (nx, ny))


#def boundary():
 #if x > thing_startx and x < thing_startx + thing_width or x + person_width > thing_startx and x + person_width < thing_startx + thing_width:
 #if x > display_width - person_width or x < 0:

def button(msg,x,y,w,h,ic,ac,action=None):
 mouse = pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 if x+w > mouse[0] > x and y+h > mouse[1] > y:
     pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

     if click[0] == 1 and action != None:
         action()
 else:
     pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

 smallText = pygame.font.SysFont("timesnewromanms",20)
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ( (x+(w/2)), (y+(h/2)) )
 gameDisplay.blit(textSurf, textRect)

def died():

 GameOver = True

 while GameOver:
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()

     gameDisplay.fill(white)
     redrawgame()
     largeText = pygame.font.SysFont("timesnewromanms", 115)
     TextSurf, TextRect = text_objects("Player 2 wins", largeText)
     TextRect.center = ((display_width / 2), (display_height / 2))
     gameDisplay.blit(TextSurf, TextRect)

     button("Play Again", 450, 450, 100, 50, blue, blue, game_loop)
     button("Quit", 650, 450, 100, 50, red, red, quitgame)

     pygame.display.update()
     clock.tick(15)

def died1():


 GameOver = True

 while GameOver:
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()

     gameDisplay.fill(white)
     redrawgame()
     largeText = pygame.font.SysFont("timesnewromanms", 115)
     TextSurf, TextRect = text_objects("Player 1 wins", largeText)
     TextRect.center = ((display_width / 2), (display_height / 2))
     gameDisplay.blit(TextSurf, TextRect)

     button("Play Again", 450, 450, 100, 50, blue, blue, game_loop)
     button("Quit", 650, 450, 100, 50, red, red, quitgame)

     pygame.display.update()
     clock.tick(15)



def health_bars(player_health, player2_health):

 if player_health > 75:
     player_health_color = green
 elif player_health > 50:
     player_health_color = yellow
 else:
     player_health_color = red
 if player2_health > 75:
     player2_health_color = green
 elif player2_health > 50:
     player2_health_color = yellow
 else:
     player2_health_color = red
 pygame.draw.rect(gameDisplay, player2_health_color, (1080, 25, player2_health, 25))
 pygame.draw.rect(gameDisplay, player_health_color, (20, 25, player_health, 25))


def redrawgame():
 player.rect.x = x
 player.rect.y = y
 player2.rect.x = nx
 player2.rect.y = ny

def game_intro():
 intro = True

 while intro:
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()

     gameDisplay.fill(white)
     largeText = pygame.font.SysFont("timesnewromanms", 115)
     TextSurf, TextRect = text_objects("Wert", largeText)
     TextRect.center = ((display_width / 2), (display_height / 2))
     gameDisplay.blit(TextSurf, TextRect)

     button("Play", 450, 450, 100, 50, blue, blue, game_loop)
     button("Quit", 650, 450, 100, 50, red, red, quitgame)

     pygame.display.update()
     clock.tick(15)
def quitgame():
 pygame.quit()
 quit()
def text_objects(text, font):
 textSurface = font.render(text, True, black)
 return textSurface, textSurface.get_rect()


def paused():
 largeText = pygame.font.SysFont("timesnewromanms", 115)
 TextSurf, TextRect = text_objects("Paused", largeText)
 TextRect.center = ((display_width / 2), (display_height / 2))
 gameDisplay.blit(TextSurf, TextRect)

 pause = True
 while pause:
     for event in pygame.event.get():

         if event.type == pygame.QUIT:
             pygame.quit()
             quit()



     button("Continue", 450, 450, 100, 50, blue, blue, game_loop)
     button("Quit", 650, 450, 100, 50, red, red, quitgame)

     pygame.display.update()
     clock.tick(15)





def game_loop():
 x = (0)
 y = (display_height * 0.37)
 ny = (display_height * 0.37)
 nx = (display_width * 0.87)
 y_change = 0
 x_change = 0
 x_change1 = 0
 y_change1 = 0
 bullet1 = Bullet1(x, y)
 bullet = Bullet(x, y)
 thing_startx = display_width/2
 thing_starty = display_height/2
 thing_startx1 = display_width / 2 - thing_width
 thing_starty1 = display_height / 2 - thing_height
 thing_startx2 = display_width / 2 + thing_width + 50
 thing_starty2 = display_height / 2 + 50
 thing_startx3 = display_width - 500
 thing_starty3 = display_height - 450
 thing_startx4 = display_width - 950
 thing_starty4 = display_height - 350
 thing_startx5 = display_width - 800
 thing_starty5 = display_height - 250

 gameExit = False

 player_health = 100
 player2_health = 100

 for bullet in bullets:
     bullet.kill()
 for bullet in bullets1:
     bullet.kill()

 while not gameExit:

         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 pygame.quit()
                 quit()
             if event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_SPACE:
                     player.shoot()
             if event.type == pygame.MOUSEBUTTONDOWN:
                 player2.shoot()
             if event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_p:
                     paused()



         y += y_change
         x += x_change
         ny += y_change1
         nx += x_change1



         gameDisplay.fill(white)
         block = pygame.draw.rect(gameDisplay, black, (thing_startx, thing_starty, thing_width, thing_height))
         block1 = pygame.draw.rect(gameDisplay, black, (thing_startx1, thing_starty1, thing_width, thing_height))
         block2 = pygame.draw.rect(gameDisplay, black, (thing_startx2, thing_starty2, thing_width, thing_height))
         block3 = pygame.draw.rect(gameDisplay, black, (thing_startx3, thing_starty3, thing_width, thing_height))
         block4 = pygame.draw.rect(gameDisplay, black, (thing_startx4, thing_starty4, thing_width, thing_height))
         block5 = pygame.draw.rect(gameDisplay, black, (thing_startx5, thing_starty5, thing_width, thing_height))

         block
         block1
         block2
         block3
         block4
         block5


         player
         player2


         #if x > thing_startx and x < thing_startx + thing_width or x + person_width > thing_startx and x + person_width < thing_startx + thing_width:
             #hit()

         #if thing_starty > display_height:
             #thing_starty = 0 - thing_height
             #thing_startx = random.randrange(0, display_width)

         if pygame.Rect.colliderect(block, bullet) or pygame.Rect.colliderect(block1, bullet) or pygame.Rect.colliderect(block2, bullet) or pygame.Rect.colliderect(block3, bullet) or pygame.Rect.colliderect(block4, bullet) or pygame.Rect.colliderect(block5, bullet):
             bullet.kill()
         if pygame.Rect.colliderect(block, bullet1) or pygame.Rect.colliderect(block1, bullet1) or pygame.Rect.colliderect(block2, bullet1) or pygame.Rect.colliderect(block3, bullet1) or pygame.Rect.colliderect(block4, bullet1) or pygame.Rect.colliderect(block5, bullet1):
             bullet1.kill()



         for bullet1 in bullets1:
             if pygame.sprite.collide_rect(bullet1, player):
                 bullet1.kill()
                 player_health -= 10
                 break
         if player_health == 0:
             died()

         for bullet in bullets:
             if pygame.sprite.collide_rect(bullet, player2):
                 bullet.kill()
                 player2_health -= 10
                 break
         if player2_health == 0:
             died1()



         all_sprites.update()

         health_bars(player_health, player2_health)
         all_sprites.draw(gameDisplay)
         pygame.display.flip()

         pygame.display.update()
         clock.tick(60)


game_intro()
game_loop()
pygame.quit()
quit()
`

1 个答案:

答案 0 :(得分:0)

colliderect()函数可以简化玩家之间的碰撞。

 if keystate[pygame.K_s] and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) \
         and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) \
         and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y):

可以简化为

 if keystate[pygame.K_s] and not pygame.Rect.colliderect(self.rect, player2.rect):

我将这些矩形放入列表中,然后使用colliderect()函数在每个函数中对其进行循环检查。

thing_starts.append((display_width/2, display_height/2))
thing_starts.append((display_width / 2 - thing_width, display_height / 2 - thing_height))
...
blocks = []
for thing_start in thing_starts:
    blocks.append(pygame.draw.rect(gameDisplay, black, (thing_start[0], thing_start[1], thing_width, thing_height)))
...
for block in blocks:
    if pygame.Rect.colliderect(block, bullet):
        bullet.kill()

(编辑:请注意,列表理解可以进一步简化操作,但这是高级操作。不过,如果您提出要求,我将向您展示。)