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

时间:2018-06-04 17:49:59

标签: python pygame

我是python和编码的新手,所以如果我的代码混乱或无组织,请原谅。我正在创建一个战舰游戏,并建立了一个命中系统(白色表示正确的猜测,红色表示不正确的猜测),但是,我似乎无法使更改成为永久性的,我希望当按下空格键时颜色并且您的猜测块会保持在该坐标中。

控制是WASD移动和SPACE开火。

非常感谢任何帮助

这是我的代码:

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()

1 个答案:

答案 0 :(得分:0)

如果问题与此有关:&#34;我似乎无法永久更改&#34;

然后您需要将此信息存储到数据文件中。

您可以从文本文件开始(或使用shelve python模块)。 这个python模块有一个名为shelve.open()的函数,它返回一个&#34;架子文件对象&#34;可用于创建,读取和写入硬盘驱动器上的磁盘文件的数据。 您可以使用任何python模块(例如:sqlite用于数据库方式)。

只需创建一个新的hit_coord_xy并在每次按下一个密钥保存时保存此坐标:

注意:使用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]

并像这样解决:

如果x,y进入hitcoorx,hitcoory,hit_air为真,那么敌人就会被air_damage从空中击中......

您需要创建一个结构数据,其中包含命中/非命中,hit_water,hit_air,可能的命中数等等的coord和值。

游戏中的每个对象都允许使用此数据来显示点击次数,得分并对游戏进行更改。

您的代码很简单,但您可以为每个对象使用类,例如:播放器,地图。源代码简短易维护的主要原因。