Python pygame.colliderect用于屏幕上的所有矩形
我正在制作我自己版本的bomberman,我想知道是否有办法让colliderect检查屏幕上的所有Rect,目前我只能让它一次为一个Rect工作。 这是我的代码(注意它不完整) 注意我试图让代码检查多个25x25矩形(地形方块)的碰撞,而不检查每个单独的碰撞Python pygame.colliderect用于屏幕上的所有矩形,python,pygame,Python,Pygame,我正在制作我自己版本的bomberman,我想知道是否有办法让colliderect检查屏幕上的所有Rect,目前我只能让它一次为一个Rect工作。 这是我的代码(注意它不完整) 注意我试图让代码检查多个25x25矩形(地形方块)的碰撞,而不检查每个单独的碰撞 import pygame from sys import exit import math import random import time from pygame.locals import * pygame.init() Dw
import pygame
from sys import exit
import math
import random
import time
from pygame.locals import *
pygame.init()
Dwidth = 1000
Dheight = 600
gameLoop = True
transparent = (0, 0, 0, 0)
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
sky = (213, 242, 246)
grey = (200,200,200)
dark_grey = (180,180,180)
gameDisplay = pygame.display.set_mode((Dwidth,Dheight))
pygame.display.set_caption("Bomberman")
clock = pygame.time.Clock()
clock.tick(30)
font = pygame.font.SysFont('Arial', 64)
pygame.mouse.set_visible(False)
Cursor = pygame.image.load("Bman_cursor.png").convert_alpha()
class terrain():
def __init__(self, x, y, image, Type):
self.x = x
self.y = y
self.image = image
self.width = 25
self.height = 25
self.Type = Type
self.collision = pygame.Rect(self.x,self.y, 24,24)
def gety(self):
return self.y
def getx(self):
return self.x
def getype(self):
return self.Type
def draw(self, gameDisplay):
gameDisplay.blit(self.image, (self.x,self.y))
def newCoords(self, X, Y):
self.x = X
self.y = Y
class Player():
def __init__(self, PlayerNO, Class, x, y):
self.playerNO = PlayerNO
self.Class = Class
self.hp = 1
self.x = x
self.y = y
self.speed = 0.4
self.bombNum = 1
self.power = 1
if self.Class == "Bomber":
self.bombNum += 1
self.ability = "bomb rush"
self.image = pygame.image.load("Bman\white\Bman_idle.png")
elif self.Class == "Chronomancer":
self.ability = "time stop"
elif self.Class == "Miner":
self.power += 2
self.ability = "land mine"
self.image = pygame.image.load("Bman\Black\Bman_black_idle.png")
elif self.Class == "Racer":
self.speed += 0.4 #0.2*2
self.ability = "sprint"
elif self.Class == "Strategist":
pass
#set bomb to time bomb
else:
print("CLASS ERROR")
self.collision = pygame.Rect(self.x,self.y, 16,22)
def getNO(self):
return self.playerNO
def getClass(self):
return self.Class
def gethp(self):
return self.hp
def getx(self):
return self.x
def gety(self):
return self.y
def getspeed(self):
return self.speed
def getbombNum(self):
return self.bombNum
def getpower(self):
return self.power
def move(self, direction):
#Poverlap = self.collision.colliderect()#this is where i hope to check for collisions with all Rect's
Poverlap = 0
#print(Poverlap)
if Poverlap == 1:
pass
elif Poverlap == 0:
if direction == "left":
if self.x > 275+self.speed:
self.x -= self.speed
elif direction == "up":
if self.y > 77.5+self.speed:
self.y -= self.speed
elif direction == "right":
if self.x < 712.5-self.speed:
self.x += self.speed
elif direction == "down":
if self.y < 505.5-self.speed:
self.y += self.speed
else:
print(Poverlap)
print(Toverlap)
print("overlap error")
def newImage(self, image):
self.image = image
def draw(self):
gameDisplay.blit(self.image, (self.x,self.y))
class bomb():
pass
def createMap(grid, Bmap):
GWstart = 250*(Dwidth/1000)
GWend = 750*(Dwidth/1000)
GHstart = 52.5*(Dheight/600)
GHend = 552.5*(Dheight/600)
GWpos = 25*(Dwidth/1000)
GHpos = 25*(Dheight/600)
if Bmap == 1:
for x in range(3,7):
grid[x][x] = 1
y = 0
for x in range(3,7):
grid[16-y][x] = 1
y += 1
y = 0
for x in range(3,7):
grid[x][16-y] = 1
y += 1
y = 0
for x in range(1,5):
grid[16-y][16-y] = 1
y += 1
for x in range(3,6):
grid[9][x] = 2
grid[10][x] = 2
grid[9][x+11] = 2
grid[10][x+11] = 2
grid[x][9] = 2
grid[x][10] = 2
grid[x+11][9] = 2
grid[x+11][10] = 2
for x in range (0,2):
grid[7+x][6] = 2
grid[9+x][6] = 1
grid[11+x][6] = 2
grid[9+x][7] = 1
grid[12+x][7] = 2
grid[9+x][8] = 2
grid[13][8] = 2
grid[6+x][9] = 1
grid[8][9] = 2
grid[12+x][9] = 1
grid[11][9+x] = 2
grid[12+x][10] = 1
grid[7+x][13] = 2
grid[9][13-x] = 1
grid[13][11+x] = 2
grid[9+x][11] = 2
grid[10][13-x] = 1
grid[11+x][13] = 2
grid[6][7+x] = 2
grid[6+x][9] = 1
grid[8][9+x] = 2
grid[6+x][10] = 1
grid[6][11+x] = 2
grid[7][7+5*x] = 2
grid[9+x][9+x] = 2
grid[9+x][10-x] = 2
grid[2+10*x][2+10*x] = 2
grid[17-15*x][2+15*x] = 2
grid[8+3*x][16] = 1
grid[16][8+3*x] = 1
grid[3+5*x][8-5*x] = 1
grid[11-8*x][3+8*x] = 1
for x in range(0,3):
grid[7-x][5-x] = 2
grid[5-x][7-x] = 2
for x in range(3,8):
grid[x][2] = 1
grid[2][x] = 1
grid[x+9][2] = 1
grid[2][x+9] = 1
grid[17][x] = 1
grid[x][17] = 1
grid[17][x+9] = 1
grid[x+9][17] = 1
for x in range(0,3):
grid[4+x][3+x] = 2
grid[3+x][4+x] = 2
grid[16-x][4+x] = 2
grid[15-x][3+x] = 2
grid[14-x][3+x] = 2
grid[16-x][5+x] = 2
grid[3+x][15-x] = 2
grid[3+x][14-x] = 2
grid[4+x][16-x] = 2
grid[5+x][16-x] = 2
grid[16-x][15-x] = 2
grid[16-x][14-x] = 2
grid[15-x][16-x] = 2
grid[14-x][16-x] = 2
grid[17][17] = 2
elif Bmap == 2:
for y in range(2,18,+3):
for x in range(2,18,+3):
grid[x][y] = 1
elif Bmap == 3:
for x in range (2,18):
grid[2][x] = 1
grid[x][2] = 1
for x in range(4,18):
grid[17][x] = 1
grid[x][17] = 1
if x < 17:
grid[x][16] = 2
if x < 16:
grid[4][x] = 1
grid[15][x] = 1
grid[3][x] = 2
grid[x][3] = 2
grid[16][x] = 2
for x in range(6,14):
grid[6][x] = 1
grid[8][x] = 1
grid[9][x] = 2
grid[10][x] = 2
grid[11][x] = 1
grid[13][x] = 1
for x in range(5,14):
grid[5][x] = 2
grid[x][5] = 2
grid[14][x] = 2
grid[x][14] = 2
for x in range(0,2):
grid[17-14*x][3+14*x] = 2
grid[16-14*x][2+14*x] = 2
grid[3-x][2+x] = 2
grid[16+x][17-x] = 2
grid[16][16] = 0
grid[14][14] = 2
elif Bmap == 4:
pass
return grid
def CSelect(playerNum, backdrop):
global Dwidth
global Dheight
global Cursor
Bbomber = pygame.image.load("button_bomber.png")
Bchrono = pygame.image.load("button_chronomancer.png")
Bminer = pygame.image.load("button_miner.png")
BRacer = pygame.image.load("button_racer.png")
Bstrat = pygame.image.load("button_strategist.png")
while True:
gameDisplay.blit(backdrop, [0,0])
gameDisplay.blit(Bbomber, [24, 230])
gameDisplay.blit(Bchrono, [195, 230])
gameDisplay.blit(Bminer, [435, 230])
gameDisplay.blit(BRacer, [605, 230])
gameDisplay.blit(Bstrat, [775, 230])
gameDisplay.blit(Cursor, ( pygame.mouse.get_pos() ) )
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
startButton = pygame.image.load("button_start.png")
Map_select(startButton)
elif event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.pos[0] >= 24*(Dwidth/1000) and event.pos[0] <= 180*(Dwidth/1000) and event.pos[1] >= 230*(Dheight/600) and event.pos[1] <= 300*(Dheight/600):
player1 = Player(playerNum, "Bomber", 300, 77.5)
return player1
if event.pos[0] >= 195*(Dwidth/1000) and event.pos[0] <= 420*(Dwidth/1000) and event.pos[1] >= 230*(Dheight/600) and event.pos[1] <= 300*(Dheight/600):
player1 = Player(playerNum, "Chronomancer", 300, 77.5)
return player1
if event.pos[0] >= 435*(Dwidth/1000) and event.pos[0] <= 580*(Dwidth/1000) and event.pos[1] >= 230*(Dheight/600) and event.pos[1] <= 300*(Dheight/600):
player1 = Player(playerNum, "Miner", 300, 77.5)
return player1
if event.pos[0] >= 605*(Dwidth/1000) and event.pos[0] <= 760*(Dwidth/1000) and event.pos[1] >= 230*(Dheight/600) and event.pos[1] <= 300*(Dheight/600):
player1 = Player(playerNum, "Racer", 300, 77.5)
return player1
if event.pos[0] >= 775*(Dwidth/1000) and event.pos[0] <= 970*(Dwidth/1000) and event.pos[1] >= 230*(Dheight/600) and event.pos[1] <= 300*(Dheight/600):
player1 = Player(playerNum, "Strategist", 300, 77.5)
return player1
pygame.display.update()
def preGame(backdrop):
global Dwidth
global Dheight
global Cursor
singleButton = pygame.image.load("button_single-player.png")
multiButton = pygame.image.load("button_multiplayer.png")
while True:
gameDisplay.blit(backdrop, [0,0])
gameDisplay.blit(singleButton, [190, 280])
gameDisplay.blit(multiButton, [555, 280])
gameDisplay.blit(Cursor, ( pygame.mouse.get_pos() ) )
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return 3
elif event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.pos[0] >= 190*(Dwidth/1000) and event.pos[0] <= 445*(Dwidth/1000) and event.pos[1] >= 280*(Dheight/600) and event.pos[1] <= 350*(Dheight/600):
playerNum = 1
return playerNum
elif event.pos[0] >= 555*(Dwidth/1000) and event.pos[0] <= 710*(Dwidth/1000) and event.pos[1] >= 280*(Dheight/600) and event.pos[1] <= 350*(Dheight/600):
playerNum = 2
return playerNum
pygame.display.update()
def inGame(backdrop, Bmap):
global Dwidth
global Dheight
global cursor
playerNum = preGame(backdrop)
if playerNum == 3:
return
if playerNum == 1:
player1 = CSelect(1, backdrop)
elif playerNum == 2:
player1 = CSelect(1, backdrop)
player2 = CSelect(2, backdrop)
grid = []
for row in range(20):
grid.append([])
for column in range(20):
grid[row].append(0) #gives me a 20x20 grid for the game processes
for x in range (20):
grid[x][0] = 1
grid[0][x] = 1
grid[19][x] = 1
grid[x][19] = 1 #1 = hard terrain, 2 = soft terrain and 0 = no terrain
gameDisplay.blit(backdrop, [0,0])
Lterrain = pygame.image.load("soft_terrain.png")
Hterrain = pygame.image.load("hard_terrain.png")
GWstart = 250*(Dwidth/1000)
GWend = 750*(Dwidth/1000)
GHstart = 52.5*(Dheight/600)
GHend = 552.5*(Dheight/600)
GWpos = 25*(Dwidth/1000)
GHpos = 25*(Dheight/600)
Lgrass = (17, 191, 60)
Dgrass = (2, 162, 36)
DVrock = (130,0,0)
LVrock = (180,30,30)
Lrock = (159,105,52)
Drock = (116,77,38)
Lsky = (240,250,255)
Dsky = (178,232,255)
dark_light = 0
grid = createMap(grid, Bmap)
if Bmap == 1:
Lcolour = LVrock
Dcolour = DVrock
base = pygame.image.load("Volcano_base.png")
elif Bmap == 2:
Lcolour = Lgrass
Dcolour = Dgrass
base = pygame.image.load("Forest_base.png")
elif Bmap == 3:
Lcolour = Lrock
Dcolour = Drock
base = pygame.image.load("Cave_base.png")
elif Bmap == 4:
Lcolour = Lsky
Dcolour = Dsky
base = pygame.image.load("Sky_base.png")
gameDisplay.blit(backdrop, [0,0])
Lcounter = 0
Ucounter = 0
Rcounter = 0
Dcounter = 0
transparent = (0, 0, 0, 0)
gameDisplay.blit(base, [GWstart, GHstart])
HRterrain = terrain(GWstart+GWpos-25,GHstart + GHpos -25, Hterrain, 1)
LIterrain = terrain(GWstart+GWpos-25,GHstart + GHpos -25, Lterrain, 2)
print(player1.getx())
print(player1.gety())
while True:
gameDisplay.blit(base, [GWstart, GHstart])
for x in range(len(grid)):
for y in range(len(grid[0])):
if grid[x][y] == 1:
HRterrain.newCoords(GWstart+GWpos-25, GHstart + GHpos -25)
pygame.Rect(GWstart+GWpos-25, GHstart+GHpos-25, 25,25)
HRterrain.draw(gameDisplay)
elif grid[x][y] == 2:
LIterrain.newCoords(GWstart+GWpos-25, GHstart + GHpos -25)
pygame.Rect(GWstart+GWpos-25, GHstart+GHpos-25, 25,25)
LIterrain.draw(gameDisplay)
GWpos += 25
GWpos = 25
GHpos += 25
GHpos = 25
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return
elif event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
#moves 11.999999999999318 pixels per 30 counter
key = pygame.key.get_pressed()
if key[K_a]:
Lcounter += 1
player1.move("left")
if Lcounter < 30:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_left1.png"))
elif Lcounter == 30:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_left2.png"))
elif Lcounter == 60:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_left1.png"))
elif Lcounter == 90:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_left3.png"))
elif Lcounter == 120:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_left1.png"))
Lcounter = 0
elif key[K_w]:
Ucounter += 1
player1.move("up")
if Ucounter < 30:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_up1.png"))
elif Ucounter == 30:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_up2.png"))
elif Ucounter == 60:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_up1.png"))
elif Ucounter == 90:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_up3.png"))
elif Ucounter == 120:
if player1.getClass() == "Miner":
player1.newImage(pygame.image.load("Bman\Black\Bman_black_up1.png"))
Ucounter = 0
elif key[K_d]:
player1.move("right")
Rcounter += 1
if Rcounter < 30:
if player1.getClass() == "Miner":
player1.newImage(pygame.transform.flip(pygame.image.load("Bman\Black\Bman_black_left1.png"), True, False))
elif Rcounter == 30:
if player1.getClass() == "Miner":
player1.newImage(pygame.transform.flip(pygame.image.load("Bman\Black\Bman_black_left2.png"), True, False))
elif Rcounter == 60:
if player1.getClass() == "Miner":
player1.newImage(pygame.transform.flip(pygame.image.load("Bman\Black\Bman_black_left1.png"), True, False))
elif Rcounter == 90:
if player1.getClass() == "Miner":
player1.newImage(pygame.transform.flip(pygame.image.load("Bman\Black\Bman_black_left3.png"), True, False))
elif Rcounter == 120:
if player1.getClass() == "Miner":
player1.newImage(pygame.transform.flip(pygame.image.load("Bman\Black\Bman_black_left1.png"), True, False))
Rcounter = 0
elif key[K_s]:
player1.move("down")
Dcounter += 1
if Dcounter < 30:
player1.newImage(pygame.image.load("Bman\Black\Bman_black_idle.png"))
elif Dcounter == 30:
player1.newImage(pygame.image.load("Bman\Black\Bman_black_forward_redo.png"))
elif Dcounter == 60:
player1.newImage(pygame.image.load("Bman\Black\Bman_black_idle.png"))
elif Dcounter == 90:
player1.newImage(pygame.image.load("Bman\Black\Bman_black_forward2.png"))
elif Dcounter == 120:
player1.newImage(pygame.image.load("Bman\Black\Bman_black_idle.png"))
Dcounter = 0
player1.draw()
pygame.display.update()
def Map_select(startButton):
global Dwidth
global Dheight
backButton = pygame.image.load("button_back.png")
Volcano_zone = pygame.image.load("Volcano zone.png")
Volcano_zone = pygame.transform.smoothscale(Volcano_zone, (Dwidth, Dheight))
Forest_zone = pygame.image.load("Forest zone.jpg")
Forest_zone = pygame.transform.smoothscale(Forest_zone, (Dwidth, Dheight))
Cave_zone = pygame.image.load("Cave_zone.jpg")
Cave_zone = pygame.transform.smoothscale(Cave_zone, (Dwidth, Dheight))
Sky_zone = pygame.image.load("Sky_zone.png")
Sky_zone = pygame.transform.smoothscale(Sky_zone, (Dwidth, Dheight))
Right_button = pygame.image.load("Right_arrow.png")
Left_button = pygame.image.load("Left_arrow.png")
Bmap = 1
while True:
if Bmap == 1:
gameDisplay.blit(Volcano_zone, [0,0])
elif Bmap == 2:
gameDisplay.blit(Forest_zone, [0,0])
elif Bmap == 3:
gameDisplay.blit(Cave_zone, [0,0])
elif Bmap == 4:
gameDisplay.blit(Sky_zone, [0,0])
elif Bmap == 5:
Bmap = 1
gameDisplay.blit(Volcano_zone, [0,0])
elif Bmap == 0:
Bmap = 4
gameDisplay.blit(Sky_zone, [0,0])
gameDisplay.blit(Left_button, [50*(Dwidth/1000), 500*(Dheight/600)])
gameDisplay.blit(Right_button, [810*(Dwidth/1000), 500*(Dheight/600)])
gameDisplay.blit(backButton, [439.5*(Dwidth/1000), 510*(Dheight/600)])
gameDisplay.blit(startButton, (100*(Dwidth/1000), 75*(Dheight/600)))
gameDisplay.blit( Cursor, ( pygame.mouse.get_pos() ) )
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
main_menu()
elif event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.pos[0] >= 50*(Dwidth/1000) and event.pos[0] <= 240*(Dwidth/1000) and event.pos[1] >= 500*(Dheight/600) and event.pos[1] <= 570*(Dheight/600):
Bmap -= 1
print("-1")
elif event.pos[0] >= 810*(Dwidth/1000) and event.pos[0] <= 950*(Dwidth/1000) and event.pos[1] >= 500*(Dheight/600) and event.pos[1] <= 570*(Dheight/600):
Bmap += 1
print("+1")
elif event.pos[0] >= 439.5*(Dwidth/1000) and event.pos[0] <= 560.5*(Dwidth/1000) and event.pos[1] >= 500*(Dheight/600) and event.pos[1] <= 550*(Dheight/600):
main_menu()
elif event.pos[0] >= 100*(Dwidth/1000) and event.pos[0] <= 265*(Dwidth/1000) and event.pos[1] >= 75*(Dheight/600) and event.pos[1] <= 155*(Dheight/600):
if Bmap == 1:
inGame(Volcano_zone,Bmap)
elif Bmap == 2:
inGame(Forest_zone,Bmap)
elif Bmap == 3:
inGame(Cave_zone,Bmap)
elif Bmap == 4:
inGame(Sky_zone,Bmap)
else:
print("error")
def settings_menu(Fwidth, Fheight, background_image):
global Dwidth
global Dheight
global gameDisplay
gameDisplay.blit(background_image, [0,0])
Settloop = True
resButton = pygame.image.load("button_resolution.png")
gameDisplay.blit(resButton, [100,275])
backButton = pygame.image.load("button_back.png")
gameDisplay.blit(backButton, [100, 475])
cntrlsButton = pygame.image.load("button_controls.png")
gameDisplay.blit(cntrlsButton, [365, 275])
while Settloop is True:
clock.tick(60)
background_image = pygame.transform.smoothscale(background_image, (Dwidth, Dheight))
gameDisplay.blit(background_image, [0,0])
gameDisplay.blit(resButton, [100*(Dwidth/1000),275*(Dheight/600)])
gameDisplay.blit(cntrlsButton, [375*(Dwidth/1000), 275*(Dheight/600)])
gameDisplay.blit(backButton, [100*(Dwidth/1000), 475*(Dheight/600)])
gameDisplay.blit(Cursor, ( pygame.mouse.get_pos() ) )
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.pos[0] >= 100*(Dwidth/1000) and event.pos[0] <= 345*(Dwidth/1000) and event.pos[1] >= 275*(Dheight/600) and event.pos[1] <= 355*(Dheight/600):
print("Current resolution:", Dwidth, "x", Dheight, "\nEnter a new resolution or enter 0 to pass\n*NOTE*: some resolutions may make the game unplayable as the game is built around the default resolution\nincreasing the resolution will also in turn decrease the framerate")
newWidth = int(input("New width: "))
newHeight = int(input("New height: "))
if newHeight <= 0 or newWidth <= 0:
pass
else:
#print("hit escape to apply changes")
Dwidth = newWidth
Dheight = newHeight
gameDisplay = pygame.display.set_mode((Dwidth,Dheight)) #changes the resolution of the game window
elif event.pos[0] >= 100*(Dwidth/1000) and event.pos[0] <= 221*(Dwidth/1000) and event.pos[1] >= 475*(Dheight/600) and event.pos[1] <= 525*(Dheight/600):
return
elif event.pos[0] >= 365*(Dwidth/1000) and event.pos[0] <= 575*(Dwidth/1000) and event.pos[1] >= 275*(Dheight/600) and event.pos[1] <= 355*(Dheight/600):
print("\nP1:\n Movement: WASD\n Place Bomb: Y\n Usable powerup(e.g time bomb): U\n Use Class ability: I") #prints controls into console
print("\nP2:\n Movement: Arrow keys\n Place bomb: Numberpad 1(N1)\n Usable powerup(e.g time bomb): N2\n Use Class ability: N3")
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return #returns to main menu
elif event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
pygame.display.flip()
def main_menu():
background_image = pygame.image.load("Title_Background.jpg")
background_image = pygame.transform.smoothscale(background_image, (Dwidth, Dheight))
gameDisplay.blit(background_image, [0,0])
startButton = pygame.image.load("button_start.png")
gameDisplay.blit(startButton, [100,275])
settingsButton = pygame.image.load("button_settings.png")
gameDisplay.blit(settingsButton, [100,375])
#tester = pygame.image.load("Bman/Black/Bman__test.png")
clock.tick(60)
while gameLoop is True:
clock.tick(60)
gameDisplay.blit(background_image, [0,0])
gameDisplay.blit(settingsButton, [100*(Dwidth/1000),375*(Dheight/600)])
gameDisplay.blit(startButton, [100*(Dwidth/1000),275*(Dheight/600)])
gameDisplay.blit( Cursor, ( pygame.mouse.get_pos() ) )
#gameDisplay.blit(tester, [10,10])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.pos[0] >= 100*(Dwidth/1000) and event.pos[0] <= 265*(Dwidth/1000) and event.pos[1] >= 275*(Dheight/600) and event.pos[1] <= 355*(Dheight/600):
Map_select(startButton)
elif event.pos[0] >= 100*(Dwidth/1000) and event.pos[0] <= 300*(Dwidth/1000) and event.pos[1] >= 375*(Dheight/600) and event.pos[1] <= 455*(Dheight/600):#the *(Dheight/600) and *(Dwidth/1000) are to make sure the buttons scale with resolution when it changes
settings_menu(Dwidth, Dheight, background_image)
# print(event.pos[0])#event.pos[0] = x-coords
# print(event.pos[1])#event.pos[1] = y-coords
# print(event.pos)#event.pos = (x-coords, y-coords) i use these to check coords when setting up buttons
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.display.quit()
pygame.quit()
exit()#exits the game
elif event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
pygame.display.flip()
main_menu()
导入pygame
从系统导入退出
输入数学
随机输入
导入时间
从pygame.locals导入*
pygame.init()
Dwidth=1000
Dheight=600
gameLoop=True
透明=(0,0,0,0)
黑色=(0,0,0)
白色=(255255)
红色=(255,0,0)
绿色=(0255,0)
蓝色=(0,0255)
天空=(213242246)
灰色=(200200)
深灰色=(180180)
gameDisplay=pygame.display.set_模式((Dwidth,Dheight))
pygame.display.set_标题(“炸弹人”)
clock=pygame.time.clock()
时钟滴答(30)
font=pygame.font.SysFont('Arial',64)
pygame.mouse.set_可见(False)
Cursor=pygame.image.load(“Bman\u Cursor.png”).convert\u alpha()
类地形()
定义初始化(self,x,y,image,Type):
self.x=x
self.y=y
self.image=image
自宽=25
自我高度=25
self.Type=Type
self.collision=pygame.Rect(self.x,self.y,24,24)
def gety(自我):
回归自我
def getx(自我):
返回self.x
def getype(self):
返回自我类型
def绘制(自绘制、游戏显示):
blit(self.image,(self.x,self.y))
def newCoords(self,X,Y):
self.x=x
self.y=y
类播放器():
定义初始化(self,PlayerNO,Class,x,y):
self.playerNO=playerNO
self.Class=Class
self.hp=1
self.x=x
self.y=y
自身速度=0.4
self.bombNum=1
自功率=1
如果self.Class==“轰炸机”:
self.bombNum+=1
self.ability=“炸弹狂潮”
self.image=pygame.image.load(“Bman\white\Bman\u idle.png”)
elif self.Class==“计时癌症”:
self.ability=“时间停止”
elif self.Class==“矿工”:
自功率+=2
self.ability=“地雷”
self.image=pygame.image.load(“Bman\Black\Bman\u Black\u idle.png”)
elif self.Class==“Racer”:
自身速度+=0.4#0.2*2
self.ability=“sprint”
elif self.Class==“战略家”:
通过
#定时炸弹
其他:
打印(“类错误”)
self.collision=pygame.Rect(self.x,self.y,16,22)
def getNO(自我):
返回self.playerNO
def getClass(自):
返回自我类
def gethp(自我):
return self.hp
def getx(自我):
返回self.x
def gety(自我):
回归自我
def getspeed(自):
回程速度
def getbombNum(自):
返回self.bombNum
def getpower(自):
回归自我力量
def移动(自身、方向):
#Poverlap=self.collision.collide Rect()#我希望在这里检查与所有Rect的冲突
波弗拉普=0
#印刷品(波弗拉普)
如果Poverlap==1:
通过
elif Poverlap==0:
如果方向==“左”:
如果self.x>275+自速度:
self.x-=自速度
elif方向==“向上”:
如果self.y>77.5+self.speed:
self.y-=自速度
elif方向==“右”:
如果self.x<712.5-self.speed:
self.x+=自速度
elif方向==“向下”:
如果自y<505.5-自速度:
self.y+=自速度
其他:
印刷品(波弗拉普)
打印(Toverlap)
打印(“重叠错误”)
def新图像(自我,图像):
self.image=image
def牵引(自):
blit(self.image,(self.x,self.y))
类炸弹():
通过
def createMap(网格,Bmap):
GWstart=250*(Dwidth/1000)
GWend=750*(Dwidth/1000)
GHstart=52.5*(Dheight/600)
根德=552.5*(Dheight/600)
GWpos=25*(Dwidth/1000)
GHpos=25*(Dheight/600)
如果Bmap==1:
对于范围(3,7)内的x:
网格[x][x]=1
y=0
对于范围(3,7)内的x:
网格[16-y][x]=1
y+=1
y=0
对于范围(3,7)内的x:
网格[x][16-y]=1
y+=1
y=0
对于范围(1,5)内的x:
网格[16-y][16-y]=1
y+=1
对于范围(3,6)内的x:
网格[9][x]=2
网格[10][x]=2
网格[9][x+11]=2
网格[10][x+11]=2
网格[x][9]=2
网格[x][10]=2
网格[x+11][9]=2
网格[x+11][10]=2
对于范围(0,2)内的x:
网格[7+x][6]=2
网格[9+x][6]=1
网格[11+x][6]=2
网格[9+x][7]=1
网格[12+x][7]=2
网格[9+x][8]=2
网格[13][8]=2
网格[6+x][9]=1
网格[8][9]=2
网格[12+x][9]=1
网格[11][9+x]=2
网格[12+x][10]=1
网格[7+x][13]=2
网格[9][13-x]=1
网格[13][11+x]=2
网格[9+x][11]=2
网格[10][13-x]=1
网格[11+x][13]=2
网格[6][7+x]=2
网格[6+x][9]=1
网格[8][9+x]=2
网格[6+x][10]=1
网格[6][11+x]=2
网格[7][7+5*x]=2
网格[9+x][9+x]=2
网格[9+x][10-x]=2
网格[2+10*x][2+10*x]=2
网格[17-15*x][2+15*x]=2
网格[8+3*x][16]=1
网格[16][8+3*x]=1
网格[3+5*x][8-5*x]=1
网格[11-8*x][3+8*x]=1
对于范围(0,3)内的x:
网格[7-x][5-x]=2
网格[5-x][7-x]=2
对于范围(3,8)内的x:
网格[x][2]=1
网格[2][x]=1
网格[x+9][2]=1
网格[2][x+9]=1
网格[17][x]=1
网格[x][17]=1
网格[17][x+9]=1
网格[x+9][17]=1
对于范围(0,3)内的x:
网格[4+x][3+x]=2
网格[3+x][4+x]=2
网格[16-x][4+x]=2
网格[15-x][3+x]=2
网格[14-x][3+x]=2
网格[16-x][5+x]=2
网格[3+x][15-x]=2
网格[3+x][14-x]=2
网格[4+x][16-x]=2
网格[5+x][16-x]=2
网格[16-x][15-x]=2
网格[16-x][14-x]=2
网格[15-x][16-x]=2
网格[14-x][16-x]=2
网格[17][17]=2
elif Bmap==2:
对于范围(2,18,+3)内的y:
对于范围(2,18,+3)内的x:
网格[x][y]=1
elif Bmap==3:
对于范围(2,18)内的x:
网格[2][x]=