import sys
import pygame
def check_keydown_events(event,ship):
"""Respond to the keypressess."""
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):
"""Respond to the keyreleases."""
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):
"""Respond to keypresses and mouse events."""
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)
import pygame
class Ship():
def __init__(self,ai_settings,screen):
"""Initialize the ship and set its starting position."""
self.screen = screen
self.ai_settings = ai_settings
#Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.png')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
#Store a decimal value for the ship's center.
self.center = float(self.rect.centerx)
#Movement Flags
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
"""Update the ship's position based on the movement flag."""
#Update the ship's center value, not the rect
if self.moving_right and self.rect.right<self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left>0:
self.center -= self.ai_settings.ship_speed_factor
#Up-dwon motion added ***Please check the logic for 1200x800 screen***
if self.moving_up and self.rect.top<self.screen_rect.top:
self.center += self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom>800:
self.center -= self.ai_settings.ship_speed_factor
#Update rect object from self.center.
self.rect.centerx = self.center
答案 0 :(得分:1)
ship.py
中只有几个推文:
在self.center = float(self.rect.centerx)
下,添加self.bottom = float(self.rect.bottom)
并更改:
if self.moving_up and self.rect.top<self.screen_rect.top:
self.center += self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom>800:
self.center -= self.ai_settings.ship_speed_factor
为:
if self.moving_up and self.rect.top<self.screen_rect.top:
self.bottom -= self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom<800:
self.bottom += self.ai_settings.ship_speed_factor
最后在self.rect.centerx = self.center
下,添加self.rect.bottom = self.bottom
。
这应该可以让你在游戏中上下起伏!
答案 1 :(得分:0)
我发现这是一个问题,在这些功能中:
check_keydown_events
check_keyup_events
check_events
在每种情况下,您都在执行if-elif-elif-elif
语句。这意味着当if
语句为True
时,elif
语句将不会运行。
在您的情况下,如果按下左或右键,您将永远无法向上或向下移动。
您应该更改此代码,以便仅使用if-elif
分别使用向上/向下和向左/向右移动。
这是一个(更正的)示例:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
if event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
如果您将每个if-elif-elif-elif
语句更改为if-elif-if-elif
,则会解决您的问题。
答案 2 :(得分:0)
<强> Ship.py 强>
def check_keydown_event(event, ship):
"""Respond to keypresses"""
....
elif event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
def check_keyup_event(event, ship):
....
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
<强> game_function.py 强>
var zip = new JSZip();
var count = 0;
var zipFilename = "zipFilename.zip";
var urls = [
'http://image-url-1',
'http://image-url-2',
'http://image-url-3'
];
urls.forEach(function(url){
var filename = "filename";
// loading a file and add it in a zip file
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
throw err; // or handle the error
}
zip.file(filename, data, {binary:true});
count++;
if (count == urls.length) {
var zipFile = zip.generate({type: "blob"});
saveAs(zipFile, zipFilename);
}
});
});