Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从if语句创建永久更改?_Python_Pygame - Fatal编程技术网

Python 如何从if语句创建永久更改?

Python 如何从if语句创建永久更改?,python,pygame,Python,Pygame,我对python和一般的编码都是新手,所以如果我的代码杂乱无章,请原谅。我正在创建一个战舰游戏,并且已经建立了一个命中系统(白色表示正确的猜测,红色表示错误的猜测),但是,我似乎无法使这些更改永久不变,我希望当按下空格按钮时,您猜测的颜色和块保持在该坐标中 控制装置用于移动,空间用于开火 非常感谢您的帮助 这是我的密码: import pygame import random pygame.init() pygame.font.init() win = pygame.display.set_

我对python和一般的编码都是新手,所以如果我的代码杂乱无章,请原谅。我正在创建一个战舰游戏,并且已经建立了一个命中系统(白色表示正确的猜测,红色表示错误的猜测),但是,我似乎无法使这些更改永久不变,我希望当按下空格按钮时,您猜测的颜色和块保持在该坐标中

控制装置用于移动,空间用于开火

非常感谢您的帮助

这是我的密码:

import pygame
import random

pygame.init()
pygame.font.init()

win = pygame.display.set_mode((1050, 550))  # set display to a size of 1000x500 pixels

pygame.display.set_caption("Battleships")  # name this screen "Battleships"


# colours
black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
shiptestcolour = (0, 50, 255)
shipfinalcolour = (0, 200, 255)
# fonts
myfont = pygame.font.SysFont('arial bold', 30)
textsurface1 = myfont.render('Your score: ', False, (0, 255, 0))  # add in score keeping variable
textsurface2 = myfont.render('Enemy score: ', False, (0, 255, 0))
textsurface3 = myfont.render('A      B      C      D      E      F      G      H      I      J', False, black)

enemyship_listx = [50,100,150,200,250,300,350,400,450,500]
enemyship_listy = [50,100,150,200,250,300,350,400,450,500]

randomnum1=(random.randint(0, 8))
randomnum2=(random.randint(0, 8))
randomnum3=(random.randint(0, 8))
randomnum4=(random.randint(0, 7))
randomnum5=(random.randint(0, 8))
randomnum6=(random.randint(0, 6))
randomnum7=(random.randint(0, 8))
randomnum8=(random.randint(0, 8))
randomnum9=(random.randint(0, 6))
randomnum10=(random.randint(0, 8))
randomnum11=(random.randint(0, 6))
randomnum12=(random.randint(0, 8))

ship1smallx = enemyship_listx[randomnum1]
ship1smally = enemyship_listy[randomnum2]
ship2mediumx = enemyship_listx[randomnum3]
ship2mediumy = enemyship_listy[randomnum4]
ship3largex = enemyship_listx[randomnum5]
ship3largey = enemyship_listy[randomnum6]
ship4largex = enemyship_listx[randomnum7]
ship4largey = enemyship_listy[randomnum8]
ship5mediumx = enemyship_listx[randomnum9]
ship5mediumy = enemyship_listy[randomnum10]
ship6smallx = enemyship_listx[randomnum11]
ship6smally = enemyship_listy[randomnum12]

enemyship_listx = [50,100,150,200,250,300,350,400,450,500]
enemyship_listy = [50,100,150,200,250,300,350,400,450,500]

shipxcoor= [ship1smallx, ship2mediumx, ship3largex, ship4largex, ship5mediumx, ship6smallx]
shipycoor= [ship1smally, ship2mediumy, ship3largey, ship4largey, ship5mediumy, ship6smally ]

hitcoorx = [50,100,150,200,250,300,350,400,450,500]
hitcoory = [50,100,150,200,250,300,350,400,450,500]

x = 0 #x used for x axis not to be confused with other x axis variables
y = 0 #y is used for y axis not to be confused with other y axis variables

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # when the player presses the Exit button in the top right the window will close
            run = False

    mouse = pygame.mouse.get_pos()

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a] and x>0:
        x = x - 1
        hitcoorx[x]
    if keys[pygame.K_d] and x < 9:
        x = x + 1
        hitcoorx[x]
    if keys[pygame.K_w] and y >0:
        y = y - 1
        hitcoory[x]
    if keys[pygame.K_s] and y< 9:
        y = y + 1
        hitcoory[y]

    win.fill(shipfinalcolour)

    pygame.draw.rect(win, (0, 100, 255), (550, 50, 500, 500))  # right side player ships and enemy hit

    pygame.draw.rect(win, shipfinalcolour, (ship1smallx, ship1smally, 50, 50))  # enemy first ship blends in
    pygame.draw.rect(win, shipfinalcolour, (ship2mediumx, ship2mediumy, 50, 100))
    pygame.draw.rect(win, shipfinalcolour, (ship3largex, ship3largey, 50, 150) )
    pygame.draw.rect(win, shipfinalcolour, (ship4largex,ship4largey,150, 50))
    pygame.draw.rect(win, shipfinalcolour, (ship5mediumx,ship5mediumy, 100, 50))
    pygame.draw.rect(win, shipfinalcolour, (ship6smallx,ship6smally, 50, 50))

    pygame.draw.rect(win, black, ((mouse[0] // 50) * 50, (mouse[1] // 50) * 50, 50, 50))  # using mouse create ships

    if keys[pygame.K_RIGHT]:
        pygame.draw.rect(win, black, (mouse[0]//50 * 50 + 50, mouse[1] // 50 *50,50, 50))
    if keys[pygame.K_LEFT]:
        pygame.draw.rect(win, black, (mouse[0]//50 * 50 - 50, mouse[1]// 50 * 50, 50, 50))
    if keys[pygame.K_UP]:
        pygame.draw.rect(win, black, (mouse[0]// 50 * 50, mouse[1]//50 * 50 - 50, 50 , 50))
    if keys[pygame.K_DOWN]:
        pygame.draw.rect(win, black, (mouse[0]//50*50, mouse[1]//50 *50 +50, 50, 50))
    pygame.draw.rect(win, black,(hitcoorx[x], hitcoory[y], 50, 50))  # player hit selector moves by 50 pixels eachtime is 50x50

    if keys[pygame.K_SPACE]:
        new_variablex = x
        new_variabley = y

        if enemyship_listx[new_variablex] == ship1smallx and enemyship_listy[new_variabley] == ship1smally:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship2mediumx and enemyship_listy[new_variabley] == ship2mediumy:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship2mediumx and enemyship_listy[new_variabley] == ship2mediumy + 50:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship3largex and enemyship_listy[new_variabley] == ship3largey:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship3largex and enemyship_listy[new_variabley] == ship3largey + 50:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship3largex and enemyship_listy[new_variabley] == ship3largey + 100:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship4largex and enemyship_listy[new_variabley] == ship4largey:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship4largex + 50 and enemyship_listy[new_variabley] == ship4largey:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship4largex + 100 and enemyship_listy[new_variabley] == ship4largey:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship5mediumx and enemyship_listy[new_variabley] == ship5mediumy:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship5mediumx + 50 and enemyship_listy[new_variabley] == ship5mediumy:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        elif enemyship_listx[new_variablex] == ship6smallx and enemyship_listy[new_variabley] == ship6smally:
            pygame.draw.rect(win, white, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

        else:
            pygame.draw.rect(win, red, (hitcoorx[new_variablex], hitcoory[new_variabley], 50, 50))

    for i in range(50,551,50):   # grid system for left side
        pygame.draw.line(win, black, (i,50), (i,550), 5)
        pygame.draw.line(win, black, (50,i), (550,i), 5)

    win.blit(textsurface1, (400, 65))
    win.blit(textsurface2, (875, 65))
    win.blit(textsurface3, (75, 10))

    pygame.display.update()
导入pygame
随机输入
pygame.init()
pygame.font.init()
win=pygame.display.set_模式((1050550))#将显示设置为1000x500像素的大小
pygame.display.set_标题(“战列舰”)#将此屏幕命名为“战列舰”
#颜色
黑色=(0,0,0)
白色=(255,255,255)
红色=(200,0,0)
绿色=(0,200,0)
鲜红色=(255,0,0)
亮绿色=(0,255,0)
ShipTestColor=(0,50,255)
ShipFinalColor=(0200255)
#字体
myfont=pygame.font.SysFont('arial bold',30)
textsurface1=myfont.render('Your score:',False,(0,255,0))#添加分数保持变量
textsurface2=myfont.render('敌人得分:',False,(0,255,0))
textsurface3=myfont.render('A B C D E F G H I J',False,黑色)
enemyship_listx=[50100150200250300350400450500]
敌我精神=[50100150200250300350400450500]
randomnum1=(random.randint(0,8))
randomnum2=(random.randint(0,8))
randomnum3=(random.randint(0,8))
randomnum4=(random.randint(0,7))
randomnum5=(random.randint(0,8))
randomnum6=(random.randint(0,6))
randomnum7=(random.randint(0,8))
randomnum8=(random.randint(0,8))
randomnum9=(random.randint(0,6))
randomnum10=(random.randint(0,8))
randomnum11=(random.randint(0,6))
randomnum12=(random.randint(0,8))
ship1smallx=敌我关系\u列表x[randomnum1]
ship1smally=敌对状态\u listy[randomnum2]
ship2mediumx=敌我关系列表x[randomnum3]
ship2mediumy=敌对状态[随机数4]
ship3largex=敌我关系\u列表x[randomnum5]
ship3largey=敌我精神[随机数6]
ship4largex=敌我关系\u列表x[randomnum7]
ship4largey=敌我精神[随机数M8]
ship5mediumx=敌我关系\u列表x[randomnum9]
ship5mediumy=敌对状态[随机数M10]
ship6smallx=敌我关系\u列表x[randomnum11]
ship6smally=敌我精神[随机数M12]
enemyship_listx=[50100150200250300350400450500]
敌我精神=[50100150200250300350400450500]
shipxcoor=[ship1smallx,ship2mediumx,ship3largex,ship4largex,ship5mediumx,ship6smallx]
shipycoor=[ship1smally,ship2mediumy,ship3largey,ship4largey,ship5mediumy,ship6smally]
hitcoorx=[50100150200250300350400450500]
hitcoory=[50100150200250300350400450500]
x=0#x用于x轴,不要与其他x轴变量混淆
y=0#y用于y轴,以免与其他y轴变量混淆
运行=真
运行时:
pygame.时间延迟(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:#当玩家按下右上角的退出按钮时,窗口将关闭
运行=错误
mouse=pygame.mouse.get_pos()
keys=pygame.key.get_pressed()
如果键[pygame.K_a]和x>0:
x=x-1
hitcoorx[x]
如果键[pygame.K_d]和x<9:
x=x+1
hitcoorx[x]
如果键[pygame.K_w]和y>0:
y=y-1
希库里[x]
如果键[pygame.K_s]和y<9:
y=y+1
希库里[y]
win.fill(ShipFinalColor)
pygame.draw.rect(赢,(0100255),(55050500500))#右侧玩家飞船和敌人命中
pygame.draw.rect(赢,shipfinalcolour,(ship1smallx,ship1smally,50,50))#敌人的第一艘飞船进入
pygame.draw.rect(赢,shipfinalcolour,(ship2mediumx,ship2mediumy,50100))
pygame.draw.rect(赢,ShipFinalColor,(ship3largex,ship3largey,50150))
pygame.draw.rect(赢,ShipFinalColor,(ship4largex,ship4largey,150,50))
pygame.draw.rect(赢,ShipFinalColor,(ship5mediumx,ship5mediumy,100,50))
pygame.draw.rect(赢,shipFinalColor,(ship6smallx,ship6smally,50,50))
pygame.draw.rect(赢,黑,((鼠标[0]//50)*50,(鼠标[1]//50)*50,50,50))#使用鼠标创建飞船
如果键[pygame.K_RIGHT]:
pygame.draw.rect(赢,黑,(鼠标[0]//50*50+50,鼠标[1]//50*50,50,50))
如果键[pygame.K_左]:
pygame.draw.rect(赢,黑,(鼠标[0]//50*50-50,鼠标[1]//50*50,50,50))
如果键[pygame.K_UP]:
pygame.draw.rect(赢,黑,(鼠标[0]//50*50,鼠标[1]//50*50-50,50,50))
如果键[pygame.K_DOWN]:
pygame.draw.rect(赢,黑,(鼠标[0]//50*50,鼠标[1]//50*50+50,50,50))
pygame.draw.rect(赢,黑,(hitcoorx[x],hitcoory[y],50,50))#玩家的命中选择器每次移动50像素,为50x50
如果键[pygame.K_SPACE]:
新变量x=x
新变量y=y
如果enemyship_listx[新变量X]==发货单,且enemyship_listy[新变量]==发货单:
pygame.draw.rect(win,white,(hitcoorx[new_variablex],hitcoory[new_variabley],50,50))
elif enemyship_listx[新变量X]==ship2mediumx和enemyship_listy[新变量]==ship2mediumy:
pygame.draw.rect(win,white,(hitcoorx[new_variablex],hitcoory[new_variabley],50,50))
elif enemyship_listx[新变量X]==ship2mediumx和enemyship_listy[新变量Y]==ship2mediumy+50:
pygame.draw.rect(win,white,(hitcoorx[new_variablex],hitcoory[new_variabley],50,50))
elif enemyship_listx[新变量X]==ship3largex和enemyship_listy[新变量]==ship3largey:
pygame.draw.rect(win,white,(hitcoorx[new_variablex],hitcoory[new_variabley],50,50))
elif enemyship_listx[新变量X]==ship3largex和enemyship_listy[新变量]==ship3largey+50:
pygame.draw.rect(win,white,(hitcoorx[新变量]),hitcoory[新变量]
if keys[pygame.K_a] and x>0:
    x = x - 1
    y =0
    hit_air = true
    hit_water = false
    hitcoord_point[x, y, hit_air, hit_water]