Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 我想和pygame玩一个躲避游戏_Python_Python 3.x_Pygame - Fatal编程技术网

Python 我想和pygame玩一个躲避游戏

Python 我想和pygame玩一个躲避游戏,python,python-3.x,pygame,Python,Python 3.x,Pygame,我已经开始用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 = (

我已经开始用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()
导入pygame
导入操作系统
随机输入
导入时间
从pygame导入混合器
pygame.init()
mixer.init()
宽度=800
高度=600
黑色=(0,0,0)
白色=(255255)
船宽=56
船舶高度=64
disp=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“空格_游戏”)
clock=pygame.time.clock()
background=pygame.image.load(os.path.join(“Backgrounds”,“Space.png”))
Ship00=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship00.png”))
Ship01=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship01.png”))
Ship02=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship02.png”))
Ship03=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship03.png”))
Ship04=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship04.png”))
Ship05=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship05.png”))
Ship06=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship06.png”))
Ship07=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship07.png”))
Ship08=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship08.png”))
Ship09=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship09.png”))
Ship10=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship10.png”))
Ship11=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship11.png”))
Ship12=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship12.png”))
Ship13=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship13.png”))
Ship14=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship14.png”))
Ship15=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship15.png”))
Ship16=pygame.image.load(os.path.join(“图像”、“动画”、“船舶动画”、“Ship16.png”))
Meteor00=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor00.png”))
Meteor01=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor01.png”))
Meteor02=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor02.png”))
Meteor03=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor03.png”))
Meteor04=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor04.png”))
Meteor05=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor05.png”))
Meteor06=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor06.png”))
Meteor07=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor07.png”))
Meteor08=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor08.png”))
Meteor09=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor09.png”))
Meteor10=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor10.png”))
Meteor11=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor11.png”))
Meteor12=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor12.png”))
Meteor13=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor13.png”))
Meteor14=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor14.png”))
Meteor15=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor15.png”))
Meteor16=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor16.png”))
Meteor17=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor17.png”))
Meteor18=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor18.png”))
Meteor19=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor19.png”))
Meteor20=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor20.png”))
Meteor21=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor21.png”))
Meteor22=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor22.png”))
Meteor23=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor23.png”))
Meteor24=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor24.png”))
Meteor25=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor25.png”))
Meteor26=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor26.png”))
Meteor27=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor27.png”))
Meteor28=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor28.png”))
Meteor29=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor29.png”))
Meteor30=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor30.png”))
Meteor31=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor31.png”))
Meteor32=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor32.png”))
Meteor33=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor33.png”))
Meteor34=pygame.image.load(os.path.join(“图像”、“动画”、“流星动画”、“Meteor34.png”))
Meteor35=pygame.image.load(os.path.join(“图像”,“动画”)
if y < ms_y + mh:
    # [...]
if y < ms_y + mh and y > ms_y:
    # [...]