Commit 922654d7 authored by 13680905's avatar 13680905
Browse files

Upload New File

parent b4e21983
import pygame
from planes_sprites import *
pygame.init()
#创建游戏窗口大小
screen=pygame.display.set_mode((480,700))
#创建背景图像
bg = pygame.image.load("./images/background.png")
screen.blit(bg,(0,0))
pygame.display.update()
#创建英雄飞机
hero = pygame.image.load("./images/me1.png")
hero2 = pygame.image.load("./images/me2.png")
screen.blit(hero,(150,500))
hero_rect=pygame.Rect(150,500,102,126)
#在所有绘制工作完成后,统一调用update方法
pygame.display.update()
#创建时钟对象
clock = pygame.time.Clock()
i=0
l=0
#建立精灵组和精灵
enemy=GameSprite("./images/enemy1.png")#创建敌机
enemy2=GameSprite("./images/enemy2.png",2)#创建敌机2
enemy_group=pygame.sprite.Group(enemy,enemy2)
#创建游戏内循环
p=0#控制水平控制的可控变量
o=2#控制垂直控制的可控变量
while True:
#1.创建动画的英雄飞机
if i ==1:
k=hero2
i=0
else:
k=hero
i=1
clock.tick(60)
for event in pygame.event.get():
print(event)
#英雄飞机的水平运动控制
if event.type==pygame.QUIT:
print("游戏退出")
if event.type==pygame.KEYDOWN:
if event.key==276:
print("英雄飞机向左移动")
p=-o
if event.key == 275:
print("英雄飞机向左移动")
p=o
if event.type==pygame.KEYUP:
if p==o:
if event.key==275:
p=0
if p==-o:
if event.key == 276:
p = 0
#英雄飞机的垂直运动控制
if event.type == pygame.KEYDOWN:
if event.key == 273:
print("英雄飞机向上移动")
l = -o
if event.key == 274:
print("英雄飞机向下移动")
l = o
if event.type == pygame.KEYUP:
if l == o:
if event.key == 274:
l = 0
if l == -o:
if event.key == 273:
l = 0
hero_rect.x +=p
hero_rect.y +=l
screen.blit(bg, (0, 0))
if enemy.rect.y >700:
print("5555")
enemy.rect.y=0
elif enemy2.rect.y > 700:
enemy2.rect.y =0
# 创建游戏内逻辑边界
if hero_rect.y < -102:
hero_rect = pygame.Rect(150, 700, 102, 126)
#检测碰撞效果
if enemy.rect.y >hero_rect.y or enemy2.rect.y >hero_rect.y:
print("碰撞")
if pygame.sprite.collide_mask(hero, enemy):
print("Collision occurred")
enemy_group.update()
enemy_group.draw(screen)
screen.blit(k, hero_rect)
pygame.display.update()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment