我正在学习pygame,到目前为止我已经有了这个。这是一个迷宫(级别尚未完成:P),当你到达红色区块(“E”)时,它会打开一个显示“你赢”的屏幕。我想这样做,当你碰到墙壁时,它会显示一个屏幕,上面写着“你失去了”这是最好的方式吗?谢谢!
import os
import random
import pygame
# Define colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
#Booleans
hasWon = False
#Classes
class Wall(object):
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Player(object):
def __init__(self):
self.rect = pygame.Rect(32, 32, 16, 16)
def move(self, dx, dy):
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)
def move_single_axis(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
testing = True
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
testing = True
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
testing = True
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
testing = True
#Initalize
pygame.init()
#Set the width and height of the screen [width,height]
size=[688,352]
screen=pygame.display.set_mode(size)
#Name on top tab
pygame.display.set_caption("My Game")
# Set positions of graphics
background_position = [0, 0]
# Load and set up graphics.
win_screen = pygame.image.load("winscreen.jpg").convert()
lose_screen = pygame.image.load("losescreen.jpg").convert()
#Arrays/Lists
walls = [] # List to hold the walls
#Create Player
player = Player()
#Level
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"WWW WW",
"WW WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW W",
"W WWWW W W",
"W W WWWW W",
"W WWW WWWW W",
"W W W W W",
"W W W WWW W W",
"W WWW WWW W W W",
"W W W W W W",
"WWW W WWWWW W W",
"WEW WW W",
"W W WWWW WWW W",
"W W W W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WE WWWW W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
# Parse the level string above. W = wall, E = exit
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "E":
end_rect = pygame.Rect(x, y, 16, 16)
x += 16
y += 16
x = 0
#DONT CHANGE
done = False
clock=pygame.time.Clock()
#MAIN LOOP
while done == False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Quit
# Clear the screen
screen.fill(white)
#Changing Speed
key = pygame.key.get_pressed()
if hasWon == False:
if hasLost == False:
if key[pygame.K_LEFT]:
player.move(-2, 0)
if key[pygame.K_RIGHT]:
player.move(2, 0)
if key[pygame.K_UP]:
player.move(0, -2)
if key[pygame.K_DOWN]:
player.move(0, 2)
#Drawing
pygame.draw.rect(screen, (255, 200, 0), player.rect)
pygame.draw.rect(screen, (255, 0, 0), end_rect)
for wall in walls:
pygame.draw.rect(screen, black, wall.rect)
#Win
if player.rect.colliderect(end_rect):
screen.blit(win_screen, background_position)
hasWon = True
#FPS Lock
clock.tick(60)
#Update screen
pygame.display.flip()
# Close the window and quit.
pygame.quit()
答案 0 :(得分:1)
collidelist
函数与您已经使用的colliderect
基本相同,只是它需要检查一个矩形列表而不是一个。
你所拥有的不是一个矩形列表,而是一个Wall
个对象列表,每个对象都有一个矩形。但这很容易解决:
wall_rects = [wall.rect for wall in walls]
所以,现在你就这样做了:
#Lose
if player.rect.collidelist(wall_rects):
screen.blit(lose_screen, background_position)
hasLost = True
当然,您还需要创建一个lose_screen
来显示,并在开头设置hasLost = False
。
作为旁注,您的代码会继续运行主循环,并允许用户即使在获胜后也可以移动。随着这种变化,即使在他失败之后也是如此。我猜你确实想要在hasWon
或hasLost
为真的情况下做一些不同的事情,但是由你来决定应该做些什么。