- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个沙盒游戏/模拟,它看起来像 danball.com 上的 powdergame,但更简单。
当生成一定数量的方 block 时,我的游戏会延迟
不要关注评论
import pygame
import time
import random
pygame.init()
clock = pygame.time.Clock()
fps = 120
wnx = 800
wny = 600
black = (0,0,0)
grey = (80,80,80)
white = (255,255,255)
black_transparent = (255,255,255,128)
red = (255,0,0)
BACKGROUNDCOLOR = (40,40,40)
#__ Elements __
sand = (255,160,50)
rock = (125,125,125)
bsand = (255,180,150)
brock = (180,180,180)
dirt = (110, 45, 0)
bdirt = (200, 90, 0)
water = (0, 150, 255)
bwater = (25, 200, 255)
wall = (100,100,100)
bwall = (140,140,140)
erase = False
Onbutton = False
color = sand
cubec = sand
wn = pygame.display.set_mode((wnx, wny))
wn.fill(white)
def cursor(cux,cuy,cuw):
boxc = pygame.draw.rect(wn, black, [cux, cuy, cuw, cuw], 1)
def message(Font,Size,colort,xt,yt,text):
font = pygame.font.SysFont('freepixelregular', Size, True)
text = font.render(text, True, colort)
wn.blit(text, (xt, yt))
def cube(cx,cy,cw,ch,cubec):
pygame.draw.rect(wn, cubec, [cx, cy, cw, ch])
def floor(fx,fy,fw,fh):
pygame.draw.rect(wn, grey, [fx, fy, fw, fh])
pygame.draw.line(wn, black, (150,504), (800, 504), 10)
def sidebar(sx,sy,sw,sh):
pygame.draw.rect(wn, grey, [0, 0, 150, 600])
pygame.draw.line(wn, black, (154,0), (154, 500), 10)
def button(bx, by, bw, bh, text, abcol, bcol, colorchange):
global color
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if bx+bw > mouse[0] > bx and by+bh > mouse[1] > by:
Onbutton = True
pygame.draw.rect(wn, abcol, [bx, by, bw, bh])
if click[0] == 1 and colorchange != None:
color = colorchange
else:
pygame.draw.rect(wn, bcol, [bx, by, bw, bh])
Onbutton = False
font = pygame.font.SysFont('freepixelregular', 25,True)
text = font.render(text, True, black)
wn.blit(text, (bx + (bw/14), by + (bh/4)))
def main():
number = 0
toggle_fast = False
erase = False
cubex = [0] * number
cubey = [0] * number
cubec = [0] * number
cubew = 10 #cube size
cubeh = cubew
floory = 500
gravity = (cubew*-1)
clickt = False
exit = False
while not exit:
#________________ QUIT ________________________________________
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
toggle_fast = not toggle_fast
if event.key == pygame.K_v:
erase = not erase
#_____________________ Click / spawn cube / erase cube _____________________________
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if toggle_fast == False:
if event.type == pygame.MOUSEBUTTONDOWN:
if mouse[1] < floory and mouse[0] >= 154:
cubex.append(round((mouse[0]/cubew),0)*cubew)
cubey.append(round((mouse[1]/cubew),0)*cubew)
cubec.append(color)
if click[0] == 1 and toggle_fast == True:
print(erase)
if mouse[1] < floory and mouse[0] >= 154:
cubex.append(round((mouse[0]/cubew),0)*cubew)
cubey.append(round((mouse[1]/cubew),0)*cubew)
cubec.append(color)
#_____________________ GRAVITY _____________________________
for i in range(len(cubex)):
cubeR = pygame.Rect(cubex[i], cubey[i] + cubew, cubew, cubeh)
cisect = [j for j in range(len(cubey)) if j != i and cubeR.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
watercheck = [j for j in range(len(cubey)) if j != i and cubec[i] != (0, 150, 255) and cubec[j] == (0, 150, 255) and cubeR.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
if not any(cisect) and not (cubey[i] + cubew) >= floory:
if not cubec[i] == (100,100,100):
cubey[i] -= gravity
#for j in range(len(cubex):
# if any(watercheck):
# if not (cubey[i] + cubew) >= floory or any(cisect):
# oldposy = CUBEINFO[i][1]
# oldposx = CUBEINFO[i][0]
# CUBEINFO.append(oldposx, oldposy, (0, 150, 255))
#cubex.append(oldposx)
#cubey.append(oldposy)
#cubec.append((0, 150, 255))
#________water physics___________
cubeRxr = pygame.Rect(cubex[i] - cubew, cubey[i], cubew, cubeh)
cubeRxl = pygame.Rect(cubex[i] + cubew, cubey[i], cubew, cubeh)
cubeRdiagr = pygame.Rect(cubex[i] + 10, cubey[i] + 10, cubew, cubeh)
cubeRdiagl = pygame.Rect(cubex[i] - 10, cubey[i] + 10, cubew, cubeh)
cisectx = [j for j in range(len(cubex)) if j != i and cubeRxr.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
cisectxl = [j for j in range(len(cubex)) if j != i and cubeRxl.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
cisectdr = [j for j in range(len(cubex)) if j != i and cubeRdiagr.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
cisectdl = [j for j in range(len(cubex)) if j != i and cubeRdiagl.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
if cubec[i] == (0, 150, 255):
if (cubey[i] + cubew) >= floory or any(cisect): # on ground
if not (cubex[i] + cubew) >= 800 and not cubex[i] <= 164:
if any(cisectx) and not any(cisectxl): #going right because of right wall
cubex[i] += 10
elif any(cisectxl) and not any(cisectx): #going left because of left wall
cubex[i] -= 10
elif any(cisectx) and any(cisectxl):
cubex[i] += 0
elif any(cisect) or (cubey[i] + cubew) >= floory:
negative = [-10, 10]
cubex[i] += random.choice(negative)
elif any(cisect) and not any(cisectdl) and not any(cisectdr):
negative = [-10, 10]
cubex[i] += random.choice(negative)
#____________________ Element _____________________________
#_____________________ DRAW _____________________________
wn.fill(BACKGROUNDCOLOR)
floor(0,floory,800,100)
sidebar(0, 0, 150, 600)
for i in range(len(cubex)):
cube(cubex[i], cubey[i], cubew, cubeh, cubec[i])
cursor(round((mouse[0]/cubew),0)*cubew, round((mouse[1]/cubew),0)*cubew, cubew,)
button(20, 40, 50, 40, 'RCK', brock, rock, (125,125,125))
button(20, 85, 50, 40, 'SND', bsand, sand, (255,160,50))
button(20, 130, 50, 40, 'DRT', bdirt, dirt, (110, 45, 2))
button(20, 175, 50, 40, 'WTR', bwater, water, (0, 150, 255))
button(20, 220, 50, 40, 'WLL', bwall, wall, (100,100,100))
#(Font,Size,colort,xt,yt,message):
message(None, 20, black, 10,400,('ERASE:'+str(erase)))
pygame.display.update()
clock.tick(fps)
main()
pygame.quit()
quit()
也许是因为正方形位置(称为 cubex、cubey)在单独的列表中或其他原因?
我刚开始使用 python,所以它可能是一个愚蠢的错误
感谢您的帮助!
最佳答案
加快游戏速度的唯一方法是避免在立方体数组中不断搜索。
要实现这一点,您必须更改数据的表示形式。你必须从另一个方向思考问题。不要在某个位置搜索立方体,而是“询问”一个位置是否有立方体。不要将立方体存储在列表中,而是创建一个二维 Playground 网格并将立方体关联到网格中的字段。
为立方体创建对象(属性“.dir”用于水,稍后解释):
class Cube:
def __init__(self, color):
self.color = color
self.dir = 1
创建空的 playground(每个字段都由 None
初始化):
cubew = 10 #cube size
cubeh = cubew
pg_rect = pygame.Rect(160, 0, 650, 500)
pg_size = (pg_rect.width // cubew, pg_rect.height // cubeh)
pg_grid = [[None for i in range(pg_size[1])] for j in range(pg_size[0])]
点击鼠标即可轻松将立方体添加到 playground:
if event.type == pygame.MOUSEBUTTONDOWN:
if mouse[1] < floory and mouse[0] >= 154:
i, j = ((mouse[0]-pg_rect.left) // cubew, (mouse[1]-pg_rect.top) // cubeh)
if not pg_grid[i][j]:
pg_grid[i][j] = Cube(color)
要绘制立方体,必须遍历整个字段:
for i in range(pg_size[0]):
for j in range(pg_size[1]):
if pg_grid[i][j]:
pos = (pg_rect.left + i * cubew, pg_rect.top + j * cubeh)
cube(*pos, cubew, cubeh, pg_grid[i][j].color)
为了更新立方体的位置(重力、水),必须列出所有立方体并且必须更改立方体在字段中的位置。
cubes = [(i, j) for i in range(pg_size[0]) for j in range(pg_size[1]-1, 0, -1) if pg_grid[i][j]]
for i, j in cubes:
# [...]
对于重力,必须检查立方体下方的文件是否“自由”(无
):
fall_down = pg_grid[i][j].color != wall
if fall_down and j < pg_size[1]-1 and pg_grid[i][j+1] == None:
pg_grid[i][j+1] = pg_grid[i][j]
pg_grid[i][j] = None
对于水效果,必须检查立方体旁边的字段(由 self.dir
标识)是否“免费”,如果是“免费”,则立方体会继续向前。否则它的方向必须改变:
is_water = pg_grid[i][j].color == water
if is_water:
if pg_grid[i][j].dir < 0:
if i <= 0 or pg_grid[i-1][j]:
pg_grid[i][j].dir = 1
else:
pg_grid[i-1][j] = pg_grid[i][j]
pg_grid[i][j] = None
else:
if i >= pg_size[0]-1 or pg_grid[i+1][j]:
pg_grid[i][j].dir = -1
else:
pg_grid[i+1][j] = pg_grid[i][j]
pg_grid[i][j] = None
查看示例,其中我将更改应用于您的原始代码:
import pygame
import time
import random
pygame.init()
clock = pygame.time.Clock()
fps = 120
wnx = 800
wny = 600
black = (0,0,0)
grey = (80,80,80)
white = (255,255,255)
black_transparent = (255,255,255,128)
red = (255,0,0)
BACKGROUNDCOLOR = (40,40,40)
#__ Elements __
sand = (255,160,50)
rock = (125,125,125)
bsand = (255,180,150)
brock = (180,180,180)
dirt = (110, 45, 0)
bdirt = (200, 90, 0)
water = (0, 150, 255)
bwater = (25, 200, 255)
wall = (100,100,100)
bwall = (140,140,140)
erase = False
Onbutton = False
color = sand
cubec = sand
wn = pygame.display.set_mode((wnx, wny))
wn.fill(white)
class Cube:
def __init__(self, color):
self.color = color
self.dir = 1
def cursor(cux,cuy,cuw):
boxc = pygame.draw.rect(wn, black, [cux, cuy, cuw, cuw], 1)
def message(Font,Size,colort,xt,yt,text):
font = pygame.font.SysFont('freepixelregular', Size, True)
text = font.render(text, True, colort)
wn.blit(text, (xt, yt))
def cube(cx,cy,cw,ch,cubec):
pygame.draw.rect(wn, cubec, [cx, cy, cw, ch])
def floor(fx,fy,fw,fh):
pygame.draw.rect(wn, grey, [fx, fy, fw, fh])
pygame.draw.line(wn, black, (150,504), (800, 504), 10)
def sidebar(sx,sy,sw,sh):
pygame.draw.rect(wn, grey, [0, 0, 150, 600])
pygame.draw.line(wn, black, (154,0), (154, 500), 10)
def button(bx, by, bw, bh, text, abcol, bcol, colorchange):
global color
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if bx+bw > mouse[0] > bx and by+bh > mouse[1] > by:
Onbutton = True
pygame.draw.rect(wn, abcol, [bx, by, bw, bh])
if click[0] == 1 and colorchange != None:
color = colorchange
else:
pygame.draw.rect(wn, bcol, [bx, by, bw, bh])
Onbutton = False
font = pygame.font.SysFont('freepixelregular', 25,True)
text = font.render(text, True, black)
wn.blit(text, (bx + (bw/14), by + (bh/4)))
def main():
number = 0
toggle_fast = False
erase = False
cubew = 10 #cube size
cubeh = cubew
pg_rect = pygame.Rect(160, 0, 650, 500)
pg_size = (pg_rect.width // cubew, pg_rect.height // cubeh)
pg_grid = [[None for i in range(pg_size[1])] for j in range(pg_size[0])]
floory = 500
gravity = (cubew*-1)
clickt = False
exit = False
while not exit:
#________________ QUIT ________________________________________
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
toggle_fast = not toggle_fast
if event.key == pygame.K_v:
erase = not erase
#_____________________ Click / spawn cube / erase cube _____________________________
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if toggle_fast == False:
if event.type == pygame.MOUSEBUTTONDOWN:
if mouse[1] < floory and mouse[0] >= 154:
i, j = ((mouse[0]-pg_rect.left) // cubew, (mouse[1]-pg_rect.top) // cubeh)
if not pg_grid[i][j]:
pg_grid[i][j] = Cube(color)
if click[0] == 1 and toggle_fast == True:
print(erase)
if mouse[1] < floory and mouse[0] >= 154:
i, j = ((mouse[0]-pg_rect.left) // cubew, (mouse[1]-pg_rect.top) // cubeh)
if not pg_grid[i][j]:
pg_grid[i][j] = Cube(color)
# update cubes
cubes = [(i, j) for i in range(pg_size[0]) for j in range(pg_size[1]-1, 0, -1) if pg_grid[i][j]]
for i, j in cubes:
fall_down = pg_grid[i][j].color != wall
is_water = pg_grid[i][j].color == water
if fall_down and j < pg_size[1]-1 and pg_grid[i][j+1] == None:
#_____________________ GRAVITY _____________________________
pg_grid[i][j+1] = pg_grid[i][j]
pg_grid[i][j] = None
elif is_water:
#________water physics___________
if pg_grid[i][j].dir < 0:
if i <= 0 or pg_grid[i-1][j]:
pg_grid[i][j].dir = 1
else:
pg_grid[i-1][j] = pg_grid[i][j]
pg_grid[i][j] = None
else:
if i >= pg_size[0]-1 or pg_grid[i+1][j]:
pg_grid[i][j].dir = -1
else:
pg_grid[i+1][j] = pg_grid[i][j]
pg_grid[i][j] = None
wn.fill(BACKGROUNDCOLOR)
floor(0,floory,800,100)
sidebar(0, 0, 150, 600)
# draw cubes
for i in range(pg_size[0]):
for j in range(pg_size[1]):
if pg_grid[i][j]:
pos = (pg_rect.left + i * cubew, pg_rect.top + j * cubeh)
cube(*pos, cubew, cubeh, pg_grid[i][j].color)
cursor(round((mouse[0]/cubew),0)*cubew, round((mouse[1]/cubew),0)*cubew, cubew,)
button(20, 40, 50, 40, 'RCK', brock, rock, (125,125,125))
button(20, 85, 50, 40, 'SND', bsand, sand, (255,160,50))
button(20, 130, 50, 40, 'DRT', bdirt, dirt, (110, 45, 2))
button(20, 175, 50, 40, 'WTR', bwater, water, (0, 150, 255))
button(20, 220, 50, 40, 'WLL', bwall, wall, (100,100,100))
#(Font,Size,colort,xt,yt,message):
message(None, 20, black, 10,400,('ERASE:'+str(erase)))
pygame.display.update()
clock.tick(fps)
main()
pygame.quit()
quit()
关于python - 简单游戏的Pygame低帧率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55073598/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!