我想让我的游戏“回到”主菜单,我想知道我是否可以使用GameWindow功能“内部”它自己,以便它会创建一个“重置”。基本上,如果我的精灵碰撞,它说“游戏结束”我想按下“返回”按钮,如果我再次按下播放它会显示一个“重置”游戏,其中“计时器”,“小行星”,“船”,回到开头就好像我又在玩一个新游戏。
我也在想,现在给我的错误是否对我的问题有任何意义?
代码/游戏大部分是工作的,只是没有“重置”的目的。
以下是错误:
"C:\Users\user\AppData\Local\Programs\Python\Python38-32\python.exe" "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py"
B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py:261: SyntaxWarning: "is" with a literal. Did you mean "=="?
if bullet_state is "fire":
B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py:322: SyntaxWarning: "is" with a literal. Did you mean "=="?
if bullet_state is "ready":
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py:171: DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
display.blit(ammoboximg,(x,y))
Traceback (most recent call last):
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 374, in <module>
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 315, in gamewindow
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 315, in gamewindow
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 315, in gamewindow
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 330, in gamewindow
keys = pygame.key.get_pressed()
pygame.error: video system not initialized
Process finished with exit code 1
下面是完整的代码:
# initialization of pygame
import pygame
import random
import math
pygame.init()
# creating the display
display = pygame.display.set_mode((500, 500))
# title & icon
spaceship = pygame.image.load("Space Blitz Sprites/spaceship.png")
pygame.display.set_icon(spaceship)
pygame.display.set_caption("SpaceBlitz")
# main menu sprites
spaceblitz = pygame.image.load("Space Blitz Sprites/spaceblitz.png")
play = pygame.image.load("Space Blitz Sprites/play.png")
howtoplay = pygame.image.load("Space Blitz Sprites/howtoplay.png")
about = pygame.image.load("Space Blitz Sprites/about.png")
quit = pygame.image.load("Space Blitz Sprites/quit.png")
# inside main menu
instruction = pygame.image.load("Space Blitz Sprites/instruction.png")
back = pygame.image.load("Space Blitz Sprites/back.png")
aboutdev = pygame.image.load("Space Blitz Sprites/aboutdev.png")
# main menu music
music = pygame.mixer.music.load("Space Blitz Sprites/mountaintrails.mp3")
# PlayerSpriteMovement
playerimg = pygame.image.load("Space Blitz Sprites/spaceship.png")
playerX = 250
playerY = 400
velocity = 3
clock = pygame.time.Clock()
# Bullet
bulletimg = pygame.image.load("Space Blitz Sprites/bullet.png")
bulletX = 0
bulletY = playerY
bulletx_change = 0
bulletY_change = 8
bullet_state = "ready"
bullet_ammo = 5
bulletfont = pygame.font.Font('freesansbold.ttf', 16)
# Ammo Box
ammoboximg = pygame.image.load('Space Blitz Sprites/ammo.png')
ammoboxX = random.randint(0,468)
ammoboxY = random.randint(-1000,-800)
ammoboxY_change = -1.5
# Asteroid
asteroidimg = []
asteroidX = []
asteroidY = []
asteroidX_change = []
asteroidY_change = []
no_of_enemies = 40
def mainmenu():
global menuselect
global spaceblitz
menu = True
pygame.mixer.music.play(50)
pygame.mixer.music.set_volume(0.2)
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if start.collidepoint(pos):
menu = False
menuselect = 1
pygame.mixer.music.stop()
if controls.collidepoint(pos):
menu = False
menuselect = 2
pygame.mixer.music.stop()
if developer.collidepoint(pos):
menu = False
menuselect = 3
pygame.mixer.music.stop()
if exit.collidepoint(pos):
menu = False
menuselect = 4
display.fill((0, 0, 0))
display.blit(spaceblitz, (170,150))
start = display.blit(play, (170,250))
controls = display.blit(howtoplay, (170,300))
developer = display.blit(about, (170,350))
exit = display.blit(quit, (170,400))
pygame.display.flip()
pygame.display.update()
def controls():
global menuselect
global menu
controls = True
while controls:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
controls = False
menu = True
menuselect = 0
balik = display.blit(back, (0,450))
display.blit(instruction, (0,0))
pygame.display.flip()
pygame.display.update()
display.fill((0, 0, 0))
def developers():
global menuselect
global menu
dev = True
while dev:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
dev = False
menu = True
menuselect = 0
balik = display.blit(back, (0, 450))
display.blit(aboutdev, (0, 0))
pygame.display.flip()
pygame.display.update()
display.fill((0, 0, 0))
# Asteroid
for r in range(no_of_enemies):
asteroidimg.append(pygame.image.load("Space Blitz Sprites/asteroid.png"))
asteroidX.append(random.randint(-100, 500))
asteroidY.append(random.randint(-300, -30))
asteroidX_change.append(0)
asteroidY_change.append(2)
# Game Over Text
overfont = pygame.font.Font('freesansbold.ttf',32)
# Sprite image
def player(x, y):
display.blit(playerimg, (x, y))
def fire_bullet(x, y):
global bullet_state
if bullet_ammo > -1:
bullet_state = "fire"
display.blit(bulletimg, (x + 9, y + -7))
else:
bullet_state = "ready"
def ammobox(x,y):
display.blit(ammoboximg,(x,y))
def AmBoxCollision(ammoboxX,ammoboxY,playerX,playerY):
ammoboxdistance = math.sqrt((math.pow(ammoboxX - playerX, 2)) + (math.pow(ammoboxY - playerY, 2)))
if ammoboxdistance < 27:
return True
else:
return False
def ammo():
global bullet_ammo
global ammo_decrease
ammo_decrease = 1
bullet_ammo -= ammo_decrease
def asteroid(x, y, r):
display.blit(asteroidimg[r], (x, y))
def BulCollision(asteroidX, asteroidY, bulletX, bulletY):
buldistance = math.sqrt((math.pow(bulletX - asteroidX, 2)) + (math.pow(bulletY - asteroidY, 2)))
if buldistance < 27:
return True
else:
return False
def PlayCollision(asteroidX, asteroidY, playerX, playerY):
playdistance = math.sqrt((math.pow(playerX - asteroidX, 2)) + (math.pow(playerY - asteroidY, 2)))
if playdistance < 27:
return True
else:
return False
def gameover_screen():
overtext = overfont.render("GAME OVER",True,(255,255,255))
display.blit(overtext, (150,250))
# mainloop
def gamewindow():
global menuselect
global playerX
global playerY
global velocity
global clock
global bulletX
global bulletY
global bulletY_change
global bullet_state
global asteroidX
global asteroidY
global asteroidY_change
global no_of_enemies
global ammoboxX
global ammoboxY
global ammoboxY_change
global dev
global menu
global bullet_ammo
global ammo_decrease
global passed_time
font = pygame.font.Font(None, 54)
font_color = pygame.Color('white')
start_time = pygame.time.get_ticks()
run_timer = True
running = True
while running:
clock.tick(60)
display.fill((0, 0, 0))
AmmoBoxCollision = AmBoxCollision(ammoboxX,ammoboxY,playerX,playerY)
if AmmoBoxCollision:
ammoboxY = random.randint(-1000, -800)
ammoboxX = random.randint(0, 468)
if bullet_ammo <= 4 and bullet_ammo > -1:
bullet_ammo += 1
if bullet_ammo == -1:
bullet_ammo +=2
if bullet_ammo == -1 or bullet_ammo == 0:
bullet_text = bulletfont.render("Ammo:%d" % bullet_ammo, True, (0, 0, 0))
display.blit(bullet_text, (10, 468))
noammo = bulletfont.render("NO AMMO",True,(255,255,255))
display.blit(noammo,(10,468))
else:
bullet_text = bulletfont.render("Ammo:%d" % bullet_ammo, True, (255, 255, 255))
display.blit(bullet_text, (10, 468))
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
if bulletY <= 0:
bulletY = playerY
bullet_state = "ready"
for r in range(no_of_enemies):
asteroid(asteroidX[r], asteroidY[r], r)
asteroidY[r] += asteroidY_change[r]
if asteroidY[r] >= 500:
asteroidY[r] = random.randint(-300, -30)
asteroidX[r] = random.randint(-100, 500)
Bulletcollision = BulCollision(asteroidX[r], asteroidY[r], bulletX, bulletY)
if Bulletcollision:
bulletY = playerY
bullet_state = "ready"
asteroidX[r] = random.randint(-100, 500)
asteroidY[r] = random.randint(-300, -30)
# Game over
PlayerCollision = PlayCollision(asteroidX[r], asteroidY[r], playerX, playerY)
if PlayerCollision:
for j in range(no_of_enemies):
asteroidY_change[j] = 0
asteroidY[j] = random.randint(-300, -30)
asteroidX[j] = random.randint(-100, 500)
asteroidY_change[j] = 2
velocity = 0
bulletY_change = 0
bulletY = 600
ammoboxY_change = 0
run_timer = False
gameover_screen()
playerX = 250
playerY = 400
velocity = 3
# movement
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
running = False
menu = True
menuselect = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if bullet_ammo <= 5 and bullet_ammo > -1:
if bullet_state is "ready":
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
ammo()
# player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerX -= velocity
if keys[pygame.K_RIGHT]:
playerX += velocity
if keys[pygame.K_DOWN]:
playerY += velocity
if keys[pygame.K_UP]:
playerY -= velocity
# Border
if playerX <= 0:
playerX = 0
elif playerX >= 468:
playerX = 468
if playerY <= 0:
playerY = 0
elif playerY >= 468:
playerY = 468
if ammoboxY > 500:
ammoboxY = random.randint(-1000,-800)
ammoboxX = random.randint(0,468)
ammobox(ammoboxX,ammoboxY)
ammoboxY -= ammoboxY_change
if run_timer:
current_time = pygame.time.get_ticks()
passed_time = current_time - start_time
text = font.render(str(passed_time / 1000), True, font_color)
display.blit(text, (50, 50))
balik = display.blit(back, (350, 450))
player(playerX, playerY)
pygame.display.update()
mainmenu()
while True:
if menuselect == 0:
mainmenu()
elif menuselect == 1:
gamewindow()
elif menuselect == 2:
controls()
elif menuselect == 3:
developers()
elif menuselect == 4:
pygame.quit()
这里是我想把我的重置按钮(在PlayerCollision或balik.collidepoint(pos))
if PlayerCollision:
for j in range(no_of_enemies):
asteroidY_change[j] = 0
asteroidY[j] = random.randint(-300, -30)
asteroidX[j] = random.randint(-100, 500)
asteroidY_change[j] = 2
velocity = 0
bulletY_change = 0
bulletY = 600
ammoboxY_change = 0
run_timer = False
gameover_screen()
playerX = 250
playerY = 400
velocity = 3
# movement
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
running = False
menu = True
menuselect = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if bullet_ammo <= 5 and bullet_ammo > -1:
if bullet_state is "ready":
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
ammo()
如果我问了很多问题,我很抱歉。这是我第一次用python编写代码(或者一般的编程),所以我真的不知道大部分的东西是如何工作的。这个代码是我的代码和我的组员的代码的混合,所以这个程序中有一半的代码我不熟悉。提示未来的编码实践是高度赞赏,但我只是想完成我的游戏现在,所以我想完成当前(和最后)的问题,我遇到的。
1条答案
按热度按时间cnwbcb6i1#
正如你所说,你才刚刚开始学习。非常好!你刚刚遇到了一个问题,你可以从中学到很多东西:添加新功能。一般来说,这个问题更适合code review stack exchange,但我认为它也适合这里。
那么到底是什么问题呢?你说你想让你的游戏“回到”主菜单。你提出了一个(我发现)难以遵循的实现,并用一堆代码来遵循,我相信这是工作,但很难(对我来说)阅读。我真的觉得你能写多少东西给人留下了深刻的印象,特别是因为这是你第一次编码!
在我看来,你真实的的问题不是如何“回到过去”,而是如何使你的代码可修复/可更新。这是一个很好的教训。有两个原因导致你当前的代码很难更新:
为什么这是一个问题?如果你想改变一些东西,你找不到在哪里改变什么,因为你找不到任何东西,因为到处都是代码。然后,一旦你找到了,你不可能真正知道所有的东西是什么,因为全局变量。最后,硬编码的值使你不可能知道是否已经修复了所有的东西,或者忘记了一个数字导致了bug(例如,你想把屏幕做两倍大,改变所有的图片大小,但忘记更新速度)。
那么,如何改变这种情况呢?
好吧,首先试着把你的代码分开,这样更容易找到东西。我喜欢把东西放在这样的文件夹里:
这可能看起来令人生畏,所以让我们跳过一切:
主文件夹.py:你运行的主文件。它看起来像这样:
当你运行
main.py
时,它会加载配置,然后运行主要组件。这个文件应该很短,这样就很容易理解发生了什么。例如,在这里,你从运行main_menu()
中获得设置,然后使用这些设置玩游戏。当游戏退出时,主菜单会再次显示。接下来是**.gitignore文件,如果你使用git的话,你会用到它。如果你不知道这是什么,或者你应该谷歌一下,它会让你的生活更轻松。
在你的core文件夹中,你有游戏的所有主要功能。这是你放置大部分代码的地方。把它分成逻辑文件是很重要的,这样你就可以很容易地找到什么放在哪里。
在utils文件夹中,有一些实用程序函数可以在项目之间共享。
在你的config**文件夹里你可以保存设置,它应该包含你使用的所有“数字或字母值”,这样你就不用手动修改代码中的数字(或者忘记一个),而是把它传递沿着。
这解决了你必须找到什么在哪里的问题。所有的东西都排列得很好,你可以很容易地找到它。如果你想改变大的功能(如添加一个新的菜单),你可以在主文件中完成,这很容易做到。
下一个问题是全局变量的使用。你有相当多的全局变量,这使得跟踪哪个值是什么变得很困难。你可能很清楚,但是想象一下,当你在一年后回来,值“随机”变化,你如何找到哪里发生了变化?
相反,尝试像这样将内容传递给函数:
这使得阅读在哪里发生的事情变得容易得多。
最后,如前所述,硬编码的值使所有内容都很难更新。以屏幕尺寸加倍为例。您将所有内容都加倍,但忘记了加倍一个速度组件(因为你忽略了它)。这可能是一个“奇怪的bug”,需要一些时间来修复。相反,尝试将所有这些值保存在你的配置中,然后像上面那样传递它们。看起来像这样:
很抱歉,我不会给你一个复制粘贴的解决方案来解决你的问题。我认为对你来说,自己重新构造你的代码,然后看看改变它的功能是多么容易,这会更有用。
成功!