私は自分のゲームに問題があります。画面上のゲームからゲームを再開しようとすると、ロボット(プレイヤー)はその開始位置に戻りますが、スクロールされた画面はまだ同じ位置ゲームを再開する(スクロールした画面をリセットする)
簡単な方法で画面の位置をリセットするか、元のプラットフォームをすべて再描画する必要がありますか?
ここには、使用されているコードの主要部分があります。私はゲームを再起動して実行するコードが含まれていないが、それは単に(game_loopを実行します)任意のヘルプ
import pygame as pg
import time
import random
pg.init()#initiates pygame
display_height = 690#Creates width and height of screen
display_width = 1024
#Colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
grass = (24,85,36)
yellow = (255,255,0)
lightGrey = (184,184,184)
grey = (73,71,65)
Robot_height = 99#Height of robot
Robot_width = 112#Width of robot
Bullet_Fired = False
PowerUp_Active = False
Robot_acc = 0.3 #Robot Acceleration
vec = pg.math.Vector2
gameDisplay = pg.display.set_mode((display_width,display_height)) #Sets display properties of window
pg.display.set_caption ("Game") #Title on window
clock = pg.time.Clock()
#Class for platforms
class Platform(pg.sprite.Sprite):
def __init__(self, x,y,w,h):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((w,h))#sets width and height of platform
self.image.fill(grass)#Fills rectangle with blue
self.rect = self.image.get_rect()#Rectangle set
self.rect.x = x#x position
self.rect.y = y#y position
#List of platforms x , y , width , height
PLATFORM_LIST = [[0,display_height - 40,2000,40],
[2300,display_height - 40,1000,40],
]
#Platform group
platforms = pg.sprite.Group()
#Checks through "PLATFORM_LIST" and adds all the platforms the the grounp "platforms"
for plat in PLATFORM_LIST:
p = Platform(*plat)
platforms.add(p)
#Draws platforms to the screen
def draw():
for plat in platforms:
pg.draw.rect(gameDisplay, grass, plat)
#Class for robot
class RobotClass(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((Robot_width,Robot_height))#Height and width of the robot
self.rect = self.image.get_rect()#Gets rectangle of robot
self.rect.center = (display_width/2, display_height/2)#Location of center of robot
self.RobotPos = vec(display_width/2, display_height/2)#Position of robot as vector
self.bottom = (0,0)#Bottom of robot
self.vel = vec(0, 0)#Robots velocity
self.acc = vec(0, 0.3)#Robots Acceleration
startX = display_width/2
startY = display_height/2
#Creates Robot
Robot = RobotClass()
#Jump function
def jump():
#Checks pixel below robot to see if there is a collision
Robot.rect.x = Robot.rect.x +1
hits = pg.sprite.spritecollide(Robot , platforms, False)
Robot.rect.x = Robot.rect.x -1
if hits:
#Gives robot velocity of 5 upwards
Robot.vel.y = -10
def game_loop():
global PLATFORM_LIST
global startX
global startY
global backgroundImg
Robot.RobotPos = (startX,startY)
score = 0 #Score
lives = 3 #Robot Lives
Robot_friction = -0.3 #Friction value
vec = pg.math.Vector2 #Setting vec as vector quantity
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit
quit()
#Starts acceleration when key is pressed
if event.type == pg.KEYDOWN:
if event.key == pg.K_LEFT:
Robot.acc.x = -Robot_acc
elif event.key == pg.K_RIGHT:
Robot.acc.x = Robot_acc
elif event.key == pg.K_UP:
jump()
#Adds friction to accleration to slow robot down when key is not being pressed
if event.type == pg.KEYUP:
if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
Robot.acc.x = Robot.acc.x * Robot_friction
#Adjusts velocity of robot by adding the acceleration on each cycle
Robot.vel = Robot.vel+ Robot.acc
#gameDisplay.fill(sky)
gameDisplay.blit(backgroundImg,(0,0))
#Changes Robot position according to its velocity,acceleration and the friction
Robot.RobotPos = Robot.RobotPos + Robot.vel + 0.5 * Robot.acc
#Loads robot onto screen
gameDisplay.blit(robotImg,(Robot.rect))
#pg.draw.rect(gameDisplay, red, Robot.rect, 2
display_lives(lives)
display_score(score)#lives
#Sets bottom of robot to its position
Robot.rect.midbottom = Robot.RobotPos
#Collision detection
if Robot.vel.y > 0:
hits = pg.sprite.spritecollide(Robot , platforms, False)
if hits:
#Puts Robot on top of platform
Robot.RobotPos.y = hits[0].rect.top + 1
Robot.vel.y = 0
#Scrolling
if Robot.rect.left < display_width/4:
Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x)
startX = startX + abs(Robot.vel.x)
for plat in platforms:
plat.rect.x = plat.rect.x + int(abs(Robot.vel.x))
if Robot.rect.right > (display_width-display_width/4):
Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x)
startX = startX - abs(Robot.vel.x)
for plat in platforms:
plat.rect.x = plat.rect.x - int(abs(Robot.vel.x))
draw()
#Losing a life
if Robot.rect.top > display_height:
lives = lives - 1
Robot.RobotPos.y = Robot.RobotPos.y - (40+Robot_height)
Robot.RobotPos.x = Robot.RobotPos.x - 200
Robot.vel.x = 0
#Sets top velocity of robot
if Robot.vel.x > 6:
Robot.vel.x = 6
if Robot.vel.x < -6:
Robot.vel.x = -6
#Makes robot velocity = 0 when it is close to 0
if Robot.vel.x < 0.05 and Robot.vel.x > -0.05:
Robot.acc.x = 0
Robot.vel.x = 0
#Draws the platforms to the screen and adds them to platform group
pg.display.update()#Updates display
clock.tick(60)
game_intro()
pg.quit()
quit()
なぜそれを再描画しないのですか? –
画面がスクロールされているときに、プラットフォームのすべての値が調整されているので、理論的には最後に最後の位置に再描画されます –
すべてのスプライトを画面から削除する方法はありますか? –