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

在运行《Python编程从入门到实践》书中的外星人入侵游戏时,遇到game_functions模块中无'check_evnets'属性的错误。错误源于代码中拼写错误,将'check_events'误写为'check_evnets'。解决方法是将所有'check_evnets'更正为'check_events'。
摘要由CSDN通过智能技术生成

使用该书发布的附件中的参考代码在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Python 小游戏,名为“入侵外星人”。这个游戏的目标是射击下降的外星人并得分。游戏有 3 层外星人,每层外星人的移动速度不同,游戏难度逐渐增加。 ```python import pygame import sys from random import randint pygame.init() # 游戏设置 width = 800 height = 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Alien Invasion") clock = pygame.time.Clock() # 颜色设置 white = (255, 255, 255) black = (0, 0, 0) # 字体设置 font = pygame.font.Font(None, 36) # 外星人设置 alien_width = 50 alien_height = 50 alien_speed = 5 alien_color = (randint(0, 255), randint(0, 255), randint(0, 255)) # 玩家设置 player_width = 50 player_height = 50 player_speed = 10 player_color = (0, 255, 0) # 子弹设置 bullet_width = 5 bullet_height = 20 bullet_speed = 10 bullet_color = (255, 0, 0) # 游戏状态 game_over = False score = 0 # 外星人生成函数 def generate_aliens(num_rows, num_cols): aliens = [] for row in range(num_rows): for col in range(num_cols): alien = { "x": col * alien_width, "y": row * alien_height, "color": (randint(0, 255), randint(0, 255), randint(0, 255)) } aliens.append(alien) return aliens # 子弹生成函数 def generate_bullet(player_x, player_y): bullet = { "x": player_x + player_width / 2 - bullet_width / 2, "y": player_y - bullet_height, } return bullet # 碰撞检测函数 def collide(rect1, rect2): if (rect1["x"] < rect2["x"] + rect2["width"] and rect1["x"] + rect1["width"] > rect2["x"] and rect1["y"] < rect2["y"] + rect2["height"] and rect1["y"] + rect1["height"] > rect2["y"]): return True return False # 游戏循环 def game_loop(): global game_over, score # 外星人生成 aliens = generate_aliens(3, 10) # 玩家初始化 player_x = width / 2 - player_width / 2 player_y = height - player_height player_speed_x = 0 # 子弹列表初始化 bullets = [] while not game_over: # 事件循环 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_speed_x = -player_speed elif event.key == pygame.K_RIGHT: player_speed_x = player_speed elif event.key == pygame.K_SPACE: bullet = generate_bullet(player_x, player_y) bullets.append(bullet) elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: player_speed_x = 0 # 碰撞检测 for bullet in bullets: for alien in aliens: if collide(bullet, alien): bullets.remove(bullet) aliens.remove(alien) score += 10 for alien in aliens: if collide(alien, {"x": player_x, "y": player_y, "width": player_width, "height": player_height}): game_over = True # 外星人移动 for alien in aliens: alien["y"] += alien_speed if alien["y"] >= height: game_over = True # 玩家移动 player_x += player_speed_x if player_x < 0: player_x = 0 elif player_x > width - player_width: player_x = width - player_width # 子弹移动 for bullet in bullets: bullet["y"] -= bullet_speed if bullet["y"] < 0: bullets.remove(bullet) # 绘制画面 screen.fill(black) for alien in aliens: pygame.draw.rect(screen, alien["color"], [alien["x"], alien["y"], alien_width, alien_height]) pygame.draw.rect(screen, player_color, [player_x, player_y, player_width, player_height]) for bullet in bullets: pygame.draw.rect(screen, bullet_color, [bullet["x"], bullet["y"], bullet_width, bullet_height]) text = font.render(f"Score: {score}", True, white) screen.blit(text, (10, 10)) pygame.display.update() # 控制游戏速度 clock.tick(30) # 游戏结束 screen.fill(black) text = font.render("Game Over", True, white) screen.blit(text, (width / 2 - 50, height / 2 - 18)) text = font.render(f"Score: {score}", True, white) screen.blit(text, (width / 2 - 50, height / 2 + 18)) pygame.display.update() pygame.time.delay(2000) # 启动游戏 game_loop() ``` 这个游戏使用了 Pygame 库来实现图形界面和事件响应。游戏界面中有外星人、玩家和子弹三种元素,玩家可以通过键盘控制玩家的移动和子弹的射击,同时需要注意外星人的攻击。游戏难度逐步增加,游戏难度越来越高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值