我正在制作一个非常基本的游戏来学习 pygame 并且我正在尝试制作它,以便鸭子(玩家)必须躲避岩石但我不能让岩石随机放置然后滚动到一边
这是我当前的代码:
import pygame
import os
import random
img_path = os.path.join('C:\Python27', 'player.png')
img_path2 = os.path.join('C:\Python27', 'rock.png')
class Bird(object):
def __init__(self):
self.image = pygame.image.load(img_path)
self.x = 0
self.y = 0
def handle_keys(self):
""" Handles Keys """
key = pygame.key.get_pressed()
dist = 3
if key[pygame.K_DOWN]:
self.y += dist
elif key[pygame.K_UP]:
self.y -= dist
if key[pygame.K_RIGHT]:
self.x += dist
elif key[pygame.K_LEFT]:
self.x -= dist
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
def background(self, surface):
bg = os.path.join('C:\Python27', 'bg.png')
self.image2 = pygame.image.load(bg)
surface.blit(self.image2, (0,0))
class Rock(object):
def __init__(self):
self.image = pygame.image.load(img_path2)
self.x = 640
self.y = 0
def rock(self):
dist = 2
if running == True:
self.x -=dist
def rock_draw(self, surface):
surface.blit(self.image, (self.x, self.y))
pygame.init()
screen = pygame.display.set_mode((640, 400))
bird = Bird() # create an instance
rock = Rock()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
bird.handle_keys()
rock.rock()
screen.fill((255,255,255))
bird.background(screen)
bird.draw(screen)
rock.rock_draw(screen)
pygame.display.update()
clock.tick(40)
我希望岩石从屏幕右侧开始并滚动,然后一旦第一个消失或一半消失,在不同的 y 位置产生另一个