当前位置:   article > 正文

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

traceback (most recent call last): file "f:\python\外星人\alien_invasion.py

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

Traceback (most recent call last):

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

run_game()

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

gf.check_evnets(ship)

AttributeError: module 'game_functions' has no attribute 'check_evnets'

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

alien_invasion.py

import 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

import 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

import 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)

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/121645
推荐阅读
相关标签
  

闽ICP备14008679号