|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#目前只是实现将飞机显示到屏幕上,并且想要通过w,s,a,d按键来控制移动方向
import sys
import traceback
from pygame.locals import *
import pygame
pygame.init()
pygame.mixer.init()
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
class Myplane(pygame.sprite.Sprite):
def __init__(self,myplane_1_image,bg_size):
pygame.sprite.Sprite.__init__(self) #继承类
self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
self.myplane_1_rect = self.myplane_1_image.get_rect()
self.myplane_1_rect.left,self.myplane_1_rect.top = \
(bg_size[0] - self.myplane_1_rect.width) // 2 ,\
bg_size[1] - self.myplane_1_rect.height
self.screen_width,self.screen_height = bg_size[0],bg_size[1]
speed = 0
def move(self):
#如果按下W,A,S,D,表示飞机分别向上下左右移动
press_can_K_w = True
press_can_K_s = True
press_can_K_a = True
press_can_K_d = True
if self.myplane_1_rect.left < 0 : #超过左边界,键盘a按下无效
press_can_K_a = False
if self.myplane_1_rect.left > self.screen_width - self.myplane_1_rect.width :
press_can_K_d = False
if self.myplane_1_rect.top < 0 :
press_can_K_w = False
if self.myplane_1_rect.top> self.screen_height - self.myplane_1_rect.height:
press_can_K_d = False
def main():
pygame.init()
myplane_1_image = "image/me1.png"
bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("image/background.png").convert()
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
pygame.mixer.music.play(-1) #无限循环背景
clock = pygame.time.Clock()
pygame.key.set_repeat(100,100)
myplane = Myplane(myplane_1_image,bg_size)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
myplane.move()
if event.type == K_w and press_can_K_w == True :
speed[1] = -1
if event.type == K_s and press_can_K_s == True :
speed[1] = 1
if event.type == K_a and press_can_K_a == True :
speed[1] = -1
if event.type == K_d and press_can_K_d == True :
speed[1] = 1
screen.blit(background,(0,0))
screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)
pygame.display.flip()
clock.tick(60)
if __name__ =="__main__":
#在双击打开文件时,如果出现异常,将异常显示出来
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
按你的想法改好了,问题主要有几个:
一、你对边界的限制的几个变量应该作为属性放在__init__下面,并且和self绑定,下面对应的方法下的同名变量也改为同样的名字。
二、主函数main中的rect在Myplane类中叫做myplane_1_rect,所以没有rect这个属性报错。
三、rect.center是一个坐标元组,无法被修改,所以你应该改成按键改变left,top这些可修改的量。
四、运行的时候你应该就发现了,飞机的移动非常不流畅,这就是事件中操作移动带来的问题了,所以建议把整个代码改成小甲鱼课程里用的
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
pass
if key_pressed[K_s] or key_pressed[K_DOWN]:
pass
if key_pressed[K_a] or key_pressed[K_LEFT]:
pass
if key_pressed[K_d] or key_pressed[K_RIGHT]:
pass
这种类型,具体的代码自己思考一下吧。
import sys
import traceback
from pygame.locals import *
import pygame
pygame.init()
pygame.mixer.init()
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
class Myplane(pygame.sprite.Sprite):
def __init__(self,myplane_1_image,bg_size):
pygame.sprite.Sprite.__init__(self) #继承类
self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
self.myplane_1_rect = self.myplane_1_image.get_rect()
self.myplane_1_rect.left,self.myplane_1_rect.top = \
(bg_size[0] - self.myplane_1_rect.width) // 2 ,\
bg_size[1] - self.myplane_1_rect.height
self.screen_width,self.screen_height = bg_size[0],bg_size[1]
self.speed = [0,0]
#如果按下W,A,S,D,表示飞机分别向上下左右移动
self.press_can_K_w = True
self.press_can_K_s = True
self.press_can_K_a = True
self.press_can_K_d = True
def move(self):
if self.myplane_1_rect.left < 0 : #超过左边界,键盘a按下无效
self.press_can_K_a = False
elif self.myplane_1_rect.left > self.screen_width - self.myplane_1_rect.width :
self.press_can_K_d = False
elif self.myplane_1_rect.top < 0 :
self.press_can_K_w = False
elif self.myplane_1_rect.top> self.screen_height - self.myplane_1_rect.height:
self.press_can_K_d = False
else:
self.press_can_K_w = True
self.press_can_K_s = True
self.press_can_K_a = True
self.press_can_K_d = True
def main():
pygame.init()
myplane_1_image = "images/me1.png"
bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("images/background.png").convert()
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
pygame.mixer.music.play(-1) #无限循环背景
clock = pygame.time.Clock()
pygame.key.set_repeat(100,100)
myplane = Myplane(myplane_1_image,bg_size)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
myplane.move()
if event.key == K_w and myplane.press_can_K_w == True :
myplane.myplane_1_rect.top -= 10
if event.key == K_s and myplane.press_can_K_s == True :
myplane.myplane_1_rect.top += 10
if event.key == K_a and myplane.press_can_K_a == True :
myplane.myplane_1_rect.left -= 10
if event.key == K_d and myplane.press_can_K_d == True :
myplane.myplane_1_rect.left += 10
screen.blit(background,(0,0))
screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)
pygame.display.flip()
clock.tick(60)
if __name__ =="__main__":
#在双击打开文件时,如果出现异常,将异常显示出来
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
|
|