IM做一個非常基本的遊戲,學習pygame的和IM試圖讓這個鴨子(播放器) 有躲閃的岩石,但我不能去隨意放置的石塊,然後滾動到側面在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位置
那麼什麼樣的行爲,你得到目前? – Pace 2013-04-25 01:43:43
它剛剛開始在右上角左移動 – Serial 2013-04-25 04:54:54