pygame event问题
import pygameimport sys
from pygame.locals import *
import traceback
import myplane
def main():
pygame.init()
pygame.mixer.init()
running=True
clock=pygame.time.Clock()
bg_size=width,height=480,700
screen=pygame.display.set_mode(bg_size)
pygame.display.set_caption('The battle of the plane')
bg_image=pygame.image.load('./images/background.png').convert_alpha()
#载入音乐
pygame.mixer.music.load('./sound/game_music.ogg')
bullet_sound=pygame.mixer.Sound('./sound/bullet.wav')
bullet_sound.set_volume(0.2)
button_sound=pygame.mixer.Sound('./sound/button.wav')
button_sound.set_volume(0.2)
enemy1_down=pygame.mixer.Sound('./sound/enemy1_down.wav')
enemy1_down.set_volume(0.2)
enemy2_down=pygame.mixer.Sound('./sound/enemy2_down.wav')
enemy2_down.set_volume(0.2)
enemy3_down=pygame.mixer.Sound('./sound/enemy3_down.wav')
enemy3_down.set_volume(0.2)
enemy3_flying=pygame.mixer.Sound('./sound/enemy3_flying.wav')
enemy3_flying.set_volume(0.2)
get_bomb=pygame.mixer.Sound('./sound/get_bomb.wav')
get_bomb.set_volume(0.2)
get_bullet=pygame.mixer.Sound('./sound/get_bullet.wav')
get_bullet.set_volume(0.2)
me_down=pygame.mixer.Sound('./sound/me_down.wav')
me_down.set_volume(0.2)
supply=pygame.mixer.Sound('./sound/supply.wav')
supply.set_volume(0.2)
upgrade=pygame.mixer.Sound('./sound/upgrade.wav')
upgrade.set_volume(0.2)
use_bomb=pygame.mixer.Sound('./sound/use_bomb.wav')
use_bomb.set_volume(0.2)
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.2)
pygame.key.set_repeat(100,100)
#创建自己的飞机
me=myplane.Myplane(bg_size)
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN:
if event.key==K_a:
me.move((-10,0))
if event.key==K_d:
me.move((10,0))
if event.key==K_w:
me.move((0,-10))
if event.key==K_s:
me.move((0,10))
screen.blit(bg_image,(0,0))
screen.blit(me.image1,me.rect)
pygame.display.flip()
clock.tick(60)
if __name__=='__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
import pygame
class Myplane:
def __init__(self,bg_size):
self.image1=pygame.image.load('./images/me1.png').convert_alpha()
self.image2=pygame.image.load('./images/me2.png').convert_alpha()
self.rect=self.image1.get_rect()
self.bg_size=bg_size
self.rect.left,self.rect.top=(self.bg_size-self.rect.width)//2,self.bg_size-self.rect.height
self.destroy1=pygame.image.load('./images/me_destroy_1.png').convert_alpha()
self.destroy2=pygame.image.load('./images/me_destroy_2.png').convert_alpha()
self.destroy3=pygame.image.load('./images/me_destroy_3.png').convert_alpha()
self.destroy4=pygame.image.load('./images/me_destroy_4.png').convert_alpha()
def move(self,speed):
self.rect=self.rect.move(speed)
if self.rect.left<0:
self.rect.left=0
if self.rect.right>self.bg_size:
self.rect.right=self.bg_size
if self.rect.top<0:
self.rect.top=0
if self.rect.bottom>self.bg_size:
self.rect.bottom=self.bg_size
在跟着小甲鱼学打飞机,{:10_297:} ,但是飞机只能朝一个方向飞,有没有办法能让飞机斜着飞的办法啊,求解 这个,我也不知道。 可以!
楼上瞎说!
只要移动的时候位置加在另外一个角度就行了 wuqramy 发表于 2020-8-4 16:43
可以!
楼上瞎说!
只要移动的时候位置加在另外一个角度就行了
你这个手有点残啊
页:
[1]