你能做到当两种颜色相互碰撞时游戏结束吗?
can you make it so when two colors collide with each other the game ends?
我是 python 的新手(今年年初才开始)。我正在为一项学校作业开发这个迷宫游戏项目,在该项目中,您使用向上键、向下键和侧键在迷宫中移动一个红色方块。目标是在不与构成迷宫的众多黑色矩形之一发生碰撞的情况下走到尽头。我已经知道一种方法,所以当正方形碰到其中一个矩形时游戏结束(屏幕上会弹出文本询问玩家是否想再次玩)。我学到的方法是对每个矩形使用 CollideRect 函数。 (c.colliderect(r))。其中 c = cube (x, y) 和 r(1,2,3,etc.) = 每个矩形。但是这种方式非常繁琐,因为使用了很多矩形。
是否有更简单、更有效的方法来实现我的目标?
我想使用颜色作为定义变量(从某种意义上说,如果红色 == 黑色 -> gameover()),但我找不到实现此目的的方法。
我也尝试过定义矩形或将它们全部放在一个 class 下。
请告诉我是否有办法做到这一点!
这是我的代码:
import pygame
import time
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
pygame.init()
size = (610, 410)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Game")
screen.fill(white)
pygame.display.flip()
x = 20
y = 360
x_speed = 0
y_speed = 0
def cube(x, y):
c = pygame.Rect(x + x_speed, y + y_speed, 30, 30)
pygame.draw.rect(screen, red,[x + x_speed, y + y_speed, 30, 30])
return c
def rectangles():
#pygame.Rect([0, 0, 610, 410], 10)
#border wasnt working as one of these but works further down#
pygame.Rect(50, 60, 50, 340)
pygame.Rect(100, 60, 50, 50)
pygame.Rect(200, 10, 50, 300)
pygame.Rect(250, 10, 50, 50)
pygame.Rect(150, 150, 50, 200)
pygame.Rect(300, 100, 50, 50)
pygame.Rect(200, 300, 100, 50)
pygame.Rect(350, 60, 50, 340)
pygame.Rect(250, 200, 50, 100)
pygame.Rect(450, 10, 50, 340)
pygame.Rect(550, 60, 50, 340)
def gameover():
font = pygame.font.SysFont(None, 55)
text = font.render("Game Over! Play Again? (y or n)", True, green)
screen.blit(text, [20, 250])
pygame.display.flip()
done = False
while (done == False):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_y):
board1()
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_n):
pygame.quit()
def board1():
x = 15
y = 360
x_speed = 0
y_speed = 0
done = False
while (not done):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_UP):
y_speed = -.1
if(event.key == pygame.K_DOWN):
y_speed = .1
if(event.key == pygame.K_LEFT):
x_speed = -.1
if(event.key == pygame.K_RIGHT):
x_speed = .1
if(event.type == pygame.KEYUP):
if (event.key == pygame.K_UP):
y_speed = 0
if (event.key == pygame.K_DOWN):
y_speed = 0
if (event.key == pygame.K_LEFT):
x_speed = 0
if (event.key == pygame.K_RIGHT):
x_speed = 0
screen.fill(white)
c = cube(x, y)
r = rectangles()
pygame.draw.rect(screen, black, [0, 0, 610, 410], 10)
pygame.draw.rect(screen, black, [50, 60, 50, 340])
pygame.draw.rect(screen, black, [100, 60, 50, 50])
pygame.draw.rect(screen, black, [200, 10, 50, 300])
pygame.draw.rect(screen, black, [250, 10, 50, 50])
pygame.draw.rect(screen, black, [150, 150, 50, 200])
pygame.draw.rect(screen, black, [300, 100, 50, 50])
pygame.draw.rect(screen, black, [200, 300, 100, 50])
pygame.draw.rect(screen, black, [350, 60, 50, 340])
pygame.draw.rect(screen, black, [250, 200, 50, 100])
pygame.draw.rect(screen, black, [450, 10, 50, 340])
pygame.draw.rect(screen, black, [550, 60, 50, 340])
pygame.display.flip()
x = x + x_speed
y = y + y_speed
if(c.colliderect(r)):
gameover()
board1()
好吧,如果您只需要检查是否有任何矩形与玩家发生碰撞,那么您只需将它们全部添加到一个列表中,然后遍历该列表检查每个矩形是否发生碰撞。
这样的事情应该有效:
import pygame
import time
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
pygame.init()
size = (610, 410)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Game")
screen.fill(white)
pygame.display.flip()
x = 20
y = 360
x_speed = 0
y_speed = 0
def cube(x, y):
c = pygame.Rect(x + x_speed, y + y_speed, 30, 30)
pygame.draw.rect(screen, red,[x + x_speed, y + y_speed, 30, 30])
return c
rects = []
def rectangles():
#pygame.Rect([0, 0, 610, 410], 10)
#border wasnt working as one of these but works further down#
# add them all to a list
rects.append(pygame.Rect(50, 60, 50, 340))
rects.append(pygame.Rect(100, 60, 50, 50))
rects.append(pygame.Rect(200, 10, 50, 300))
rects.append(pygame.Rect(250, 10, 50, 50))
rects.append(pygame.Rect(150, 150, 50, 200))
rects.append(pygame.Rect(300, 100, 50, 50))
rects.append(pygame.Rect(200, 300, 100, 50))
rects.append(pygame.Rect(350, 60, 50, 340))
rects.append(pygame.Rect(250, 200, 50, 100))
rects.append(pygame.Rect(450, 10, 50, 340))
rects.append(pygame.Rect(550, 60, 50, 340))
def gameover():
font = pygame.font.SysFont(None, 55)
text = font.render("Game Over! Play Again? (y or n)", True, green)
screen.blit(text, [20, 250])
pygame.display.flip()
done = False
while (done == False):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_y):
board1()
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_n):
pygame.quit()
def board1():
x = 15
y = 360
x_speed = 0
y_speed = 0
done = False
while (not done):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_UP):
y_speed = -.1
if(event.key == pygame.K_DOWN):
y_speed = .1
if(event.key == pygame.K_LEFT):
x_speed = -.1
if(event.key == pygame.K_RIGHT):
x_speed = .1
if(event.type == pygame.KEYUP):
if (event.key == pygame.K_UP):
y_speed = 0
if (event.key == pygame.K_DOWN):
y_speed = 0
if (event.key == pygame.K_LEFT):
x_speed = 0
if (event.key == pygame.K_RIGHT):
x_speed = 0
screen.fill(white)
c = cube(x, y)
r = rectangles()
pygame.draw.rect(screen, black, [0, 0, 610, 410], 10)
pygame.draw.rect(screen, black, [50, 60, 50, 340])
pygame.draw.rect(screen, black, [100, 60, 50, 50])
pygame.draw.rect(screen, black, [200, 10, 50, 300])
pygame.draw.rect(screen, black, [250, 10, 50, 50])
pygame.draw.rect(screen, black, [150, 150, 50, 200])
pygame.draw.rect(screen, black, [300, 100, 50, 50])
pygame.draw.rect(screen, black, [200, 300, 100, 50])
pygame.draw.rect(screen, black, [350, 60, 50, 340])
pygame.draw.rect(screen, black, [250, 200, 50, 100])
pygame.draw.rect(screen, black, [450, 10, 50, 340])
pygame.draw.rect(screen, black, [550, 60, 50, 340])
pygame.display.flip()
x = x + x_speed
y = y + y_speed
# Check for each one of the walls
for rect in rects:
if c.colliderect(rect):
print("collision")
gameover()
board1()
但是如果你确实想通过颜色检测,它有点复杂,但你可以看看这个:
.
我是 python 的新手(今年年初才开始)。我正在为一项学校作业开发这个迷宫游戏项目,在该项目中,您使用向上键、向下键和侧键在迷宫中移动一个红色方块。目标是在不与构成迷宫的众多黑色矩形之一发生碰撞的情况下走到尽头。我已经知道一种方法,所以当正方形碰到其中一个矩形时游戏结束(屏幕上会弹出文本询问玩家是否想再次玩)。我学到的方法是对每个矩形使用 CollideRect 函数。 (c.colliderect(r))。其中 c = cube (x, y) 和 r(1,2,3,etc.) = 每个矩形。但是这种方式非常繁琐,因为使用了很多矩形。
是否有更简单、更有效的方法来实现我的目标?
我想使用颜色作为定义变量(从某种意义上说,如果红色 == 黑色 -> gameover()),但我找不到实现此目的的方法。
我也尝试过定义矩形或将它们全部放在一个 class 下。
请告诉我是否有办法做到这一点!
这是我的代码:
import pygame
import time
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
pygame.init()
size = (610, 410)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Game")
screen.fill(white)
pygame.display.flip()
x = 20
y = 360
x_speed = 0
y_speed = 0
def cube(x, y):
c = pygame.Rect(x + x_speed, y + y_speed, 30, 30)
pygame.draw.rect(screen, red,[x + x_speed, y + y_speed, 30, 30])
return c
def rectangles():
#pygame.Rect([0, 0, 610, 410], 10)
#border wasnt working as one of these but works further down#
pygame.Rect(50, 60, 50, 340)
pygame.Rect(100, 60, 50, 50)
pygame.Rect(200, 10, 50, 300)
pygame.Rect(250, 10, 50, 50)
pygame.Rect(150, 150, 50, 200)
pygame.Rect(300, 100, 50, 50)
pygame.Rect(200, 300, 100, 50)
pygame.Rect(350, 60, 50, 340)
pygame.Rect(250, 200, 50, 100)
pygame.Rect(450, 10, 50, 340)
pygame.Rect(550, 60, 50, 340)
def gameover():
font = pygame.font.SysFont(None, 55)
text = font.render("Game Over! Play Again? (y or n)", True, green)
screen.blit(text, [20, 250])
pygame.display.flip()
done = False
while (done == False):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_y):
board1()
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_n):
pygame.quit()
def board1():
x = 15
y = 360
x_speed = 0
y_speed = 0
done = False
while (not done):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_UP):
y_speed = -.1
if(event.key == pygame.K_DOWN):
y_speed = .1
if(event.key == pygame.K_LEFT):
x_speed = -.1
if(event.key == pygame.K_RIGHT):
x_speed = .1
if(event.type == pygame.KEYUP):
if (event.key == pygame.K_UP):
y_speed = 0
if (event.key == pygame.K_DOWN):
y_speed = 0
if (event.key == pygame.K_LEFT):
x_speed = 0
if (event.key == pygame.K_RIGHT):
x_speed = 0
screen.fill(white)
c = cube(x, y)
r = rectangles()
pygame.draw.rect(screen, black, [0, 0, 610, 410], 10)
pygame.draw.rect(screen, black, [50, 60, 50, 340])
pygame.draw.rect(screen, black, [100, 60, 50, 50])
pygame.draw.rect(screen, black, [200, 10, 50, 300])
pygame.draw.rect(screen, black, [250, 10, 50, 50])
pygame.draw.rect(screen, black, [150, 150, 50, 200])
pygame.draw.rect(screen, black, [300, 100, 50, 50])
pygame.draw.rect(screen, black, [200, 300, 100, 50])
pygame.draw.rect(screen, black, [350, 60, 50, 340])
pygame.draw.rect(screen, black, [250, 200, 50, 100])
pygame.draw.rect(screen, black, [450, 10, 50, 340])
pygame.draw.rect(screen, black, [550, 60, 50, 340])
pygame.display.flip()
x = x + x_speed
y = y + y_speed
if(c.colliderect(r)):
gameover()
board1()
好吧,如果您只需要检查是否有任何矩形与玩家发生碰撞,那么您只需将它们全部添加到一个列表中,然后遍历该列表检查每个矩形是否发生碰撞。 这样的事情应该有效:
import pygame
import time
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
pygame.init()
size = (610, 410)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Game")
screen.fill(white)
pygame.display.flip()
x = 20
y = 360
x_speed = 0
y_speed = 0
def cube(x, y):
c = pygame.Rect(x + x_speed, y + y_speed, 30, 30)
pygame.draw.rect(screen, red,[x + x_speed, y + y_speed, 30, 30])
return c
rects = []
def rectangles():
#pygame.Rect([0, 0, 610, 410], 10)
#border wasnt working as one of these but works further down#
# add them all to a list
rects.append(pygame.Rect(50, 60, 50, 340))
rects.append(pygame.Rect(100, 60, 50, 50))
rects.append(pygame.Rect(200, 10, 50, 300))
rects.append(pygame.Rect(250, 10, 50, 50))
rects.append(pygame.Rect(150, 150, 50, 200))
rects.append(pygame.Rect(300, 100, 50, 50))
rects.append(pygame.Rect(200, 300, 100, 50))
rects.append(pygame.Rect(350, 60, 50, 340))
rects.append(pygame.Rect(250, 200, 50, 100))
rects.append(pygame.Rect(450, 10, 50, 340))
rects.append(pygame.Rect(550, 60, 50, 340))
def gameover():
font = pygame.font.SysFont(None, 55)
text = font.render("Game Over! Play Again? (y or n)", True, green)
screen.blit(text, [20, 250])
pygame.display.flip()
done = False
while (done == False):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_y):
board1()
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_n):
pygame.quit()
def board1():
x = 15
y = 360
x_speed = 0
y_speed = 0
done = False
while (not done):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_UP):
y_speed = -.1
if(event.key == pygame.K_DOWN):
y_speed = .1
if(event.key == pygame.K_LEFT):
x_speed = -.1
if(event.key == pygame.K_RIGHT):
x_speed = .1
if(event.type == pygame.KEYUP):
if (event.key == pygame.K_UP):
y_speed = 0
if (event.key == pygame.K_DOWN):
y_speed = 0
if (event.key == pygame.K_LEFT):
x_speed = 0
if (event.key == pygame.K_RIGHT):
x_speed = 0
screen.fill(white)
c = cube(x, y)
r = rectangles()
pygame.draw.rect(screen, black, [0, 0, 610, 410], 10)
pygame.draw.rect(screen, black, [50, 60, 50, 340])
pygame.draw.rect(screen, black, [100, 60, 50, 50])
pygame.draw.rect(screen, black, [200, 10, 50, 300])
pygame.draw.rect(screen, black, [250, 10, 50, 50])
pygame.draw.rect(screen, black, [150, 150, 50, 200])
pygame.draw.rect(screen, black, [300, 100, 50, 50])
pygame.draw.rect(screen, black, [200, 300, 100, 50])
pygame.draw.rect(screen, black, [350, 60, 50, 340])
pygame.draw.rect(screen, black, [250, 200, 50, 100])
pygame.draw.rect(screen, black, [450, 10, 50, 340])
pygame.draw.rect(screen, black, [550, 60, 50, 340])
pygame.display.flip()
x = x + x_speed
y = y + y_speed
# Check for each one of the walls
for rect in rects:
if c.colliderect(rect):
print("collision")
gameover()
board1()
但是如果你确实想通过颜色检测,它有点复杂,但你可以看看这个: