Python 如何感知墙和玩家之间的碰撞:Pygame
我有一个代码,我需要感知玩家和墙之间的碰撞。我看了其他答案和线索,但无法理解: 代码如下:Python 如何感知墙和玩家之间的碰撞:Pygame,python,pygame,physics,maze,Python,Pygame,Physics,Maze,我有一个代码,我需要感知玩家和墙之间的碰撞。我看了其他答案和线索,但无法理解: 代码如下: import pygame BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) PURPLE = (255, 0, 255) class Wall(pygame.sprite.Sprite):
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(pygame.sprite.Sprite):
speedx = 0
speedy = 0
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([15, 15])
self.image.fill(PURPLE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def changespeed(self, x, y):
self.speedx += x
self.speedy += y
def move(self, walls):
self.rect.x += self.speedx
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
if self.speedx > 0:
self.rect.right = block.rect.left
else:
self.rect.left = block.rect.right
self.rect.y += self.speedy
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
if self.speedy > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
play_col = (self.rect.left, self.rect.right, 10, 10)
wall = (10, 200, 10, 10)
if ship1.colliderect(ship2):
print "ajasj"
class StartScreen():
def __init__(self, background, startButton, sbx, sby, sbw, sbh):
self.background = background
self.startButton = startButton
self.sbx = sbx
self.sby = sby
self.sbw = sbw
self.sbh = sbh
self.active = True
self.b = None
def drawScreen(self, screen):
print "a"
print self.background
screen.blit(self.background, (0, 0))
self.b = screen.blit(self.startButton, (self.sbx, self.sby))
def onClick(self, event):
pos = pygame.mouse.get_pos()
if self.b.collidepoint(pos):
self.active = False
class EndScreen():
def __init__(self, background):
self.background = background
self.active = True
def drawBG(self, screen):
screen.blit(self.background, (0,0))
class Room(object):
wall_list = None
enemy_sprites = None
def __init__(self):
self.wall_list = pygame.sprite.Group()
self.enemy_sprites = pygame.sprite.Group()
class Room1(Room):
def __init__(self):
Room.__init__(self)
walls = [[0, 0, 20, 250, PURPLE],
[0, 350, 20, 250, PURPLE],
[780, 0, 20, 250, PURPLE],
[780, 350, 20, 250, PURPLE],
[20, 0, 760, 20, PURPLE],
[20, 580, 760, 20, PURPLE],
[190, 50, 20, 500, PURPLE],
[590, 50, 20, 500, PURPLE]
]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
class Room2(Room):
def __init__(self):
Room.__init__(self)
walls = [[0, 0, 20, 250, PURPLE],
[0, 350, 20, 250, PURPLE],
[780, 0, 20, 250, PURPLE],
[780, 350, 20, 250, PURPLE],
[20, 0, 760, 20, PURPLE],
[20, 580, 760, 20, PURPLE]
]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
for x in range(100, 800, 100):
wall = Wall(x, 100, 20, 400, PURPLE)
self.wall_list.add(wall)
class Room3(Room):
def __init__(self):
Room.__init__(self)
walls = [[0, 0, 20, 250, PURPLE],
[0, 350, 20, 250, PURPLE],
[780, 0, 20, 250, PURPLE],
[780, 350, 20, 250, PURPLE],
[20, 0, 760, 20, PURPLE],
[20, 580, 760, 20, PURPLE]
]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
for x in range(100, 800, 100):
for y in range(50, 451, 300):
wall = Wall(x, y, 20, 200, PURPLE)
self.wall_list.add(wall)
for x in range(150, 700, 100):
wall = Wall(x, 200, 20, 200, PURPLE)
self.wall_list.add(wall)
def main():
pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption("Wonder Maze")
player = Player(50, 50)
movingsprites = pygame.sprite.Group()
movingsprites.add(player)
rooms = []
room = Room1()
rooms.append(room)
room = Room2()
rooms.append(room)
room = Room3()
rooms.append(room)
screen.blit(pygame.image.load("bg.png"), (0,0))
startScreen = StartScreen(pygame.image.load("bg.png"), pygame.image.load("startbutton.png"), 240, 190, 320, 60)
current_room_no = 0
current_room = rooms[current_room_no]
clock = pygame.time.Clock()
ingame = True
while ingame:
while startScreen.active:
for event in pygame.event.get():
if event.type == pygame.QUIT:
startScreen.active = False
ingame = False
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
startScreen.onClick(event)
startScreen.drawScreen(screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-5, 0)
if event.key == pygame.K_RIGHT:
player.changespeed(5, 0)
if event.key == pygame.K_UP:
player.changespeed(0, -5)
if event.key == pygame.K_DOWN:
player.changespeed(0, 5)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(5, 0)
if event.key == pygame.K_RIGHT:
player.changespeed(-5, 0)
if event.key == pygame.K_UP:
player.changespeed(0, 5)
if event.key == pygame.K_DOWN:
player.changespeed(0, -5)
player.move(current_room.wall_list)
if player.rect.x < -15:
if current_room_no == 0:
current_room_no = 2
current_room = rooms[current_room_no]
player.rect.x = 790
elif current_room_no == 2:
current_room_no = 1
current_room = rooms[current_room_no]
player.rect.x = 790
else:
current_room_no = 0
current_room = rooms[current_room_no]
player.rect.x = 790
if player.rect.x > 801:
if current_room_no == 0:
current_room_no = 1
current_room = rooms[current_room_no]
player.rect.x = 0
elif current_room_no == 1:
current_room_no = 2
current_room = rooms[current_room_no]
player.rect.x = 0
else:
ingame = False
print current_room, current_room_no
# --- Drawing ---
screen.fill(WHITE)
movingsprites.draw(screen)
current_room.wall_list.draw(screen)
pygame.display.flip()
clock.tick(60)
endScreen = EndScreen(pygame.image.load("wp.png"))
while endScreen.active:
endScreen.drawBG(screen)
pygame.display.flip()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
endScreen.active = False
pygame.quit()
pygame.quit()
if __name__ == "__main__":
main()
导入pygame
黑色=(0,0,0)
白色=(255,255,255)
蓝色=(0,0255)
绿色=(0,255,0)
红色=(255,0,0)
紫色=(255,0255)
类墙(pygame.sprite.sprite):
定义初始值(自、x、y、宽度、高度、颜色):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface([宽度,高度])
self.image.fill(颜色)
self.rect=self.image.get_rect()
self.rect.x=x
自校正y=y
职业玩家(pygame.sprite.sprite):
speedx=0
速度=0
定义初始化(self,x,y):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface([15,15])
self.image.fill(紫色)
self.rect=self.image.get_rect()
self.rect.x=x
自校正y=y
def更换速度(自身、x、y):
self.speedx+=x
自加速+=y
def移动(自身、墙壁):
self.rect.x+=self.speedx
block\u hit\u list=pygame.sprite.spritecollide(self、walls、False)
对于block_命中列表中的block_:
如果self.speedx>0:
self.rect.right=block.rect.left
其他:
self.rect.left=block.rect.right
self.rect.y+=自加速
block\u hit\u list=pygame.sprite.spritecollide(self、walls、False)
对于block_命中列表中的block_:
如果self.speeded>0:
self.rect.bottom=block.rect.top
其他:
self.rect.top=block.rect.bottom
play_col=(self.rect.left,self.rect.right,10,10)
墙=(10200,10,10)
如果ship1.CollizeRect(ship2):
打印“ajasj”
类StartScreen():
定义初始(自身、背景、开始按钮、sbx、sby、sbw、sbh):
self.background=背景
self.startButton=startButton
self.sbx=sbx
self.sby=sby
self.sbw=sbw
self.sbh=sbh
self.active=True
self.b=无
def drawScreen(自身,屏幕):
打印“a”
打印自己的背景
屏幕blit(self.background,(0,0))
self.b=screen.blit(self.startButton,(self.sbx,self.sby))
def onClick(自我,事件):
pos=pygame.mouse.get_pos()
如果自平衡点(位置):
self.active=False
类EndScreen():
定义初始(自我,背景):
self.background=背景
self.active=True
def drawBG(自身,屏幕):
屏幕blit(self.background,(0,0))
教室(实物):
墙列表=无
敌人精灵=无
定义初始化(自):
self.wall\u list=pygame.sprite.Group()
self.敌方精灵=pygame.sprite.Group()
教室1(房间):
定义初始化(自):
房间。uuu初始(自我)
墙=[[0,0,20250,紫色],
[035020250,紫色],
[780,0,20250,紫色],
[780350,20250,紫色],
[20,0760,20,紫色],
[2058076020,紫色],
[190,50,20500,紫色],
[590,50,20500,紫色]
]
对于墙中的项目:
墙=墙(项目[0],项目[1],项目[2],项目[3],项目[4])
self.wall\u list.add(墙)
二级教室(房间):
定义初始化(自):
房间。uuu初始(自我)
墙=[[0,0,20250,紫色],
[035020250,紫色],
[780,0,20250,紫色],
[780350,20250,紫色],
[20,0760,20,紫色],
[2058076020,紫色]
]
对于墙中的项目:
墙=墙(项目[0],项目[1],项目[2],项目[3],项目[4])
self.wall\u list.add(墙)
对于范围(100800100)内的x:
墙=墙(x,100,20,400,紫色)
self.wall\u list.add(墙)
3班教室(房间):
定义初始化(自):
房间。uuu初始(自我)
墙=[[0,0,20250,紫色],
[035020250,紫色],
[780,0,20250,紫色],
[780350,20250,紫色],
[20,0760,20,紫色],
[2058076020,紫色]
]
对于墙中的项目:
墙=墙(项目[0],项目[1],项目[2],项目[3],项目[4])
自我介绍