日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python如何实现飞机上下移动_python实现飞船游戏的纵向移动

發(fā)布時間:2024/1/18 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何实现飞机上下移动_python实现飞船游戏的纵向移动 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例為大家分享了python實現(xiàn)飛船游戲的縱向移動,供大家參考,具體內(nèi)容如下

我是跟著書里一步步寫到橫向移動后 我就想怎么縱向移動,放上自己寫的代碼,如果有問題的話,請指出來,我也是剛剛學習python,希望可以跟大家多多交流。

新增的就是有關(guān)up和down的代碼了。

我自己是成功了,肯定有其他的更優(yōu)化的,那就等我在學習一段時間吧。

附上代碼:

game_function:

import sys

import pygame

# 監(jiān)視鍵盤和鼠標事件

def check_keydown_events(event,ship):

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_UP:

ship.moving_up=True

elif event.key==pygame.K_DOWN:

ship.moving_down=True

def check_keyup_events(event,ship):

if event.key==pygame.K_RIGHT:

ship.moving_right=False

elif event.key==pygame.K_LEFT:

ship.moving_left=False

elif event.key==pygame.K_UP:

ship.moving_up=False

elif event.key==pygame.K_DOWN:

ship.moving_down=False

def check_events(ship):

for event in pygame.event.get():

if event.type==pygame.QUIT:

sys.exit()

elif event.type==pygame.KEYDOWN:

check_keydown_events(event,ship)

elif event.type==pygame.KEYUP:

check_keyup_events(event,ship)

def update_screen(ai_settings,screen,ship):

screen.fill(ai_settings.bg_color)

ship.blitme()

pygame.display.flip() #讓最近回執(zhí)的屏幕可見(刷新)

ship:

import pygame

class Ship():

def __init__(self,ai_settings,screen):

self.screen=screen

self.ai_settings=ai_settings

#加載飛船圖像

self.image=pygame.image.load('chengyan_ship.bmp')

self.rect=self.image.get_rect()

self.screen_rect=screen.get_rect()

self.rect.centerx=self.screen_rect.centerx #x的坐標

self.rect.centery=self.screen_rect.centery #y的坐標

self.rect.bottom=self.screen_rect.bottom

self.moving_right=False

self.moving_left=False

self.moving_up=False

self.moving_down=False

#得到飛船移動到最下面的值(我不知道有沒有表述清楚...就是只能飛到界面的最下面)

self.screen_top=self.rect.top

def update(self):

#橫向移動

if self.moving_right and self.rect.right

self.rect.centerx+=self.ai_settings.ship_speed_factor

#縱向移動

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

self.rect.centerx-=self.ai_settings.ship_speed_factor

if self.moving_up and self.rect.top>0:

self.rect.centery-=self.ai_settings.ship_speed_factor

if self.moving_down and self.rect.top

self.rect.centery+=self.ai_settings.ship_speed_factor

self.rect.centerx=self.rect.centerx

self.rect.centery=self.rect.centery

def blitme(self):

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的python如何实现飞机上下移动_python实现飞船游戏的纵向移动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。