为什么我的飞船不会动,按了向右,没反应。
pygame吧
全部回复
仅看楼主
level 3
vv键 楼主
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()
2019年09月24日 08点09分 1
level 3
vv键 楼主
或者看看这个
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()
2019年09月24日 08点09分 3
level 3
vv键 楼主
搞定了,书本代码写错了,
在使用键盘时:
if event.key == pygame.K_RIGHT
而不是
if event.type == pygame.K_RIGHT
在用pygame.QUIT时才是
if event.type == pygame.QUIT
2019年09月24日 08点09分 4
2025年2月24日17:34:59,好人一生平安!!!
2025年02月24日 09点02分
大哥大哥 我发自内心的感谢你,2020年2月20日,你解决了我的大问题!!!
2020年02月20日 02点02分
大哥大哥 我也发自内心的感谢你,2020年7月3日,你也解决了我的大问题!!!
2020年07月03日 02点07分
大哥大哥我也发自内心的感谢你,2020年7月3日,你也解决了我的大问题!!!
2020年07月07日 14点07分
level 11
建议截图发代码,在贴吧直接复制代码的话会丢失格式。
2020年07月14日 02点07分 5
level 6
type是KEYDOWN(或者KEYUP如果你要放手才跑....), 這方便把所有按鍵集中一起elif
2024年03月12日 13点03分 6
level 1
好人一生平安~
2024年11月19日 11点11分 7
1