pygame吧 关注:724贴子:1,959
  • 13回复贴,共1

为什么我的飞船不会动,按了向右,没反应。

只看楼主收藏回复

import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
#初始化背景设置
pygame.init()
ai_settings = Settings()
#显示窗口大小
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
#标题
pygame.display.set_caption("Alien Invasion")
ship = Ship(screen)
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
run_game()
class Settings():
def __init__(self):
self.screen_width = 800
self.screen_height = 600
self.bg_color = (230, 230, 230)
import pygame
class Ship():
def __init__(self, screen):
self.screen = screen
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.moving_right = False
def update(self):
if self.moving_right:
self.rect.centerx += 1
def blitme(self):
self.screen.blit(self.image, self.rect)
import sys
import pygame
def check_events(ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT:
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.type == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()


IP属地:中国澳门1楼2019-09-24 16:08回复
    或者看看这个
    import pygame
    import sys
    class Settings():
    def __init__(self):
    self.screen_width = 1200
    self.screen_height = 800
    self.bg_color = (230, 230, 230)
    class Ship():
    def __init__(self, screen):
    self.screen = screen
    self.image = pygame.image.load('images/ship.bmp')
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()
    self.rect.centerx = self.screen_rect.centerx
    self.rect.bottom = self.screen_rect.bottom
    self.moving_right = False
    def update(self):
    if self.moving_right:
    self.rect.centerx += 1
    def blitme(self):
    self.screen.blit(self.image, self.rect)
    def check_events(ship):
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    sys.exit()
    elif event.type == pygame.KEYDOWN:
    if event.type == pygame.K_RIGHT:
    ship.moving_right = True
    elif event.type == pygame.KEYUP:
    if event.type == pygame.K_RIGHT:
    ship.moving_right = False
    def update_screen(ai_settings, screen, ship):
    screen.fill(ai_settings.bg_color)
    ship.blitme()
    pygame.display.flip()
    def run_game():
    #初始化背景设置
    pygame.init()
    ai_settings = Settings()
    #显示窗口大小
    screen = pygame.display.set_mode(
    (ai_settings.screen_width, ai_settings.screen_height))
    #标题
    pygame.display.set_caption("Alien Invasion")
    ship = Ship(screen)
    while True:
    check_events(ship)
    ship.update()
    update_screen(ai_settings, screen, ship)
    run_game()


    IP属地:中国澳门3楼2019-09-24 16:20
    回复
      搞定了,书本代码写错了,
      在使用键盘时:
      if event.key == pygame.K_RIGHT
      而不是
      if event.type == pygame.K_RIGHT
      在用pygame.QUIT时才是
      if event.type == pygame.QUIT


      IP属地:中国澳门4楼2019-09-24 16:41
      收起回复
        建议截图发代码,在贴吧直接复制代码的话会丢失格式。


        IP属地:广东来自Android客户端5楼2020-07-14 10:09
        回复
          type是KEYDOWN(或者KEYUP如果你要放手才跑....), 這方便把所有按鍵集中一起elif


          IP属地:中国香港6楼2024-03-12 21:12
          回复
            好人一生平安~


            IP属地:广西7楼2024-11-19 19:14
            回复