首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中运行1位的游戏,比如离线的chrome dino。

在Python中运行1位的游戏,比如离线的chrome dino。
EN

Code Review用户
提问于 2018-10-23 21:09:32
回答 1查看 391关注 0票数 8

我制作了这个小游戏来测试我的诺基亚5110屏幕是否能处理游戏。就像每秒处理许多帧/游戏循环一样。

我正在使用为这个屏幕制作的名为Adafruit_Nokia_LCD的库

如果需要的话,这些是某些情况下的进口:

代码语言:javascript
复制
import Adafruit_Nokia_LCD as LCD
import Adafruit_GPIO.SPI as SPI
import threading
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

我就是这样加载图像的。我有一些黑白图像,使用二进制转换器将它们转换成多行字符串。例如,这些是一些tamagotchi精灵:

代码语言:javascript
复制
tam1 = """00000000000000111100000
00001111000000111110000
00001111100001111110000
00011111100001111110000
00011111111111111111000
00011111111111111111000
00011111111111111111000
00111111111111111111000
00111111111111111111100
01001111000000011111110
01100000011110000000010
10110000111101000000001
10110000111110100000001
10110000111110100000001
10110000111110100000001
01110000011100100000001
01100000001111000000010
00100101000000000000010
00010011000000000000100
00001100000000000011110
00000011111111111100001
00000100000000000000001
00001000000000000011110
00001001000000000010000
00000111000000000010000
00000001000000000011100
00000010000000000100010
00000100000111111000010
00000100011000000111100
00000011100000000000000"""

还有两个精灵,一个叫做tam2和tam3的行走动画

我把它们分成几行:

代码语言:javascript
复制
tam1a = tam1.split('\n')
tam2a = tam2.split('\n')
tam3a = tam3.split('\n')

我有一个不同的线程正在运行以捕获键输入,在这种情况下是空格跳转:

代码语言:javascript
复制
key = "lol" #uhh this is the keyword for no input pretty much idk i just left it in
getch = _Getch()
def thread1():
    global key
    lock = threading.Lock()
    while True:
        with lock:
            key = getch() #this is some code that works just like the msvcrt version
threading.Thread(target = thread1).start()

这是游戏循环,在设置了一些变量之后:

代码语言:javascript
复制
#jump variables 
dist = 0 #distance off the ground
gup = False #isjumping
#obstacle variables
xx = 0 #x position of the obstacle that loops through the screen

#other variables
score = 0;
ind = 0; #current frame of player, there is 3
extraspeed = 0; #makes the pillar go faster as your score rises
while True: #gameloop start
    draw.rectangle((0,0,LCD.LCDWIDTH,LCD.LCDHEIGHT), outline=255, fill=255)
    #clears the screen --^
    draw.text((0,0),str(score),font=font) #draw score
    extraspeed = floor(score / 100) #set extraspeed based on score
    if extraspeed > 10:
        extraspeed = 10
    score += 3
    draw.rectangle((xx,32,xx+3,42),outline=0,fill=0)
    xx = xx + 4 +extraspeed #move the pillar
    if xx >= 84:
        xx = -4;
    if key == ' ':
        if dist == 0:
            gup = True
            key = "lol" #if dist = 0 means its on the ground
            #the "lol" thing is something I left over for some reason, it means no key pressed
        else:
            key = "lol"
    if gup == True:
        if dist != 12: #jumps up to 12 pixels off the ground
            dist += 3
        else:
            gup = False #start falling if reached top
    else:
        if dist > 0:
            dist -= 3
        else:
            dist = 0
    #ind is the current animation sprite to draw
    if ind == 1:
        i = 12-dist #top left corner of drawn sprite y
        j = 60 #top left corner of drawn sprite x
        for line in tam1a:
            for c in line:
                if c == '1':
                    draw.point((j,i),fill=0)
                j+=1
            i+=1
            j=60 #make same as j at start
    if ind == 2:
        i = 12-dist #top left corner of drawn sprite y
        j = 60 #top left corner of drawn sprite x
        for line in tam2a:
            for c in line:
                if c == '1':
                    draw.point((j,i),fill=0)
                j+=1
            i+=1
            j=60 #make same as j at start
    if ind == 3:
        i = 12-dist #top left corner of drawn sprite y
        j = 60 #top left corner of drawn sprite x
        for line in tam3a:
            for c in line:
                if c == '1':
                    draw.point((j,i),fill=0)
                j+=1
            i+=1
            j=60 #make same as j at start

    ind += 1
    if ind == 4:
        ind = 1 #restart the animation
    draw.line((0,43,83,43),fill=0)
    draw.line((0,44,83,44),fill=0) #draw some ground
    if xx >= float(67) and xx<= float(80) and dist <= 7:
        break #Some simple collision detection

# Display image.
    disp.image(image) #draw everything
    disp.display() #display everything

    time.sleep(0.2) #5 frames per second
draw.text((40,10),'Hit',font=font) #got here if 'break' occurs
disp.image(image) #displays hit, which means game over and loop is broken
disp.display()

我的问题:

首先:这个屏幕通过GPIO引脚连接到我的Raspberry-Pi上。

你认为这种装载精灵的方法怎么样?

画精灵怎么样?我遍历字符串,逐行,然后逐字符分别绘制像素(0表示不绘制,1表示绘制黑色)。

现在,当这段代码工作时,我在我的液晶屏幕上经历了幽灵和图像烧录。虽然我知道诺基亚5110屏幕不是为游戏系统设计的,但我的代码能被优化到减少这种鬼影的程度吗?

你认为我的关键输入获取方法如何,这份工作是否可以单独执行?

最后,一种不同的绘图方法是否允许我达到每秒60帧,还是因为这个屏幕的制造方式而不可能?

这是张照片。注意最后一帧的图像刻录是如何进行的?只有一个柱子,只有一个球员的框架,但有两个出现。这个问题是可以解决的,还是仅仅因为屏幕原因?

EN

回答 1

Code Review用户

发布于 2018-10-30 00:38:43

我注意到您正在更新extraspeed的每一个帧。

对此的一个替代办法可以是:

代码语言:javascript
复制
if extraspeed < 10:
    desired_boost = floor(score / 100)
    if desired_boost <= 10:
        extraspeed = desired_boost
    else:
        extraspeed = 10

这样,您不会更新每个帧的extraspeed,只有当它小于10时。

另外,您应该删除以下行中的分号:

代码语言:javascript
复制
score = 0;
ind = 0; #current frame of player, there is 3
extraspeed = 0;

我会为你进一步研究这个问题,但我从来没有亲自去过电脑屏幕以外的屏幕,所以我可能无法进一步帮助你。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/206135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档