Python 如何设置障碍物阻止玩家穿墙
速记。这是我的A级NEA编程项目。有两个主要部分-一个是生成迷宫,用户必须在给定的时间段内导航,该时间段目前尚未实施,另一个是用户必须回答教育物理问题以获得最佳分数。问题是从存储在我的系统本地的文本文件中导入的。然后,用户的分数连同完成日期一起导出到本地文本文件 到目前为止,我的程序生成了迷宫,用户可以自由移动。教育方面的工作正如预期的那样Python 如何设置障碍物阻止玩家穿墙,python,python-3.x,pygame,navigation,maze,Python,Python 3.x,Pygame,Navigation,Maze,速记。这是我的A级NEA编程项目。有两个主要部分-一个是生成迷宫,用户必须在给定的时间段内导航,该时间段目前尚未实施,另一个是用户必须回答教育物理问题以获得最佳分数。问题是从存储在我的系统本地的文本文件中导入的。然后,用户的分数连同完成日期一起导出到本地文本文件 到目前为止,我的程序生成了迷宫,用户可以自由移动。教育方面的工作正如预期的那样 # Imports import pygame import sys import csv import random from datetime impo
# Imports
import pygame
import sys
import csv
import random
from datetime import date
# Initialising pygame
pygame.init()
# Setting parameters for commonly used colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Creating a variable set to 'False' for the generation of the maze
done = False
# Setting variables for the size of the maze, how many columns and rows there will be
cols = 10
rows = 10
# Setting variables for the size of the window and the size of each individual part of the walls
width = 600
height = 600
wr = width/cols
hr = height/rows
# Initialising the 'screen' this is the surface within pygame that everything will be displayed upon
screen = pygame.display.set_mode([width, height])
screen_rect = screen.get_rect()
pygame.display.set_caption("Maze Generator")
# Creating a clock for the pygame module to run off of
clock = pygame.time.Clock()
# This is a class for the pathfinder section of the maze, it will allow the program to spot where needs to be visited
class Spot:
def __init__(self, x, y):
self.x = x
self.y = y
self.f = 0
self.g = 0
self.h = 0
self.neighbors = []
self.visited = False
self.walls = [True, True, True, True]
def show(self, color=BLACK):
if self.walls[0]:
pygame.draw.line(screen, color, [self.x*hr, self.y*wr], [self.x*hr+hr, self.y*wr], 2)
if self.walls[1]:
pygame.draw.line(screen, color, [self.x*hr+hr, self.y*wr], [self.x*hr+hr, self.y*wr + wr], 2)
if self.walls[2]:
pygame.draw.line(screen, color, [self.x*hr+hr, self.y*wr+wr], [self.x*hr, self.y*wr+wr], 2)
if self.walls[3]:
pygame.draw.line(screen, color, [self.x*hr, self.y*wr+wr], [self.x*hr, self.y*wr], 2)
def show_block(self, color):
if self.visited:
pygame.draw.rect(screen, color, [self.x*hr+2, self.y*wr+2, hr-2, wr-2])
def add_neighbors(self):
if self.x > 0:
self.neighbors.append(grid[self.x - 1][self.y])
if self.y > 0:
self.neighbors.append(grid[self.x][self.y - 1])
if self.x < rows - 1:
self.neighbors.append(grid[self.x + 1][self.y])
if self.y < cols - 1:
self.neighbors.append(grid[self.x][self.y + 1])
grid = [[Spot(i, j) for j in range(cols)] for i in range(rows)]
for i in range(rows):
for j in range(cols):
grid[i][j].add_neighbors()
current = grid[0][0]
visited = [current]
completed = False
def breakwalls(a, b):
if a.y == b.y and a.x > b.x:
grid[b.x][b.y].walls[1] = False
grid[a.x][a.y].walls[3] = False
if a.y == b.y and a.x < b.x:
grid[a.x][a.y].walls[1] = False
grid[b.x][b.y].walls[3] = False
if a.x == b.x and a.y < b.y:
grid[b.x][b.y].walls[0] = False
grid[a.x][a.y].walls[2] = False
if a.x == b.x and a.y > b.y:
grid[a.x][a.y].walls[0] = False
grid[b.x][b.y].walls[2] = False
class Player:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, hr-2, wr-2)
self.x = int(x)
self.y = int(y)
self.colour = (255, 0, 0)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 5
def draw(self, win):
pygame.draw.rect(win, self.colour, self.rect)
def update(self):
self.velX = 0
self.velY = 0
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed:
self.velY = -self.speed
if self.down_pressed and not self.up_pressed:
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(self.x, self.y, hr-2, wr-2)
def readMyFiles():
questionsAndAnswers = []
correctAnswers = []
with open('questions.txt', newline='') as f:
reader = csv.reader(f, delimiter='\t')
for row in reader:
questionsAndAnswers.append(row)
return questionsAndAnswers
def game(questions, answers, correctAnswers):
score = 0
counter = 0
numberOfQuestions = len(questions)
while not counter == numberOfQuestions:
print(questions[counter])
print(answers[counter])
userAnswer = input('\nWhat is the correct answer?\n')
if userAnswer == correctAnswers[counter]:
print('Well done! That is correct.')
score += 1
else:
print('Better luck next time, that is not correct.')
counter += 1
return score
def shuffleSplit(qna):
random.shuffle(qna)
questions = []
answers = []
correctAnswers = []
for q in qna:
questions.append(q[0])
correctAnswers.append(q[1])
del q[0]
random.shuffle(q)
answers.append(q)
return (questions, answers, correctAnswers)
def exportScores(score, ):
with open('scores.txt', mode='a') as scores:
scores = csv.writer(scores, delimiter='\t')
today = date.today()
dateFormat = today.strftime("%d/%m/%Y")
scores.writerow([dateFormat, score])
player = Player(2, 2)
while not done:
clock.tick(60)
screen.fill(BLACK)
if not completed:
grid[current.x][current.y].visited = True
got_new = False
temp = 10
while not got_new and not completed:
r = random.randint(0, len(current.neighbors)-1)
Tempcurrent = current.neighbors[r]
if not Tempcurrent.visited:
visited.append(current)
current = Tempcurrent
got_new = True
if temp == 0:
temp = 10
if len(visited) == 0:
completed = True
break
else:
current = visited.pop()
temp = temp - 1
if not completed:
breakwalls(current, visited[len(visited)-1])
current.visited = True
current.show_block(WHITE)
for i in range(rows):
for j in range(cols):
grid[i][j].show(WHITE)
# grid[i][j].show_block(BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
questionsAndAnswers = readMyFiles()
questions, answers, correctAnswers = shuffleSplit(questionsAndAnswers)
score = game(questions, answers, correctAnswers)
exportScores(score)
print('\nYour score is', str(score))
sys.exit()
if event.type == pygame.KEYDOWN and completed:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
player.rect.clamp_ip(screen_rect)
if player.x <= 2:
player.left_pressed = False
player.x = 2
if player.y <= 2:
player.up_pressed = False
player.y = 2
if player.x >= width-(wr-2):
player.right_pressed = False
player.x = width-(wr-2)
if player.y >= height-(wr-2):
player.down_pressed = False
player.y = height-(wr-2)
player.draw(screen)
player.update()
pygame.display.flip()
#导入
导入pygame
导入系统
导入csv
随机输入
起始日期时间导入日期
#初始化pygame
pygame.init()
#设置常用颜色的参数
黑色=(0,0,0)
白色=(255,255,255)
蓝色=(0,0255)
红色=(255,0,0)
绿色=(0,255,0)
#为生成迷宫创建一个设置为“False”的变量
完成=错误
#设置迷宫大小、将有多少列和行的变量
cols=10
行=10
#为窗的大小和墙的每个单独部分的大小设置变量
宽度=600
高度=600
wr=宽度/列
hr=高度/行数
#初始化“屏幕”这是pygame中所有内容都将显示的表面
screen=pygame.display.set_模式([宽度,高度])
screen\u rect=screen.get\u rect()
pygame.display.set_标题(“迷宫生成器”)
#创建pygame模块运行的时钟
clock=pygame.time.clock()
#这是一个迷宫探路者部分的类,它将允许程序定位需要访问的位置
上课地点:
定义初始化(self,x,y):
self.x=x
self.y=y
self.f=0
self.g=0
self.h=0
self.neights=[]
自我访问=错误
self.walls=[真的,真的,真的,真的]
def显示(自身,颜色=黑色):
如果self.walls[0]:
pygame.draw.line(屏幕,颜色,[self.x*hr,self.y*wr],[self.x*hr+hr,self.y*wr],2)
如果self.walls[1]:
pygame.draw.line(屏幕,颜色,[self.x*hr+hr,self.y*wr],[self.x*hr+hr,self.y*wr+wr],2)
如果self.walls[2]:
pygame.draw.line(屏幕,颜色,[self.x*hr+hr,self.y*wr+wr],[self.x*hr,self.y*wr+wr],2)
如果self.walls[3]:
pygame.draw.line(屏幕,颜色,[self.x*hr,self.y*wr+wr],[self.x*hr,self.y*wr],2)
def显示_块(自身,颜色):
如果自行访问:
pygame.draw.rect(屏幕,颜色,[self.x*hr+2,self.y*wr+2,hr-2,wr-2])
def添加_邻居(自身):
如果self.x>0:
self.neights.append(网格[self.x-1][self.y])
如果self.y>0:
self.neights.append(网格[self.x][self.y-1])
如果self.x<行-1:
self.neights.append(网格[self.x+1][self.y])
如果self.yb.x:
网格[b.x][b.y]。墙[1]=假
网格[a.x][a.y]。墙[3]=假
如果a.y==b.y且a.xb.y:
网格[a.x][a.y]。墙[0]=假
网格[b.x][b.y]。墙[2]=假
职业球员:
定义初始化(self,x,y):
self.rect=pygame.rect(x,y,hr-2,wr-2)
self.x=int(x)
self.y=int(y)
self.color=(255,0,0)
self.velX=0
self.velY=0
self.left_pressed=False
self.right\u pressed=False
self.up\u pressed=False
self.down\u pressed=False
自身速度=5
def抽签(自我,赢):
pygame.draw.rect(赢,自我着色,自我校正)
def更新(自我):
self.velX=0
self.velY=0
如果按下self.left_而未按下self.right_:
self.velX=-self.speed
如果按下self.right\u而未按下self.left\u:
self.velX=self.speed
如果按下self.up而未按下self.down:
self.velY=-self.speed
如果按下self.down而未按下self.up:
self.velY=self.speed
self.x+=self.velX
self.y+=self.y
self.rect=pygame.rect(self.x,self.y,hr-2,wr-2)
def readMyFiles():
问题和答案=[]
正确答案=[]
将open('questions.txt',换行符='')作为f:
reader=csv.reader(f,分隔符='\t')
对于读取器中的行:
问题和答案。附加(行)
返回问题和答案
def游戏(问题、答案、正确答案):
分数=0
计数器=0
numberOfQuestions=len(问题)
而不是计数器==问题数:
打印(问题[计数器])
打印(答案[计数器])
userAnswer=输入('\n正确答案是什么?\n')
如果userAnswer==correctAnswers[计数器]:
打印('做得好!没错')
分数+=1
其他:
打印(“下次运气好一点,这是不对的。”)
计数器+=1
回击得分
def shuffleSplit(qna):
随机洗牌(qna)
问题=[]
答案=[]
正确答案=[]
对于qna中的q:
问题.追加(q[0])
正确答案。追加(q[1])
德尔q[0]
随机洗牌(q)
答案.附加(q)
返回(问题、答案、正确答案)
def导出分数(分数,):
打开('scores.txt',mode='a')作为分数:
scores=csv.writer(分数,分隔符='\t')
今天=日期。今天()
达特福