国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python 中出現(xiàn)AttributeError: ‘Event‘ object has no attribute ‘key‘

這篇具有很好參考價(jià)值的文章主要介紹了Python 中出現(xiàn)AttributeError: ‘Event‘ object has no attribute ‘key‘。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

《python編程從入門到實(shí)踐》中在學(xué)習(xí)外星人入侵項(xiàng)目中運(yùn)行程序時(shí)出現(xiàn)報(bào)錯(cuò) AttributeError: 'Event' object has no attribute 'key'

錯(cuò)誤代碼如下:

# coding = utf-8

import sys
import pygame
from settings import Settings
from ship import Ship

class AlienInvasion:
    """管理游戲資源與行為的類"""

    def __init__(self):
        """初始化游戲并創(chuàng)建游戲資源"""
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)

    def run_game(self):
        """開始游戲的主循環(huán)"""
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()

    def _check_events(self):
        """響應(yīng)按鍵和鼠標(biāo)事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:  
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)‘

            #按Q鍵退出游戲
            elif event.key == pygame.K_q:
                sys.exit()

    def _check_keydown_events(self,event):
        """響應(yīng)按鍵"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True

    def _check_keyup_events(self,event):
        """響應(yīng)松開"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False
    
    def _update_screen(self):
        """更新屏幕上的圖像,并切換到新屏幕"""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()

        pygame.display.flip()

if __name__ == "__main__":
    #創(chuàng)建游戲?qū)嵗⑦\(yùn)行游戲
    ai = AlienInvasion()
    ai.run_game()

運(yùn)行錯(cuò)誤提示

C:\Users\魚語(yǔ)雨\Desktop\python\python\alien_invasion>python alien_invasion.py
pygame 2.1.2 (SDL 2.0.18, Python 3.10.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\魚語(yǔ)雨\Desktop\python\python\alien_invasion\alien_invasion.py", line 69, in <module>
    ai.run_game()
  File "C:\Users\魚語(yǔ)雨\Desktop\python\python\alien_invasion\alien_invasion.py", line 25, in run_game
    self._check_events()
  File "C:\Users\魚語(yǔ)雨\Desktop\python\python\alien_invasion\alien_invasion.py", line 39, in _check_events
    elif event.key == pygame.K_q:
AttributeError: 'Event' object has no attribute 'key'

導(dǎo)致錯(cuò)誤的原因?yàn)椤?#按Q鍵退出游戲”這部分程序中“elif event.key == pygame.K_q:”這句語(yǔ)句寫在了與 事件類型 “event.type == pygame.KEYDOWN:” 并列的位置上。

“KEYDOWN”和“KEYUP”作為 鍵盤事件,其提供了一個(gè)成員屬性key,通過該屬性來(lái)獲取本次鍵盤事件的內(nèi)容,及按下了那個(gè)按鍵,所以“event.key”不能與“event.type”處于并列位置。

使用pygame處理事件時(shí),邏輯一般是先判斷事件類型,然后在根據(jù)不同的事件操作,執(zhí)行不同的游戲操作,及先判斷事件類型,再判斷事件內(nèi)容。

正確代碼如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-503653.html

# coding = utf-8

import sys
import pygame
from settings import Settings
from ship import Ship

class AlienInvasion:
    """管理游戲資源與行為的類"""

    def __init__(self):
        """初始化游戲并創(chuàng)建游戲資源"""
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)

    def run_game(self):
        """開始游戲的主循環(huán)"""
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()

    def _check_events(self):
        """響應(yīng)按鍵和鼠標(biāo)事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:  
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self,event):
        """響應(yīng)按鍵"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        #按Q鍵退出游戲
        elif event.key == pygame.K_q:
            sys.exit()

    def _check_keyup_events(self,event):
        """響應(yīng)松開"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False
    
    def _update_screen(self):
        """更新屏幕上的圖像,并切換到新屏幕"""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()

        pygame.display.flip()

if __name__ == "__main__":
    #創(chuàng)建游戲?qū)嵗⑦\(yùn)行游戲
    ai = AlienInvasion()
    ai.run_game()

到了這里,關(guān)于Python 中出現(xiàn)AttributeError: ‘Event‘ object has no attribute ‘key‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Python 中 AttributeError: Int object Has No Attribute 錯(cuò)誤

    Python 中 AttributeError: Int object Has No Attribute 錯(cuò)誤

    int 數(shù)據(jù)類型是最基本和最原始的數(shù)據(jù)類型之一,它不僅在 Python 中,而且在其他幾種編程語(yǔ)言中都用于存儲(chǔ)和表示整數(shù)。 只要沒有小數(shù)點(diǎn),int 數(shù)據(jù)類型就可以存儲(chǔ)任何正整數(shù)或負(fù)整數(shù)。 本篇文章重點(diǎn)介紹并提供了一種解決方案,以應(yīng)對(duì)我們?cè)?Python 中使用 int 數(shù)據(jù)類型時(shí)可能

    2024年02月04日
    瀏覽(33)
  • python: AttributeError: ‘tuple‘ object has no attribute ‘xx‘

    python: AttributeError: ‘tuple‘ object has no attribute ‘xx‘

    在使用argparse模塊創(chuàng)建一個(gè)包含命令行中所有參數(shù)的對(duì)象,后續(xù)調(diào)用時(shí)出現(xiàn)這個(gè)錯(cuò)誤。 ?原始代碼如下: 在Pycharm中執(zhí)行這個(gè)命令時(shí),報(bào)錯(cuò): 但這個(gè)錯(cuò)誤很奇怪,如果是在Anaconda的spyder中執(zhí)行的話是沒有報(bào)錯(cuò)的。 如果想要在Pycharm中執(zhí)行此命令,需要把parser.parse_args()改成parse

    2024年02月05日
    瀏覽(44)
  • Python報(bào)錯(cuò):AttributeError: ‘ImageDraw‘ object has no attribute ‘textbbox‘

    報(bào)錯(cuò)原因是pillow的版本過低,導(dǎo)致不能使用 解決方法: 打開Anaconda prompt查看下載列表 刪除原有的pillow: ?從新下載pillow: 如果上面的命令報(bào)錯(cuò)下載不成功嘗試下面的代碼:(從清華鏡像下載pillow) 其他下載鏡像: 清華:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirror

    2024年02月06日
    瀏覽(30)
  • 【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘split‘

    【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘split‘

    【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘split‘ ?? 個(gè)人主頁(yè):高斯小哥 ?? 高質(zhì)量專欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分92+),分享更多關(guān)

    2024年04月14日
    瀏覽(25)
  • Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤

    Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤

    Python “ AttributeError: ‘NoneType’ object has no attribute ” 發(fā)生在我們嘗試訪問 None 值的屬性時(shí),例如 來(lái)自不返回任何內(nèi)容的函數(shù)的賦值。 要解決該錯(cuò)誤,請(qǐng)?jiān)谠L問屬性之前更正分配。 這是一個(gè)非常簡(jiǎn)單的示例,說(shuō)明錯(cuò)誤是如何發(fā)生的。 嘗試訪問或設(shè)置 None 值的屬性會(huì)導(dǎo)致錯(cuò)誤

    2024年02月06日
    瀏覽(25)
  • 【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘replace‘

    【Python】成功解決AttributeError: ‘list‘ object has no attribute ‘replace‘

    【Python】成功解決AttributeError: ‘list’ object has no attribute ‘replace’ ?? 個(gè)人主頁(yè):高斯小哥 ?? 高質(zhì)量專欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分92+),分享更多

    2024年03月25日
    瀏覽(25)
  • 【python|OpenCV】AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    當(dāng)使用OpenCV出現(xiàn)這個(gè)錯(cuò)誤時(shí),但是又沒有中文路徑或者路徑的錯(cuò)誤時(shí),可能是版本沒有對(duì)上,或者是其他的問題,我也用過很多博主的辦法,但是都沒辦法解決的時(shí)候,真的可以試一下 直接刪除,再重新下載。 目錄 情況描述 我的做法 感想 留言 本來(lái)我使用的是版本是4.7.0,

    2024年02月03日
    瀏覽(25)
  • 將圖結(jié)構(gòu)轉(zhuǎn)換矩陣數(shù)據(jù)轉(zhuǎn)換為PyTorch支持的張量類型時(shí),出現(xiàn)錯(cuò)誤AttributeError ‘Tensor‘ object has no attribute ‘todense‘

    將圖結(jié)構(gòu)轉(zhuǎn)換矩陣數(shù)據(jù)轉(zhuǎn)換為PyTorch支持的張量類型時(shí),出現(xiàn)錯(cuò)誤AttributeError ‘Tensor‘ object has no attribute ‘todense‘

    將圖結(jié)構(gòu)轉(zhuǎn)換矩陣數(shù)據(jù)轉(zhuǎn)換為PyTorch支持的張量類型時(shí),出現(xiàn)錯(cuò)誤AttributeError: ‘Tensor’ object has no attribute ‘todense’ 實(shí)例來(lái)源于《PyTorch深度學(xué)習(xí)和圖神經(jīng)網(wǎng)絡(luò) 卷1》實(shí)例26:用圖卷積神經(jīng)網(wǎng)絡(luò)為論文分類 出錯(cuò)部分p284頁(yè) 原代碼: 錯(cuò)誤提示: ? 找了一圈沒有一樣的解決方案,但

    2024年02月13日
    瀏覽(28)
  • AttributeError: ‘DataFrame‘ object has no attribute ‘iteritems‘解決方案【Bug已解決-Python】

    AttributeError: ‘DataFrame‘ object has no attribute ‘iteritems‘解決方案【Bug已解決-Python】

    本文主要介紹了AttributeError: ‘DataFrame‘ object has no attribute ‘iteritems‘解決方案,希望能對(duì)大家有所幫助。 今天在運(yùn)行項(xiàng)目時(shí),卻出現(xiàn)AttributeError: ‘DataFrame‘ object has no attribute ‘iteritems‘的錯(cuò)誤提示,具體報(bào)錯(cuò)信息如下所示: AttributeError: ‘DataFrame‘ object has no attribute ‘i

    2024年03月23日
    瀏覽(29)
  • Python產(chǎn)生關(guān)鍵詞云報(bào)錯(cuò):AttributeError: ‘ImageDraw‘ object has no attribute ‘textbbox‘

    Python產(chǎn)生關(guān)鍵詞云報(bào)錯(cuò):AttributeError: ‘ImageDraw‘ object has no attribute ‘textbbox‘

    利用jieba snownlp分別分詞,產(chǎn)生云。代碼報(bào)錯(cuò),檢查了以下代碼沒錯(cuò)。 最后在csdn找到了解決方法。 這原來(lái)是pillow的版本過低的原因。 辦法如下: 打開Anaconda prompt查看下載列表: pip? list ? 刪除現(xiàn)有的pillow: ?pip uninstall pillow ?下載新版本的pillow: pip install pillow 推薦用

    2024年02月12日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包