我在PyGame中创建了两个简单的精灵,其中一个是伞,另一个是雨滴。
雨滴被添加到名为all_sprites的精灵组中。伞精灵有自己的组,叫做Umbrella_sprite
雨滴从屏幕顶部”落下”,如果其中一个碰到雨伞/与之相撞。。雨滴应该被删除。但不是特定的雨滴,所有其他雨滴都会受到影响。
主文件(雨.py)在#!/usr/bin/python
VERSION = “0.1”
import os, sys, raindrop
from os import path
try:
import pygame
from pygame.locals import *
except ImportError, err:
print ‘Could not load module %s’ % (err)
sys.exit(2)
main variables
WIDTH, HEIGHT, FPS = 300, 300, 30
initialize game
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption(“Rain and Rain”)
background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((40,44,52))
blitting
screen.blit(background,(0,0))
pygame.display.flip()
clock for FPS settings
clock = pygame.time.Clock()
def main():
all_sprites = pygame.sprite.Group()
umbrella_sprite = pygame.sprite.Group()
a function to create new drops
def newDrop():
nd = raindrop.Raindrop()
all_sprites.add(nd)
creating 10 rain drops
for x in range(0,9): newDrop()
variable for main loop
running = True
init umbrella
umb = raindrop.Umbrella()
all_sprites.add(umb)
umbrella_sprite.add(umb)
event loop
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for enemy in all_sprites:
gets_hit = pygame.sprite.spritecollideany(umb, all_sprites)
if gets_hit:
all_sprites.remove(enemy)
screen.blit(background,(100,100))
clear
all_sprites.clear(screen,background)
umbrella_sprite.clear(screen,background)
update
all_sprites.update()
umbrella_sprite.update()
draw
all_sprites.draw(screen)
umbrella_sprite.draw(screen)
flip the table
pygame.display.flip()
pygame.quit()
if name == ‘main‘:
main()
在雨滴.py(雨滴和雨伞)
^{pr2}$
Original: https://blog.csdn.net/weixin_35959694/article/details/114455619
Author: Parker Sept
Title: python精灵和精灵组_游戏精灵与精灵组碰撞
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783124/
转载文章受原作者版权保护。转载请注明原作者出处!