我开始用pygame制作一个躲避游戏,我差不多完成但是有一个问题。所以有这些流星我必须在太空躲闪,如果我触碰它我崩溃但是当我落后其中一个流星我也崩溃了。 继承我的代码:
import pygame
import os
import random
import time
from pygame import mixer
pygame.init()
mixer.init()
width = 800
height = 600
black = (0,0,0)
white = (255,255,255)
ship_width = 56
ship_height = 64
disp = pygame.display.set_mode((width,height))
pygame.display.set_caption("space_game")
clock = pygame.time.Clock()
background = pygame.image.load(os.path.join("Backgrounds", "Space.png"))
Ship00 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship00.png"))
Ship01 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship01.png"))
Ship02 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship02.png"))
Ship03 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship03.png"))
Ship04 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship04.png"))
Ship05 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship05.png"))
Ship06 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship06.png"))
Ship07 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship07.png"))
Ship08 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship08.png"))
Ship09 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship09.png"))
Ship10 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship10.png"))
Ship11 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship11.png"))
Ship12 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship12.png"))
Ship13 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship13.png"))
Ship14 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship14.png"))
Ship15 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship15.png"))
Ship16 = pygame.image.load(os.path.join("Images", "Animations", "Ship Animation", "Ship16.png"))
Meteor00 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor00.png"))
Meteor01 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor01.png"))
Meteor02 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor02.png"))
Meteor03 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor03.png"))
Meteor04 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor04.png"))
Meteor05 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor05.png"))
Meteor06 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor06.png"))
Meteor07 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor07.png"))
Meteor08 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor08.png"))
Meteor09 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor09.png"))
Meteor10 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor10.png"))
Meteor11 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor11.png"))
Meteor12 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor12.png"))
Meteor13 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor13.png"))
Meteor14 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor14.png"))
Meteor15 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor15.png"))
Meteor16 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor16.png"))
Meteor17 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor17.png"))
Meteor18 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor18.png"))
Meteor19 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor19.png"))
Meteor20 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor20.png"))
Meteor21 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor21.png"))
Meteor22 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor22.png"))
Meteor23 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor23.png"))
Meteor24 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor24.png"))
Meteor25 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor25.png"))
Meteor26 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor26.png"))
Meteor27 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor27.png"))
Meteor28 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor28.png"))
Meteor29 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor29.png"))
Meteor30 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor30.png"))
Meteor31 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor31.png"))
Meteor32 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor32.png"))
Meteor33 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor33.png"))
Meteor34 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor34.png"))
Meteor35 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor35.png"))
Meteor36 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor36.png"))
Meteor37 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor37.png"))
Meteor38 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor38.png"))
Meteor39 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor39.png"))
Meteor40 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor40.png"))
Meteor41 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor41.png"))
Meteor42 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor42.png"))
Meteor43 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor43.png"))
Meteor44 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor44.png"))
Meteor45 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor45.png"))
Meteor46 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor46.png"))
Meteor47 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor47.png"))
Meteor48 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor48.png"))
Meteor49 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor49.png"))
Meteor50 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor50.png"))
Meteor51 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor51.png"))
Meteor52 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor52.png"))
Meteor53 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor53.png"))
Meteor54 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor54.png"))
Meteor55 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor55.png"))
Meteor56 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor56.png"))
Meteor57 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor57.png"))
Meteor58 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor58.png"))
Meteor59 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor59.png"))
Meteor60 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor60.png"))
Meteor61 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor61.png"))
Meteor62 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor62.png"))
Meteor63 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor63.png"))
Meteor64 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor64.png"))
Meteor65 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor65.png"))
Meteor66 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor66.png"))
Meteor67 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor67.png"))
Meteor68 = pygame.image.load(os.path.join("Images", "Animations", "Meteor Animation", "Meteor68.png"))
bulletImg = pygame.image.load(os.path.join("Images", "Bullet.png"))
shipImages = [Ship00, Ship01, Ship02, Ship03, Ship04, Ship05, Ship06, Ship07, Ship08, Ship09, Ship10, Ship11, Ship12, Ship13, Ship14, Ship15, Ship16]
meteorImages = [Meteor00 , Meteor01 , Meteor02 , Meteor03 , Meteor04 , Meteor05 , Meteor06 , Meteor07 , Meteor08 , Meteor09 , Meteor11 , Meteor12 , Meteor13 , Meteor14 , Meteor15 , Meteor16 , Meteor17 , Meteor18 , Meteor19 , Meteor20 , Meteor21 , Meteor22 , Meteor23 , Meteor24 , Meteor25 , Meteor26 , Meteor27 , Meteor28 , Meteor29 , Meteor30 , Meteor31 , Meteor32 , Meteor33 , Meteor34 , Meteor35 , Meteor36 , Meteor37 , Meteor38 , Meteor39 , Meteor40 , Meteor41 , Meteor42 , Meteor43 , Meteor44 , Meteor45 , Meteor46 , Meteor47 , Meteor48 , Meteor49 , Meteor50 , Meteor51 , Meteor52 , Meteor53 , Meteor54 , Meteor55 , Meteor56 , Meteor57 , Meteor58 , Meteor59 , Meteor60 , Meteor61 , Meteor62 , Meteor63 , Meteor64 , Meteor65 , Meteor66 , Meteor67 , Meteor68]
def score(count):
font = pygame.font.Font("Pixeled.ttf", 10)
text = font.render("Score: " + str(count), True, white)
disp.blit(text,(0,0))
def text_obj(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
game_over_txt = pygame.font.Font("Pixeled.ttf", 50)
TextSurf, TextRect = text_obj(text, game_over_txt)
TextRect.center = (400,300)
disp.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
gameLoop()
def game_over():
message_display("Game Over")
def gameLoop():
x = (width * 0.45)
y = (height * 0.8)
x_ch = 0
y_ch = 0
x_bg = 0
mw = 92
mh = 88
mspeed = 5
index_s = 0
index_m = 0
ms_x = random.randrange(0, width)
ms_y = -500
mscore = 0
music = mixer.music.load(os.path.join("Sounds", "Music", "space_song2.wav"))
mixer.music.play(-1)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == ord("a"):
x_ch = -5
elif event.key == ord("d"):
x_ch = 5
elif event.key == ord("w"):
y_ch = -5
elif event.key == ord("s"):
y_ch = 5
if event.type == pygame.KEYUP:
if event.key == ord("a") or event.key == ord("d"):
x_ch = 0
if event.key == ord("w") or event.key == ord("s"):
y_ch = 0
x += x_ch
y += y_ch
if x > width - ship_width or x < 0:
x_ch = 0
if y > height - ship_height or y < 0:
y_ch = 0
disp.fill(black)
x_loop = x_bg % background.get_rect().height
disp.blit(background, (0, x_loop - background.get_rect().height))
if x_loop < height:
disp.blit(background, (0, x_loop))
x_bg += 5
index_s += 1
index_s %= len(shipImages)
current_shpImg = shipImages[index_s]
disp.blit(current_shpImg, (x,y))
index_m += 1
index_m %= len(meteorImages)
current_metImg = meteorImages[index_m]
disp.blit(current_metImg, (ms_x,ms_y))
ms_y += mspeed
score(mscore)
if ms_y > height:
ms_y = 0 - mh
ms_x = random.randrange(10,790)
mscore += 1
mspeed += 0.2
pygame.mixer.Channel(0).play(pygame.mixer.Sound('Sounds\Effects\scoreup.wav'))
if y < ms_y + mh:
if x > ms_x and x < ms_x + mw or x + ship_width > ms_x and x + ship_width < ms_x + mw:
crash_sound = mixer.music.load(os.path.join("Sounds", "Effects", "crash_sound.wav"))
mixer.music.play()
game_over()
if event.type == pygame.KEYDOWN:
if event.key == ord(" "):
disp.blit(bulletImg, (x-16, y-16))
disp.blit(bulletImg, (x+16, y-16))
pygame.display.flip()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
我一直试图在过去的半小时内解决这个问题而且我无法做到这一点。 有人可以帮我解决这个问题吗?感谢
答案 0 :(得分:0)
使用:
if y < ms_y + mh:
# [...]
您只定义了当船在流星上方时的坠机,因此危险区域是船舶下方/后方的整个区域。
要解决此问题,您需要限制此危险区域:
if y < ms_y + mh and y > ms_y:
# [...]
您应该像使用x坐标测试一样使用船舶的高度,以便更准确。