请问老师为什么无法通过点击进入playing状态?
import sys
import pygame
import constants
from game.plane import OurPlane
def main():
'''游戏入口'''
#初始化pygame
READY = 0
PLAYING = 1
status =READY
frame = 0
pygame.init()
width , height = 480 , 852 #(根据图片尺寸获得)
#屏幕对象
screen = pygame.display.set_mode((width , height))
pygame.display.set_caption('飞机大战')
#背景图片
bg = pygame.image.load(constants.BG_IMG)
img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
img_game_title_rect = img_game_title.get_rect()
t_width, t_height = img_game_title.get_size()
img_game_title_rect.topleft = (int((width - t_width) / 2),
(int(height - t_height) / 2))
btn_start= pygame.image.load(constants.IMG_GAME_START_BTN)
btn_start_rect = btn_start.get_rect()
btn_width, btn_height = btn_start.get_size()
btn_start_rect.topleft = (int((width - btn_width) / 2),
(int(height / 2 + 2 * btn_height)))
pygame.mixer.music.load(constants.BG_MUSIC)
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.8)
our_plane = OurPlane
clock = pygame.time.Clock()
while True:
# 监听事件
clock.tick(60)
frame += 1
if frame >= 60:
frame = 0
for event in pygame.event.get():
#退出游戏
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if status == READY:
status == PLAYING
elif event.type == pygame.KEYDOWN:
if status == PLAYING:
if event.key == pygame.K_w or event.key == pygame.K_UP:
our_plane.move_up()
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
our_plane.move_down()
elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
our_plane.move_left()
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
our_plane.move_right()
elif event.key == pygame.K_SPACE:
our_plane.shoot()
if status == READY:
screen.blit(bg, bg.get_rect())
screen.blit(img_game_title, img_game_title_rect)
screen.blit(btn_start, btn_start_rect)
elif status == PLAYING:
screen.blit(bg,bg.get_rect())
our_plane.update()
our_plane.bullets.update()
pygame.display.flip()
if __name__ == "__main__":
main()
正在回答
同学,你好。
1、status在赋值时应该是等于号
2、在创建OurPlane()实例时,应该添加括号,并传入相应的参数
3、同学代码中还有其他的问题,同学根据相应的报错提示进行修改即可
如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~
Plane代码如下
————————
import pygame
import constants
from game.bullet import Bullet
class Plane(pygame.sprite.Sprite):
#飞机图片
plane_images = []
#存活状态
active = True
#子弹发射精灵组
bullets = pygame.sprite.Group()
"""飞机基类"""
def __init__(self,screen,speed=None):
super().__init__()
self.screen = screen
self.speed = speed or 10
self.img_list = []
self.rect = self.img_list[0].get_rect()
self.plane_w, self.plane_h = self.img_list[0].get_size()
self.width, self.height = self.screen.get_size()
self.rect.left = int((self.width - self.plane_w) / 2)
self.rect.top = int((self.height /2))
def load_src(self):
for img in self.plane_images:
self.img_list.append(pygame.image.load(img))
@property
def image(self):
return self.img_list[0]
def blit_me(self):
self.screen.blit(self.image,self.rect)
def move_up(self):
self.rect.top -= self.speed
def move_down(self):
self.rect.top += self.speed
def move_left(self):
self.rect.left -= self.speed
def move_right(self):
self.rect.left += self.speed
def shoot(self):
bullet =Bullet(self.screen,self,15)
self.bullets.add(bullet)
class OurPlane(Plane):
plane_images = constants.OUR_PLANE_IMG_LIST
def update(self, main):
if main.frame %5 :
self.screen.blit(self.plane_images[0],self.rect)
else:
self.screen.blit(self.plane_images[1],self.rect)
def move_up(self):
super().move_up()
if self.rect.top <= 0:
self.rect.top = 0
def move_down(self):
super().move_down()
if self.rect.top >= self.height - self.plane_h:
self.rect.top = self.height - self.plane_h
def move_left(self):
super().move_left()
if self.rect.left <=0:
self.rect.left = 0
def move_right(self):
super().move_right()
if self.rect.left >= self.width - self.plane_w:
self.rect.left = self.width - self.plane_w
- 参与学习 人
- 提交作业 2727 份
- 解答问题 8160 个
想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星