出于某种原因,当我运行程序时,我的坏图像(小行星)在移动时滞后并且断断续续但目标没有。我尝试在每个移动代码下放置pygame.display.update()
,但它只是闪烁。此外,我切换了目标和坏人的移动代码的位置,导致目标滞后和口吃。
import pygame, sys, random
pygame.init()
def game():
width = 800
height = 600
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Asteroid Dodger")
background = pygame.image.load("field_of_stars_full.jpg")
player = pygame.image.load("Silver_Surfer.png")
px = 350
py = 450
baddie = pygame.image.load("Asteroid.png")
baddiepos = baddie.get_rect()
target = pygame.image.load("Cosmic_Clump.png")
targetpos = [0,0]
gamespeed = 100
speed = [gamespeed*random.randint(1,3),gamespeed*random.randint(1,3)]
speed2 = [gamespeed*10,gamespeed*10]
clock = pygame.time.Clock()
movex = movey = 0
targetvisible = True
playervisible = True
font = pygame.font.Font(None,36)
score = 0
scoretext = font.render("Score: " + str(score), 2, (255,255,255))
lives = 3
livestext = font.render("Lives: " + str(lives),2, (255,255,255))
while True:
screen.blit(background, (0,0))
screen.blit(player, (px,py))
scoretext = font.render("Score: " + str(score), 2, (255,255,255))
livestext = font.render("Lives: " + str(lives), 2, (255,255,255))
screen.blit(scoretext, (20,20))
screen.blit(livestext, (670,20))
if targetvisible == True:
seconds = clock.tick()/1000.0
targetpos[0] += seconds*speed[0]
targetpos[1] += seconds*speed[1]
screen.blit(target, targetpos)
else:
screen.blit(target, targetpos)
if playervisible == True:
seconds = clock.tick()/1000.0
baddiepos[0] += seconds*speed2[0]
baddiepos[1] += seconds*speed2[1]
screen.blit(baddie, baddiepos)
else:
screen.blit(baddie, baddiepos)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_ESCAPE:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
movex = 2
elif event.key == pygame.K_LEFT:
movex = -2
player = pygame.transform.flip(player, 1, 0)
elif event.key == pygame.K_DOWN:
movey = 2
elif event.key == pygame.K_UP:
movey = -2
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
movex = 0
elif event.key == pygame.K_LEFT:
movex = 0
player = pygame.transform.flip(player, 1, 0)
elif event.key == pygame.K_DOWN:
movey = 0
elif event.key == pygame.K_UP:
movey = 0
elif event.key == pygame.K_ESCAPE:
pygame.quit()
px += movex
py += movey
if targetpos[0]+60 > width or targetpos[0]+15 < 0:
speed[0] = -speed[0]
targetpos[0] += seconds*speed[0]
if targetpos[1]+70 > height or targetpos[1]+30 < 0:
speed[1] = -speed[1]
targetpos[1] += seconds*speed[1]
if abs(targetpos[0]-px)<20 and abs(targetpos[1]-py)<20:
targetvisible = False
score += 10
targetpos[0] = random.randint(0,720)
targetpos[1] = random.randint(0,520)
screen.blit(target, (targetpos))
targetvisible = True
if baddiepos[0] > width or baddiepos[0] < 0:
speed2[0] = -speed2[0]
baddiepos[0] += seconds*speed2[0]
if baddiepos[1] > height or baddiepos[1] < 0:
speed2[1] = -speed2[1]
baddiepos[1] += seconds*speed2[1]
if abs(baddiepos[0]-px)<20 and (baddiepos[1]-py)<20:
playervisble = False
lives -= 1
baddiepos[0] = random.randint(0,720)
baddiepos[1] = random.randint(0,520)
screen.blit(baddie, (baddiepos))
playervisible = True
if px > 800:
px = 0
elif px < 0:
px = 800
if py > 600:
py = 0
elif py < 0:
py = 600
答案 0 :(得分:0)
baddie
排名取决于second
变量clock.tick()
。
来自pygame.clock.tick()文档:
它将计算多少 自上一次调用(clock.tick())以来已经过了毫秒。
所以它给你的值0 - 0.001,但大多数是0.所以baddie
大多数都没有移动。
文档:
每帧应调用一次此方法。
也许你会使用pygame.time.get_ticks(),但我不知道你为什么需要它。
您的代码可能如下所示:
import pygame
import sys
import random
#----------------------------------------------------------------------
#~ PLAYER_IMAGE = "ball2.png"
#~ TARGET_IMAGE = "ball3.png"
#~ BADDIE_IMAGE = "ball1.png"
#~ BACKGROUND_IMAGE = "background.jpg"
PLAYER_IMAGE = "Silver_Surfer.png"
TARGET_IMAGE = "Cosmic_Clump.png"
BADDIE_IMAGE = "Asteroid.png"
BACKGROUND_IMAGE = "field_of_stars_full.jpg"
#----------------------------------------------------------------------
class Player():
def __init__(self, screen_rect):
self.screen_rect = screen_rect
self.image = pygame.image.load(PLAYER_IMAGE)
self.rect = self.image.get_rect()
self.rect.centerx = 350
self.rect.centery = 450
self.move_x = self.move_y = 0
self.visible = True
def draw(self, screen):
screen.blit(self.image, self.rect.topleft)
def flip(self):
self.image = pygame.transform.flip(self.image, 1, 0)
def update(self):
self.rect.x += self.move_x
self.rect.y += self.move_y
if self.rect.centerx > self.screen_rect.right:
self.rect.centerx = self.screen_rect.left
elif self.rect.centerx < self.screen_rect.left:
self.rect.centerx = self.screen_rect.right
if self.rect.centery > self.screen_rect.bottom:
self.rect.centery = self.screen_rect.top
elif self.rect.centery < self.screen_rect.top:
self.rect.centery = self.screen_rect.bottom
def event_handler(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.move_x = 5
elif event.key == pygame.K_LEFT:
self.move_x = -5
self.flip()
elif event.key == pygame.K_DOWN:
self.move_y = 5
elif event.key == pygame.K_UP:
self.move_y = -5
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_RIGHT, pygame.K_LEFT):
self.move_x = 0
elif event.key in (pygame.K_DOWN, pygame.K_UP):
self.move_y = 0
if event.key == pygame.K_LEFT:
self.flip()
def random_position(self):
self.rect.centerx = random.randint(0,720)
self.rect.centery = random.randint(0,520)
#----------------------------------------------------------------------
class Baddie():
def __init__(self, screen_rect, speed):
self.screen_rect = screen_rect
self.image = pygame.image.load(BADDIE_IMAGE)
self.rect = self.image.get_rect()
self.speed_x = speed * random.randint(1,3)
self.speed_y = speed * random.randint(1,3)
self.visible = True
def draw(self, screen):
screen.blit(self.image, self.rect.topleft)
def update(self, player_visible):
if player_visible:
self.rect.x += self.speed_x * random.randint(1, 5)
self.rect.y += self.speed_y * random.randint(1, 5)
if self.rect.centerx > self.screen_rect.right:
self.rect.centerx = self.screen_rect.left
elif self.rect.centerx < self.screen_rect.left:
self.rect.centerx = self.screen_rect.right
if self.rect.centery > self.screen_rect.bottom:
self.rect.centery = self.screen_rect.top
elif self.rect.centery < self.screen_rect.top:
self.rect.centery = self.screen_rect.bottom
def random_position(self):
self.rect.centerx = random.randint(0,720)
self.rect.centery = random.randint(0,520)
#----------------------------------------------------------------------
class Target():
def __init__(self, screen_rect, speed):
self.screen_rect = screen_rect
self.image = pygame.image.load(TARGET_IMAGE)
self.rect = self.image.get_rect()
self.rect.centerx = 100
self.rect.centery = 100
self.speed_x = speed * 10
self.speed_y = speed * 10
self.visible = True
def draw(self, screen):
screen.blit(self.image, self.rect.topleft)
def update(self, screen):
if self.visible:
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.right > self.screen_rect.right or self.rect.left < 0:
self.speed_x = -self.speed_x
if self.rect.bottom > self.screen_rect.bottom or self.rect.top < 0:
self.speed_y = -self.speed_y
def random_position(self):
self.rect.centerx = random.randint(0,720)
self.rect.centery = random.randint(0,520)
#----------------------------------------------------------------------
class Game():
def __init__(self):
pygame.init()
width = 800
height = 600
self.screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Asteroid Dodger")
# --- variables ---
self.score = 0
self.lives = 3
self.gamespeed = 1
self.font = pygame.font.Font(None,36)
self.scoretext = self.font.render("Score: " + str(self.score), 2, (255,255,255))
self.livestext = self.font.render("Lives: " + str(self.lives), 2, (255,255,255))
self.pausetext = self.font.render("PAUSE", 2, (255,255,255))
# center pausetext on screen
self.pausetext_rect = self.pausetext.get_rect(center=self.screen.get_rect().center)
# --- sprites ---
self.background = pygame.image.load(BACKGROUND_IMAGE)
screen_rect = self.screen.get_rect()
self.player = Player(screen_rect)
self.baddie = Baddie(screen_rect, self.gamespeed)
self.target = Target(screen_rect, self.gamespeed)
#----------------------------------------
def run(self):
clock = pygame.time.Clock()
RUNNING = True
PAUSED = False
while RUNNING:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
RUNNING = False
elif event.key == pygame.K_SPACE:
PAUSED = not PAUSED
self.player.event_handler(event)
# --- updates ---
if not PAUSED:
self.player.update()
self.baddie.update(self.player.visible)
self.target.update(self.player.visible)
# detect cilision
if pygame.sprite.collide_rect(self.player, self.baddie):
self.lives -= 1
self.player.random_position()
if pygame.sprite.collide_rect(self.player, self.target):
self.score += 10
self.target.random_position()
self.scoretext = self.font.render("Score: " + str(self.score), 2, (255,255,255))
self.livestext = self.font.render("Lives: " + str(self.lives), 2, (255,255,255))
# --- draws ---
self.screen.blit(self.background, (0,0))
self.player.draw(self.screen)
self.baddie.draw(self.screen)
self.target.draw(self.screen)
self.screen.blit(self.scoretext, (20,20))
self.screen.blit(self.livestext, (670,20))
if PAUSED:
self.screen.blit(self.pausetext, self.pausetext_rect)
pygame.display.update()
# --- FPS ---
clock.tick(25) # 25 Frames Per Second
# --- the end ---
pygame.quit()
#----------------------------------------------------------------------
Game().run()
PS。如果有人需要图像'ball1.png','ball2.png','ball3.png'来运行这个例子你可以找到它来回答
答案 1 :(得分:0)
使用
pygame.image.load("image").**convert**()
或 pygame.image.load(“ image”)。 convert_alpha ()for png