python外星人入侵游戏图片_《python编程从入门到实践》书中的外星人入侵小游戏报错...

使用该书发布的附件中的参考代码在Pyhton3.7 Pycharm2019.2.4(最新版本)的环境下运行,出现报错如下:

Traceback (most recent call last):

File “F:/python_work/AI/Alien_Invasion.py”, line 25, in

1run_game()

File “F:/python_work/AI/Alien_Invasion.py”, line 21, in run_game

1gf.check_evnets(ship)

AttributeError: module ‘game_functions’ has no attribute ‘check_evnets’

使用的相关具体代码如下:(并没有贴settings.py和bullet.py)

alien_invasion.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31import pygame

from pygame.sprite import Group

from settings import Settings

from ship import Ship

import game_functions as gf

def run_game():

# Initialize pygame, settings, and screen object.

pygame.init()

ai_settings = Settings()

screen = pygame.display.set_mode(

(ai_settings.screen_width, ai_settings.screen_height))

pygame.display.set_caption("Alien Invasion")

# Set the background color.

bg_color = (230, 230, 230)

# Make a ship.

ship = Ship(ai_settings, screen)

# Make a group to store bullets in.

bullets = Group()

# Start the main loop for the game.

while True:

gf.check_events(ai_settings, screen, ship, bullets)

ship.update()

gf.update_bullets(bullets)

gf.update_screen(ai_settings, screen, ship, bullets)

run_game()

game_functions.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67import sys

import pygame

from bullet import Bullet

def check_keydown_events(event, ai_settings, screen, ship, bullets):

"""Respond to keypresses."""

if event.key == pygame.K_RIGHT:

ship.moving_right = True

elif event.key == pygame.K_LEFT:

ship.moving_left = True

elif event.key == pygame.K_SPACE:

fire_bullet(ai_settings, screen, ship, bullets)

def check_keyup_events(event, ship):

"""Respond to key releases."""

if event.key == pygame.K_RIGHT:

ship.moving_right = False

elif event.key == pygame.K_LEFT:

ship.moving_left = False

def check_events(ai_settings, screen, ship, bullets):

"""Respond to keypresses and mouse events."""

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

elif event.type == pygame.KEYDOWN:

check_keydown_events(event, ai_settings, screen, ship, bullets)

elif event.type == pygame.KEYUP:

check_keyup_events(event, ship)

def fire_bullet(ai_settings, screen, ship, bullets):

"""Fire a bullet, if limit not reached yet."""

# Create a new bullet, add to bullets group.

if len(bullets) < ai_settings.bullets_allowed:

new_bullet = Bullet(ai_settings, screen, ship)

bullets.add(new_bullet)

def update_screen(ai_settings, screen, ship, bullets):

"""Update images on the screen, and flip to the new screen."""

# Redraw the screen, each pass through the loop.

screen.fill(ai_settings.bg_color)

# Redraw all bullets, behind ship and aliens.

for bullet in bullets.sprites():

bullet.draw_bullet()

ship.blitme()

# Make the most recently drawn screen visible.

pygame.display.flip()

def update_bullets(bullets):

"""Update position of bullets, and get rid of old bullets."""

# Update bullet positions.

bullets.update()

# Get rid of bullets that have disappeared.

for bullet in bullets.copy():

if bullet.rect.bottom <= 0:

bullets.remove(bullet)

ship.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39import pygame

class Ship():

def __init__(self, ai_settings, screen):

"""Initialize the ship, and set its starting position."""

self.screen = screen

self.ai_settings = ai_settings

# Load the ship image, and get its rect.

self.image = pygame.image.load('images/ship.bmp')

self.rect = self.image.get_rect()

self.screen_rect = screen.get_rect()

# Start each new ship at the bottom center of the screen.

self.rect.centerx = self.screen_rect.centerx

self.rect.bottom = self.screen_rect.bottom

# Store a decimal value for the ship's center.

self.center = float(self.rect.centerx)

# Movement flags.

self.moving_right = False

self.moving_left = False

def update(self):

"""Update the ship's position, based on movement flags."""

# Update the ship's center value, not the rect.

if self.moving_right and self.rect.right < self.screen_rect.right:

self.center += self.ai_settings.ship_speed_factor

if self.moving_left and self.rect.left > 0:

self.center -= self.ai_settings.ship_speed_factor

# Update rect object from self.center.

self.rect.centerx = self.center

def blitme(self):

"""Draw the ship at its current location."""

self.screen.blit(self.image, self.rect)

请问各位这个问题该怎么解决?感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值