我在编程类的介绍中,我们必须使用pygame制作我们的owm游戏。我希望我的游戏成为一个试图避免掉落物体的人。我已经让这个男人移动,但我无法弄清楚一个得分系统以及如何让图像下降然后消失。这就是我所拥有的:
import pygame
import os,sys
from pygame.locals import *
green = (0,255,0)
white = (255,255,255)
red = (255,0,0)
black = (0,0,0)
def draw_stick_figure(screen,x,y):
# Head
pygame.draw.ellipse(screen,black,[1+x,y,10,10],0)
# Legs
pygame.draw.line(screen,black,[5+x,17+y],[10+x,27+y],2)
pygame.draw.line(screen,black,[5+x,17+y],[x,27+y],2)
# Body
pygame.draw.line(screen,green,[5+x,17+y],[5+x,7+y],2)
# Arms
pygame.draw.line(screen,green,[5+x,7+y],[9+x,17+y],2)
pygame.draw.line(screen,green,[5+x,7+y],[1+x,17+y],2)
pygame.init()
size = [700,500]
screen = pygame.display.set_mode(size)
x_coord=350
y_coord=250
x_speed=0
y_speed=0
done = False
clock=pygame.time.Clock()
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -3
elif event.key == pygame.K_RIGHT:
x_speed = 3
elif event.key == pygame.K_UP:
y_speed = -3
elif event.key == pygame.K_DOWN:
y_speed = 3
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_speed = 0
elif event.key == pygame.K_RIGHT:
x_speed = 0
elif event.key == pygame.K_UP:
y_speed = 0
elif event.key == pygame.K_DOWN:
y_speed = 0
x_coord = x_coord + x_speed
y_coord = y_coord + y_speed
screen.fill(red)
draw_stick_figure(screen,x_coord,y_coord)
pygame.display.flip()
clock.tick(20)
pygame.quit()
答案 0 :(得分:2)
你可以做的是使用精灵类
为这样的块创建一个类:
from pygame.locals import *
import pygame
import os
#added spaces show up as part of the code
class Block(pygame.sprite.Sprite):
def __init__(self, pos):
#make it a sprite
pygame.sprite.Sprite.__init__(self)
#create a rect at position (pos) that is 25 by 25 pixels
self.image = pygame.Rect(pos[0], pos[1] 25, 25)
#make the rect a class variable that can be moved
self.rect = self.image.get_rect()
def update(self):
#move rect 20 pixels each update (can be adjusted)
self.rect.y += 20
#if it goes to to the bottom of the screen delete it
if self.rect.y > screen_height:
self.kill()
然后创建一个这样的块:
block = Block([20, 20])
然后绘制它并在主循环中更新它,如下所示:
block.update()
block.draw()
你可以使用精灵组创建多个会掉落的块:
block_list = pygame.sprite.Group()
for i in xrange(10):
block = Block([i,i])
block_list.add(block)
现在你可以用同样的方式更新整个组:
block_list.draw()
block_list.update()
你应该对玩家做同样的事情并使用图像而不是用线条绘制他
对不起,如果我不知所措,一开始很难,但学习非常有用
祝你好运:)答案 1 :(得分:0)
加载图片。设置图片x
,y
,y_speed
。更改主循环中的图像位置。如果图片y
大于屏幕高度,请将图片y
设置为0以再次使用它。使用pygame.Rect()
用于图像和播放器,您可以使用pygame.rect.collision_rect()
来检测碰撞玩家和图像并更改分数。
等等。