当前位置:   article > 正文

pygame之植物大战僵尸

pygame之植物大战僵尸

代码以及配套资源已上传到百度网盘
链接:https://pan.baidu.com/s/11ICgm7O_30kddvJWGKFEBA
提取码:cz0r

import random
import pygame
import sys
from pygame.locals import *

#生成随机数
#i=random.randint(1,100)
#print(i)

#记录帧数
i=0

def main():
    pygame.init()
    pygame.mixer.init()
    size=width,height=800,602
    screen=pygame.display.set_mode(size)
    pygame.mixer.music.load('植物大战僵尸背景音乐.mp3')
    pygame.mixer.music.set_volume(0.2)
    pygame.mixer.music.play(loops=-1)
    fire_sound=pygame.mixer.Sound('射击音效.wav')
    fire_sound.set_volume(0.2)


    bg=pygame.image.load('植物大战僵尸背景图.jpg')
    bgposition=bg.get_rect()

    pygame.display.set_caption('植物大战僵尸!')

    group=pygame.sprite.Group()

    while True:

        img=pygame.image.load('豌豆射手.png')
        img_position=img.get_rect().center=40,150

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
        screen.blit(bg,bgposition)
        screen.blit(img,img_position)

        key=pygame.key.get_pressed()
        if key[K_f]:
            fire_sound.play(loops=0)
            group.add(Bullet())
            continue

        for bullet in group.sprites():
            bullet.move()
            screen.blit(bullet.img,bullet.rect)
        pygame.display.flip()
        pygame.time.Clock().tick(60)

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        position=[122,185]
        speed=[3,0]
        self.img=pygame.image.load('豌豆射手的炮弹.png')
        self.rect=self.img.get_rect()
        self.rect.center=position
        self.speed=speed
    def move(self):
        self.rect=self.rect.move(self.speed)

main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/907223
推荐阅读
相关标签
  

闽ICP备14008679号